Example #1
0
        /// <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();
                }
            }
        }