/// <summary> /// Cleans out old security data and initializes the RSI for any newly added securities. /// This functional also seeds any new indicators using a history request. /// </summary> /// <param name="algorithm">The algorithm instance that experienced the change in securities</param> /// <param name="changes">The security additions and removals from the algorithm</param> public override void OnSecuritiesChanged(QCAlgorithmFramework algorithm, SecurityChanges changes) { // clean up data for removed securities if (changes.RemovedSecurities.Count > 0) { var removed = changes.RemovedSecurities.ToHashSet(x => x.Symbol); foreach (var subscription in algorithm.SubscriptionManager.Subscriptions) { if (removed.Contains(subscription.Symbol)) { _symbolDataBySymbol.Remove(subscription.Symbol); subscription.Consolidators.Clear(); } } } // initialize data for added securities var addedSymbols = new List <Symbol>(); foreach (var added in changes.AddedSecurities) { if (!_symbolDataBySymbol.ContainsKey(added.Symbol)) { if (!added.Symbol.HasUnderlying) { //algorithm.Consolidate(added.Symbol, _resolution, HandleAction); //var consolidator = algorithm.ResolveConsolidator(added.Symbol, _resolution); //consolidator.DataConsolidated += Consolidator_DataConsolidated; var symbolData = new SymbolData(added.Symbol, _period); _symbolDataBySymbol[added.Symbol] = symbolData; addedSymbols.Add(symbolData.Symbol); var chart = new Chart(added.Symbol.Value + " - Options Contango"); chart.AddSeries(symbolData.FrontIVSeries); chart.AddSeries(symbolData.BackIVSeries); chart.AddSeries(symbolData.STDSeries); algorithm.AddChart(chart); } } } if (addedSymbols.Count > 0) { // warmup our indicators by pushing history through the consolidators /* * algorithm.History(addedSymbols, _resolution.Multiply(_period)) * .PushThrough(data => * { * SymbolData symbolData; * if (_symbolDataBySymbol.TryGetValue(data.Symbol, out symbolData)) * { * symbolData.RSI.Update(data.EndTime, data.Value); * } * }); */ } }
/// <summary> /// Cleans out old security data and initializes the RSI for any newly added securities. /// This functional also seeds any new indicators using a history request. /// </summary> /// <param name="algorithm">The algorithm instance that experienced the change in securities</param> /// <param name="changes">The security additions and removals from the algorithm</param> public override void OnSecuritiesChanged(QCAlgorithmFramework algorithm, SecurityChanges changes) { // clean up data for removed securities if (changes.RemovedSecurities.Count > 0) { var removed = changes.RemovedSecurities.ToHashSet(x => x.Symbol); foreach (var subscription in algorithm.SubscriptionManager.Subscriptions) { if (removed.Contains(subscription.Symbol)) { _symbolDataBySymbol.Remove(subscription.Symbol); subscription.Consolidators.Clear(); } } } // initialize data for added securities var addedSymbols = new List <Symbol>(); foreach (var added in changes.AddedSecurities) { if (!_symbolDataBySymbol.ContainsKey(added.Symbol)) { if (!added.Symbol.HasUnderlying) //ignore derivatives { var symbolData = new SymbolData(algorithm, added.Symbol, _period, this.IsPercent); _symbolDataBySymbol[added.Symbol] = symbolData; addedSymbols.Add(symbolData.Symbol); var chart = new Chart(added.Symbol.Value + " - " + typeof(T).Name); chart.AddSeries(symbolData.IndicatorSeries); chart.AddSeries(symbolData.IndicatorSeriesSTD); algorithm.AddChart(chart); } } } if (addedSymbols.Count > 0) { // warmup our indicators by pushing history through the consolidators algorithm.History(addedSymbols, _resolution.Multiply(_period)) .PushThrough(data => { SymbolData symbolData; if (_symbolDataBySymbol.TryGetValue(data.Symbol, out symbolData)) { symbolData.Update((TradeBar)data); } }); } }
/// <summary> /// Event fired each time the we add/remove securities from the data feed /// </summary> /// <param name="algorithm">The algorithm instance that experienced the change in securities</param> /// <param name="changes">The security additions and removals from the algorithm</param> public override void OnSecuritiesChanged(QCAlgorithmFramework algorithm, SecurityChanges changes) { foreach (var added in changes.AddedSecurities) { if (added.Symbol.HasUnderlying) { continue; } SymbolData symbolData; if (!_symbolDataBySymbol.TryGetValue(added.Symbol, out symbolData)) { // create fast/slow EMAs var bb = algorithm.BB(added.Symbol, _period, _threshold); symbolData = _symbolDataBySymbol[added.Symbol] = new SymbolData { Security = added, BB = bb, LastDirection = InsightDirection.Flat, Price = new Series("Price", SeriesType.Line), Upper = new Series("Upper", SeriesType.Line), Middle = new Series("Middle", SeriesType.Line), Lower = new Series("Lower", SeriesType.Line), }; var chart = new Chart(added.Symbol.Value + " - Bollinger Bands"); chart.AddSeries(symbolData.Price); chart.AddSeries(symbolData.Upper); chart.AddSeries(symbolData.Middle); chart.AddSeries(symbolData.Lower); algorithm.AddChart(chart); } else { // a security that was already initialized was re-added, reset the indicators symbolData.BB.Reset(); } } }