17 using System.Collections.Generic;
31 private readonly Dictionary<Symbol, SymbolData> _symbolDataBySymbol =
new Dictionary<Symbol, SymbolData>();
33 private readonly
int _period;
47 _resolution = resolution;
48 Name = $
"{nameof(RsiAlphaModel)}({_period},{_resolution})";
60 var insights =
new List<Insight>();
61 foreach (var kvp
in _symbolDataBySymbol)
64 var rsi = kvp.Value.RSI;
65 var previousState = kvp.Value.State;
66 var state = GetState(rsi, previousState);
68 if (state != previousState && rsi.IsReady)
70 var insightPeriod = _resolution.ToTimeSpan().Multiply(_period);
74 case State.TrippedLow:
78 case State.TrippedHigh:
84 kvp.Value.State = state;
99 foreach (var security
in changes.RemovedSecurities)
101 SymbolData symbolData;
102 if (_symbolDataBySymbol.TryGetValue(security.Symbol, out symbolData))
104 _symbolDataBySymbol.Remove(security.Symbol);
105 symbolData.Dispose();
110 var addedSymbols =
new List<Symbol>();
111 foreach (var added
in changes.AddedSecurities)
113 if (!_symbolDataBySymbol.ContainsKey(added.Symbol))
115 var symbolData =
new SymbolData(algorithm, added.Symbol, _period, _resolution);
116 _symbolDataBySymbol[added.Symbol] = symbolData;
117 addedSymbols.Add(added.Symbol);
121 if (addedSymbols.Count > 0)
124 algorithm.History(addedSymbols, _period, _resolution)
127 SymbolData symbolData;
128 if (_symbolDataBySymbol.TryGetValue(data.Symbol, out symbolData))
130 symbolData.Update(data);
144 return State.TrippedHigh;
149 return State.TrippedLow;
152 if (previous == State.TrippedLow)
160 if (previous == State.TrippedHigh)
174 private class SymbolData : IDisposable
176 public State State {
get;
set; }
178 private Symbol _symbol {
get; }
179 private QCAlgorithm _algorithm;
182 public SymbolData(QCAlgorithm algorithm, Symbol symbol,
int period,
Resolution resolution)
184 _algorithm = algorithm;
186 State = State.Middle;
189 _consolidator = _algorithm.ResolveConsolidator(symbol, resolution);
190 algorithm.RegisterIndicator(symbol, RSI, _consolidator);
195 _consolidator.
Update(bar);
198 public void Dispose()
200 _algorithm.SubscriptionManager.RemoveConsolidator(_symbol, _consolidator);