18 using System.Collections.Generic;
30 private IDictionary<SecurityIdentifier, TradeBar> _previousPeriod =
new Dictionary<SecurityIdentifier, TradeBar>();
31 private IDictionary<SecurityIdentifier, TradeBar> _currentPeriod =
new Dictionary<SecurityIdentifier, TradeBar>();
32 private readonly Func<IEnumerable<TradeBar>, decimal> _computeSubValue;
33 private readonly Func<decimal, decimal, decimal> _computeMainValue;
34 private DateTime? _currentPeriodTime =
null;
39 public AdvanceDeclineIndicator(
string name, Func<IEnumerable<TradeBar>, decimal> computeSub, Func<decimal, decimal, decimal> computeMain)
42 _computeSubValue = computeSub;
43 _computeMainValue = computeMain;
52 if (!_currentPeriod.ContainsKey(asset.
ID))
54 _currentPeriod.Add(asset.
ID,
null);
61 [Obsolete(
"Please use Add(asset)")]
73 _currentPeriod.Remove(asset.
ID);
79 [Obsolete(
"Please use Remove(asset)")]
88 public override bool IsReady => _previousPeriod.Keys.Any();
102 var advStocks =
new List<TradeBar>();
103 var dclStocks =
new List<TradeBar>();
106 foreach (var stock
in _currentPeriod)
108 if (!_previousPeriod.TryGetValue(stock.Key, out tradeBar) || tradeBar ==
null)
112 else if (stock.Value.Close < tradeBar.Close)
114 dclStocks.Add(stock.Value);
116 else if (stock.Value.Close > tradeBar.Close)
118 advStocks.Add(stock.Value);
122 return _computeMainValue(_computeSubValue(advStocks), _computeSubValue(dclStocks));
134 if (!_previousPeriod.Keys.Any() || _currentPeriod.Any(p => p.Value ==
null))
148 _previousPeriod.Clear();
149 foreach (var key
in _currentPeriod.Keys.ToList())
151 _currentPeriod[key] =
null;
157 private void Enqueue(
TradeBar input)
159 if (input.
EndTime == _currentPeriodTime)
161 _previousPeriod[input.
Symbol.
ID] = input;
165 if (input.
Time > _currentPeriodTime)
167 _previousPeriod.Clear();
168 foreach (var key
in _currentPeriod.Keys.ToList())
170 _previousPeriod[key] = _currentPeriod[key];
171 _currentPeriod[key] =
null;
173 _currentPeriodTime = input.
Time;
176 if (_currentPeriod.ContainsKey(input.
Symbol.
ID) && (!_currentPeriodTime.HasValue || input.
Time == _currentPeriodTime))
178 _currentPeriod[input.
Symbol.
ID] = input;
179 if (!_currentPeriodTime.HasValue)
181 _currentPeriodTime = input.
Time;