Back

Rolling window - simple example

Hi!

I'm trying to use capabilities of Rolling window and got an error:

" System.ArgumentOutOfRangeException: Must be between 0 and 0
Parameter name: i
Actual value was 1. "

using System;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;

namespace QuantConnect.Algorithm.CSharp
{
public class BasicTemplateAlgorithm : QCAlgorithm
{
private Symbol _symbol = QuantConnect.Symbol.Create("BTCUSD", SecurityType.Crypto, Market.GDAX);
private RollingWindow<decimal> Close;
public override void Initialize()
{
SetStartDate(DateTime.Now.Date.AddDays(-30)); //Set Start Date
SetEndDate(DateTime.Now.Date.AddDays(-1)); //Set End Date
SetCash(10000); //Set Strategy Cash
AddSecurity(SecurityType.Equity, _symbol, Resolution.Hour);
Close = new RollingWindow<decimal>(2);
}

public void OnData(TradeBars data)
{
Close.Add(data[_symbol].Close);
var Close_priv = Close[1];
var Close_cur = Close[0];
var holding = Portfolio[_symbol];
var SellCondition = (Close_priv > Close_cur);
var BuyCondition = (Close_priv < Close_cur);
if (holding.Quantity == 0) //no position
{
if (BuyCondition)
{
SetHoldings(_symbol, 1.0);
}
else if (SellCondition)
{
SetHoldings(_symbol, -1.0);
}
}
else if (holding.Quantity > 0) //in long position
{
if (SellCondition)
{
Liquidate(_symbol);
SetHoldings(_symbol, -1.0);
}
}
else if (holding.Quantity < 0) //in short position
{
if (BuyCondition)
{
Liquidate(_symbol);
SetHoldings(_symbol, 1.0);
}
}
}
}
}
Update Backtest








if you add something the first time

and there is only one value at index 0 ...........

why are you asking the non existent value at index 1 in the rolling window which value this is?

:) i think that could be the problem..... :):) in this line.... just add an if statement checking the length greater ..... 0 :)

var Close_priv = Close[1];

check in this library provided by the quantconnect team
 

https://github.com/QuantConnect/Lean/tree/master/Algorithm.CSharp

 

the rolling window example

https://github.com/QuantConnect/Lean/blob/master/Algorithm.CSharp/RollingWindowAlgorithm.cs
0

Thanks for your reply.

I thought I do the same as an example (with _window):

// Creates a Rolling Window indicator to keep the 2 TradeBar
_window = new RollingWindow<TradeBar>(2); // For other security types, use QuoteBar
...
public override void OnData(Slice data)
{
// Add SPY TradeBar in rollling window
_window.Add(data["SPY"]);

// Wait for windows to be ready.
if (!_window.IsReady || !_smaWin.IsReady) return;

var currBar = _window[0]; // Current bar had index zero.
var pastBar = _window[1]; // Past bar has index one.

What is the difference?

0

this line:

if (!_window.IsReady || !_smaWin.IsReady) return;

which checks if the window is full filled with the amount of values you set (2).
then it is used after the check

see this ... ? the return statements is going to be executed when the window is not ready / not filled with the amount of data.

1

Thanks, Michael!

Now it works.

0


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