Array out of range error

 
int arrayLengthIndex = 28;
   string currencyArray[28] = {"USDCHF", "GBPUSD", "EURUSD", "USDJPY", "USDCAD", "AUDUSD", "EURGBP", "EURAUD", "EURCHF", 
                              "EURJPY", "GBPCHF", "CADJPY", "GBPJPY", "AUDNZD", "AUDCAD", "AUDCHF", "AUDJPY", "CHFJPY", "EURNZD", "EURCAD", 
                              "CADCHF", "NZDJPY", "NZDUSD", "GBPAUD", "GBPCAD", "GBPNZD", "NZDCAD", "NZDCHF" };
 
 
   for (int k =0 ; k <arrayLengthIndex; k++) {
      
   double BuyStop  = High[iHighest(currencyArray[k],PERIOD_M30,MODE_HIGH,dist,0)]; 
   double SellStop  =Low[iLowest(currencyArray[k],PERIOD_M30,MODE_LOW,dist,0)];

Hi All

I am getting an array out of range error when i try to backtest and its pointing to the High[] array , what is wrong with my coding?


Thank you 

 

Simon Mungwira:

what is wrong with my coding?

I'd say you ought to trap for errors when getting iHighest().

Per the documentation:

The index of the highest value found on the corresponding chart (shift relative to the current bar) or -1 in case of an error.
For error details, call the GetLastError() function.

 
Anthony Garot:

I'd say you ought to trap for errors when getting iHighest().

Per the documentation:

The index of the highest value found on the corresponding chart (shift relative to the current bar) or -1 in case of an error.
For error details, call the GetLastError() function.

In addition to this, OP:


You shouldn't be defining the array size when you use an initialization list. Instead only use the initialization to automatically resize the array, that way you can add/remove items from the array and you don't have to worry about creating accidental bugs. 

   string currencyArray[] = {
      "USDCHF", "GBPUSD", "EURUSD", "USDJPY", "USDCAD", "AUDUSD", 
      "EURGBP", "EURAUD", "EURCHF", "EURJPY", "GBPCHF", "CADJPY", 
      "GBPJPY", "AUDNZD", "AUDCAD", "AUDCHF", "AUDJPY", "CHFJPY", 
      "EURNZD", "EURCAD", "CADCHF", "NZDJPY", "NZDUSD", "GBPAUD", 
      "GBPCAD", "GBPNZD", "NZDCAD", "NZDCHF" 
   };
   int arrayLengthIndex = ArraySize(currencyArray);

Also, you will have more hidden errors because you are using the wrong method for calling data from external pairs. You need to be using iHigh instead of High. 

   double buyStop = iHigh(currencyArray[k], PERIOD_M30,
      iHighest(currencyArray[k], PERIOD_M30, MODE_HIGH, dist, 0)
   );
   if(!buyStop)
      //HANDLE ERRORS!
 
nicholi shen:

In addition to this, OP:


You shouldn't be defining the array size when you use an initialization list. Instead only use the initialization to automatically resize the array, that way you can add/remove items from the array and you don't have to worry about creating accidental bugs. 

Also, you will have more hidden errors because you are using the wrong method for calling data from external pairs. You need to be using iHigh instead of High. 

Thanks @ nicholi shen and Anthony Garot the error is gone

Reason: