/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using QuantConnect.Data.Market;
using System;
using System.Collections.Generic;
using System.Linq;
namespace QuantConnect.Indicators
{
///
/// The advance-decline indicator compares the number of stocks
/// that closed higher against the number of stocks
/// that closed lower than their previous day's closing prices.
///
public abstract class AdvanceDeclineIndicator : TradeBarIndicator, IIndicatorWarmUpPeriodProvider
{
private IDictionary _previousPeriod = new Dictionary();
private IDictionary _currentPeriod = new Dictionary();
private readonly Func, decimal> _computeSubValue;
private readonly Func _computeMainValue;
private DateTime? _currentPeriodTime = null;
///
/// Initializes a new instance of the class
///
public AdvanceDeclineIndicator(string name, Func, decimal> computeSub, Func computeMain)
: base(name)
{
_computeSubValue = computeSub;
_computeMainValue = computeMain;
}
///
/// Add tracking asset issue
///
/// tracking asset issue
public virtual void Add(Symbol asset)
{
if (!_currentPeriod.ContainsKey(asset.ID))
{
_currentPeriod.Add(asset.ID, null);
}
}
///
/// Deprecated
///
[Obsolete("Please use Add(asset)")]
public void AddStock(Symbol asset)
{
Add(asset);
}
///
/// Remove tracking asset issue
///
/// tracking asset issue
public virtual void Remove(Symbol asset)
{
_currentPeriod.Remove(asset.ID);
}
///
/// Deprecated
///
[Obsolete("Please use Remove(asset)")]
public void RemoveStock(Symbol asset)
{
Remove(asset);
}
///
/// Gets a flag indicating when this indicator is ready and fully initialized
///
public override bool IsReady => _previousPeriod.Keys.Any();
///
/// Required period, in data points, for the indicator to be ready and fully initialized.
///
public int WarmUpPeriod => 2;
///
/// Computes the next value of this indicator from the given state
///
/// The input given to the indicator
/// A new value for this indicator
protected override decimal ComputeNextValue(TradeBar input)
{
var advStocks = new List();
var dclStocks = new List();
TradeBar tradeBar;
foreach (var stock in _currentPeriod)
{
if (!_previousPeriod.TryGetValue(stock.Key, out tradeBar) || tradeBar == null)
{
continue;
}
else if (stock.Value.Close < tradeBar.Close)
{
dclStocks.Add(stock.Value);
}
else if (stock.Value.Close > tradeBar.Close)
{
advStocks.Add(stock.Value);
}
}
return _computeMainValue(_computeSubValue(advStocks), _computeSubValue(dclStocks));
}
///
/// Computes the next value of this indicator from the given state
///
/// The input given to the indicator
/// A new value for this indicator
protected override IndicatorResult ValidateAndComputeNextValue(TradeBar input)
{
Enqueue(input);
if (!_previousPeriod.Keys.Any() || _currentPeriod.Any(p => p.Value == null))
{
return new IndicatorResult(0, IndicatorStatus.ValueNotReady);
}
var vNext = ComputeNextValue(input);
return new IndicatorResult(vNext);
}
///
/// Resets this indicator to its initial state
///
public override void Reset()
{
_previousPeriod.Clear();
foreach (var key in _currentPeriod.Keys.ToList())
{
_currentPeriod[key] = null;
}
base.Reset();
}
private void Enqueue(TradeBar input)
{
if (input.EndTime == _currentPeriodTime)
{
_previousPeriod[input.Symbol.ID] = input;
return;
}
if (input.Time > _currentPeriodTime)
{
_previousPeriod.Clear();
foreach (var key in _currentPeriod.Keys.ToList())
{
_previousPeriod[key] = _currentPeriod[key];
_currentPeriod[key] = null;
}
_currentPeriodTime = input.Time;
}
if (_currentPeriod.ContainsKey(input.Symbol.ID) && (!_currentPeriodTime.HasValue || input.Time == _currentPeriodTime))
{
_currentPeriod[input.Symbol.ID] = input;
if (!_currentPeriodTime.HasValue)
{
_currentPeriodTime = input.Time;
}
}
}
}
}