Hi Everybody!

I'm having a prety basic problem. I want to access a previous value in a RollingWindow (_signal). When I access the value _signal[0] there is no problem. When I try to access any other value _signal[i], i > 0, it doesn't work. Am I doing anything wrong when adding the values to the rolling window?

Thanks in advance!

Best,

Sergio.

namespace QuantConnect { /* * Filter and Trigger template */ public class FilterAndTriggerAlgorithm : QCAlgorithm { string _symbol = "AAPL"; bool _trade = true; private int _kPeriod = 14; private int _dPeriod = 3; RelativeStrengthIndex _rsi; ExponentialMovingAverage _ema, _ema_long, _ema_long_bottom, _ema_small, _ema_med; MovingAverageConvergenceDivergence _macd; Stochastic _sto; RollingWindow<IndicatorDataPoint> _window, _deriv; RollingWindow<decimal> _signal; decimal _price; decimal _short_price, _long_price; decimal strong_signal; //Initialize the data and resolution you require for your strategy: public override void Initialize() { //#5 - Stock Plotter with Trades Chart plotter = new Chart("Plotter"); plotter.AddSeries(new Series("Price", SeriesType.Line, index:0)); plotter.AddSeries(new Series("200MA", SeriesType.Line, index:0)); plotter.AddSeries(new Series("50MA", SeriesType.Line, index:0)); plotter.AddSeries(new Series("10MA", SeriesType.Line, index:0)); plotter.AddSeries(new Series("Buy", SeriesType.Scatter, index:0)); plotter.AddSeries(new Series("Sell", SeriesType.Scatter, index:0)); plotter.AddSeries(new Series("Buy - Liquidate", SeriesType.Scatter, index:0)); plotter.AddSeries(new Series("Sell - Liquidate", SeriesType.Scatter, index:0)); plotter.AddSeries(new Series("Buy - Stop Loss", SeriesType.Scatter, index:0)); plotter.AddSeries(new Series("Sell - Stop Loss", SeriesType.Scatter, index:0)); plotter.AddSeries(new Series("SSignal", SeriesType.Line, index:1)); AddChart(plotter); //Initialize SetStartDate(2019, 4, 8); SetEndDate(2019, 4, 12); SetCash(100000); _short_price = 0.0m; _long_price = 0.0m; //Add as many securities as you like. All the data will be passed into the event handler: AddSecurity(SecurityType.Equity, _symbol, Resolution.Minute); //Set up Indicators: _rsi = RSI(_symbol, 14, MovingAverageType.Simple, Resolution.Minute); _sto = STO(_symbol, 14, _kPeriod, _dPeriod); _ema_long = EMA(_symbol, 200, Resolution.Minute); _ema_med = EMA(_symbol, 50, Resolution.Minute); _ema_small = EMA(_symbol, 10, Resolution.Minute); _window = new RollingWindow<IndicatorDataPoint>(2); _deriv = new RollingWindow<IndicatorDataPoint>(2); _signal = new RollingWindow<decimal>(10); // Here is where the RollingWindow is updated with the latest RSI observation. _sto.Updated += (object sender, IndicatorDataPoint updated) => { _window.Add(updated); }; _ema_small.Updated += (object sender, IndicatorDataPoint updated) => { _deriv.Add(updated); }; Schedule.Event().EveryDay().At(15, 44).Run(() => { _trade = false; SetHoldings(_symbol, 0); Debug("End of day... "); }); Schedule.Event().EveryDay().At(9, 31).Run(() => { Debug("Ready to trade!"); _trade = true; }); } public void OnData(TradeBars data) { if (!_ema_long.IsReady) return; if (!_ema_med.IsReady) return; if (!_ema_small.IsReady) return; if (!_window.IsReady) return; if (!_rsi.IsReady) return; if (!_sto.IsReady) return; _price = data[_symbol].Close; if(_price > _ema_small) { strong_signal = 1; } else{ strong_signal = 0; } if(_price < _ema_small) { strong_signal = -1; } _signal.Add(strong_signal); Debug(Time + " " + _signal[0]); Plot("Plotter", "Price", _price); Plot("Plotter", "200MA", _ema_long); Plot("Plotter", "50MA", _ema_med); Plot("Plotter", "10MA", _ema_small); Plot("Plotter", "SSignal", strong_signal); } } }

 

Author