Array out of range error

 

Hello! 

I have the following indicator:


int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
      int counted_bars = IndicatorCounted(), limit;
      
      if(counted_bars > 0)
         counted_bars--;
      
      limit = Bars - counted_bars;
      
      if(limit > max_bars)
         limit = max_bars;
         
      for(int i = 
1; i < limit; i++)
      {  
         if(indicator(i) == 0)
            buyBuffer[i] = Low[i] - 20 * Point;
         else
            buyBuffer[i] = EMPTY_VALUE;
            
         if(indicator(i) == 1)
            sellBuffer[i] = High[i] + 20 * Point;
         else
            sellBuffer[i] = EMPTY_VALUE;
      }
   
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

//+--------------------------------------------+
//| Definir Compra (0),  Venta (1) o NC (2) |
//+--------------------------------------------+ 
int indicator(int bar)
{
//---Inicio---//

/*
Code...
*/
   
   double oscilator = (Close[bar] ...Code...;
   This line -> double oscilator_p = (Close[bar + 1] ...Code...;
   

/*
Code...
*/

//---Fin---//

I want it to be based just upon the previous candle to avoid repainting, therefore I started the for loop at 1 instead of 0. But when I add it to the strategy tester to check it, I get the Array out of Range error on the line marked as "This line ->" in the Close access of the previous bar (bar + 1, or the value of i in the for loop + 1 as that's the input for the signals function), I tried starting the for loop at 0 but I keep getting the same issue... could someone help me out to figure what's wrong?

 
     for(int i = 1; i < limit; i++){// [1 … Bars-1]
         if(indicator(i) == 0)      // [1 … Bars-1]
    ⋮
  }
int indicator(int bar){             // [1 … Bars-1]
   This line -> double oscilator_p = (Close[bar + 1] ...Code...; // [2 … Bars] <== Close[Bars] does not exist

Do your lookbacks correctly.
 
William Roeder:

Do your lookbacks correctly.
Can you elaborate on why Close[bar] does not exist? (note that bar is the function input, not Bars containing the amount of available bars on the chart) 

EDIT

The issue only happens on the tester, when I load it on the chart, it has no issues, at least that I notice, not sure if my error is on the OnCalculate() function? 
 
Fernando Jose Velasco Borea: can you elaborate on why Close[bar] does not exist? (note that bar is the function input, not Bars containing the amount of available bars on the chart)
Close[bar] exists. Your line reads Close[bar+1] which fails when bar equals Bars-1.
 
William Roeder:
Close[bar] exists. Your line reads Close[bar+1] which fails when bar equals Bars-1.
Oh!! So the fail is for example when Bars is 999 and it tries to access index 1000? If so, the solution would be in the for loop to do i < limit - 1?
 
Fernando Jose Velasco Borea:
Oh!! So the fail is for example when Bars is 999 and it tries to access index 1000? If so, the solution would be in the for loop to do i < limit - 1?

When Bars is 999 there is no index 999 either

The index is from [0] to [998]

 
Keith Watford:

When Bars is 999 there is no index 999 either

The index is from [0] to [998]

You're right, let me rephrase, so the issue is for example when Bars equals to 1000 and the loop tries to access bar with index 1000, which doesn't exist as the last index is 999?
 
Fernando Jose Velasco Borea:
You're right, let me rephrase, so the issue is for example when Bars equals to 1000 and the loop tries to access bar with index 1000, which doesn't exist as the last index is 999?

Correct.

Reason: