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;
44 protected Dictionary<Symbol, SymbolData>
_symbolData {
get; init; }
62 _fastPeriod = fastPeriod;
63 _slowPeriod = slowPeriod;
64 _signalPeriod = signalPeriod;
65 _movingAverageType = movingAverageType;
66 _resolution = resolution;
68 Name = $
"{nameof(MacdAlphaModel)}({fastPeriod},{slowPeriod},{signalPeriod},{movingAverageType},{resolution})";
81 if (sd.Security.Price == 0)
87 var normalizedSignal = sd.MACD.Signal / sd.Security.Price;
88 if (normalizedSignal > BounceThresholdPercent)
92 else if (normalizedSignal < -BounceThresholdPercent)
98 if (direction == sd.PreviousDirection)
103 sd.PreviousDirection = direction;
107 CancelInsights(algorithm, sd.Security.Symbol);
111 var insightPeriod = _resolution.ToTimeSpan().Multiply(_fastPeriod);
112 var insight =
Insight.
Price(sd.Security.Symbol, insightPeriod, direction);
113 _insightCollection.
Add(insight);
115 yield
return insight;
127 foreach (var added
in changes.AddedSecurities)
133 _symbolData.Add(added.Symbol,
new SymbolData(algorithm, added, _fastPeriod, _slowPeriod, _signalPeriod, _movingAverageType, _resolution));
136 foreach (var removed
in changes.RemovedSecurities)
138 var symbol = removed.Symbol;
144 algorithm.SubscriptionManager.RemoveConsolidator(symbol, data.Consolidator);
149 CancelInsights(algorithm, symbol);
155 if (_insightCollection.
TryGetValue(symbol, out var insights))
157 algorithm.
Insights.Cancel(insights);
158 _insightCollection.
Clear(
new[] { symbol });