2-dimensional array out of range error

 

Hi, I'm coding an EA and I have this problem with a 2-dimensional array: when I backtest the EA the "array out of range" error pops up, can anyone help me please?

input int LookBackPeriod=10;    //Look back period
input int NumStds=5;            //Number of half stds

int LookBackPeriodVar=LookBackPeriod;
int NumStdsVar=NumStds;

double PriceSTDS[100][100];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   MqlRates PriceInformation[];
   ArraySetAsSeries(PriceInformation,true);
   int Data=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),PriceInformation);
   double std=0;
   
   for (int i=0; i<LookBackPeriodVar; i++)
     {
      std=(PriceInformation[i+1].high-PriceInformation[i+1].low)/2;
      PriceSTDS[i][0]=std+PriceInformation[i+1].low;
      for (int i2=0; i2<NumStdsVar; i++)
        {
         PriceSTDS[i][i2+1]=PriceInformation[i+1].high+std*i2;
        }
      for (int i2=NumStdsVar; i2<2*NumStdsVar; i++)
        {
         PriceSTDS[i][i2+1]=PriceInformation[i+1].low-std*(NumStdsVar-i2);
        }
     }
   
   return(INIT_SUCCEEDED);
  }
 
Yes, you are relying on data that might not be ready by the terminal in OnInit.

Try checking the value of Data.
 
Dominik Egert:
Yes, you are relying on data that might not be ready by the terminal in OnInit.

Try checking the value of Data.

I have the same problem also If I put that code in the OnTick() function

 
Giorgio: I have the same problem also If I put that code in the OnTick() function

You are ignoring @Dominik Egert's other warning. You are not checking the returned value in the "Data" variable. You are just assuming that enough data was returned by CopyRates but not checking.

Check that "Data" is actually greater or equal than "LookBackPeriodVar" plus one, since you are index at [i+1].

 
Fernando Carreiro:

You are ignoring @Dominik Egert's other warning. You are not checking the returned value in the "Data" variable. You are just assuming that enough data was returned by CopyRates but not checking.

Check that "Data" is actually greater or equal than "LookBackPeriodVar" plus one, since you are index at [i+1].

Ok, done that, but still doesn't work (init keeps failing because Data Is not >=LookBackPeriodVar+1), how can I resolve It?

 
Fernando Carreiro:

You are ignoring @Dominik Egert's other warning. You are not checking the returned value in the "Data" variable. You are just assuming that enough data was returned by CopyRates but not checking.

Check that "Data" is actually greater or equal than "LookBackPeriodVar" plus one, since you are index at [i+1].

The array out of range Is PriceSTDS (the first one inside the 'for')
 
Giorgio: Ok, done that, but still doesn't work (init keeps failing because Data Is not >=LookBackPeriodVar+1), how can I resolve It?

You also need to check "Bars(Symbol(),Period()" and make sure there are in fact enough bars. Also make sure you are doing this only in the OnTick(). Do not use these functions in OnInit().

 
Giorgio: The array out of range Is PriceSTDS (the first one inside the 'for')

You are declaring the PriceSTDS with a hardcoded size instead dynamically assigning its size, so make sure that the calculated indexes based on the variables LookBackPeriod, NumStds will be within range.

Remember that you are multiplying NumStdsVar by two and adding one to the index. Arrays start at 0, not 1.

If an array has size "n", then its indexing is 0,1,2 ... n - 1

If an array has size "10", then its indexing is 0,1,2 ... 9

 
Giorgio:
The array out of range Is PriceSTDS (the first one inside the 'for')

Problem solved, thanks guys :)

Reason: