trophy

Long Only, ETF Alpha Streams Competition with $27,500 Prize Pool

Back

Not being able to access older values of RollingWindow

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);
}

}
}

 

Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Sergio,

The reason is, RollingWindow requires time to get ready just like Indicators. In your algorithm, _signal does not get updated until all indicators are ready (it returns before reaching to the statements of updating RollingWindow). Therefore it would throw error when the algorithm trying to print the _signal[1] when only signal[0] is updated.

There are many ways to fix it, depending on your strategy. A simple one would be put a IsReady check before accessing values in RollingWindow.

if (_signal.IsReady){
    Debug(Time + " " + _signal[9]);
}


Hope it helps!

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed