I want to keep an eye on the candle, the number of which I indicate in the input "CountDirCandle". When I choose to trade in real time, I don't have any errors. But when I use the Strategy Analyzer and the parameter CountDirCandle > 1 I get an error "Strategy 'NewCDC': Error on calling 'OnBarUpdate' method on bar 1: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."
What check did I miss or what am I doing wrong? Here is my code below:
int CountDirCandle = 2;
if (CurrentBars[0] < 1)
return;
if((ToTime(Time[0]) >= TradingStart && ToTime(Time[0]) < TradingEnd ))
{
// Set 1
if ((Buy == true)
&& (Position.MarketPosition == MarketPosition.Flat)
&& (Open[CountDirCandle] < Close[CountDirCandle]))
{
EnterLong(Convert.ToInt32(Lots), "Long");
}
// Set 2
if ((Sell == true)
&& (Position.MarketPosition == MarketPosition.Flat)
&& (Open[CountDirCandle] > Close[CountDirCandle]))
{
EnterShort(Convert.ToInt32(Lots), "Short");
}
}

Comment