16 using System.Collections.Generic;
33 private readonly
int _fastPeriod;
34 private readonly
int _slowPeriod;
35 private readonly
int _signalPeriod;
38 private const decimal BounceThresholdPercent = 0.01m;
40 protected readonly Dictionary<Symbol, SymbolData> _symbolData;
58 _fastPeriod = fastPeriod;
59 _slowPeriod = slowPeriod;
60 _signalPeriod = signalPeriod;
61 _movingAverageType = movingAverageType;
62 _resolution = resolution;
63 _symbolData =
new Dictionary<Symbol, SymbolData>();
64 Name = $
"{nameof(MacdAlphaModel)}({fastPeriod},{slowPeriod},{signalPeriod},{movingAverageType},{resolution})";
75 foreach (var sd
in _symbolData.Values)
77 if (sd.Security.Price == 0)
83 var normalizedSignal = sd.MACD.Signal / sd.Security.Price;
84 if (normalizedSignal > BounceThresholdPercent)
88 else if (normalizedSignal < -BounceThresholdPercent)
94 if (direction == sd.PreviousDirection)
99 sd.PreviousDirection = direction;
103 CancelInsights(algorithm, sd.Security.Symbol);
107 var insightPeriod = _resolution.ToTimeSpan().Multiply(_fastPeriod);
108 var insight =
Insight.
Price(sd.Security.Symbol, insightPeriod, direction);
109 _insightCollection.
Add(insight);
111 yield
return insight;
123 foreach (var added
in changes.AddedSecurities)
125 if (_symbolData.ContainsKey(added.Symbol))
129 _symbolData.Add(added.Symbol,
new SymbolData(algorithm, added, _fastPeriod, _slowPeriod, _signalPeriod, _movingAverageType, _resolution));
132 foreach (var removed
in changes.RemovedSecurities)
134 var symbol = removed.Symbol;
137 if (_symbolData.TryGetValue(symbol, out data))
140 algorithm.SubscriptionManager.RemoveConsolidator(symbol, data.Consolidator);
141 _symbolData.Remove(symbol);
145 CancelInsights(algorithm, symbol);
151 if (_insightCollection.
TryGetValue(symbol, out var insights))
153 algorithm.
Insights.Cancel(insights);
154 _insightCollection.
Clear(
new[] { symbol });