Array out of range for mrate[i].close when backtesting only!

 

This piece of code is giving Array out of range error only when backtesting.

When EA is running. No errors whatsoever!

CopyRates(symbol, PERIOD_D1, 0, 30, mrate);

for (int i = 0; i < 30; i++){

      ArrayCount = VolumeArray.Size();

         

      if ((int)mrate[i].close % 10 != 0) ///////////////////////////////////////////////////// this line gives the out of array error

         VolumeArray[ArrayCount -1] = ((int)mrate[i].close - ((int)mrate[i].close % 10));

      else

         VolumeArray[ArrayCount -1] = (int)mrate[i].close;

      ArrayResize(VolumeArray, ArrayCount + 1);

         

}
The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
 

You are not checking the return value of the CopyRates() function. Don't just assume that it will return 30 elements. It may be less or fail completely. You should also be checking the size of the mrate[] and not hard coding the loop.

if( CopyRates( symbol, PERIOD_D1, 0, 30, mrate ) ) {
   int mrate_count = ArraySize( mrate );
   for( int i = 0; i < mrate_count; i++ ){
      // rest of code
   };
};

Also, why are you typecasting the close price into an integer?

 
Hesham Ahmed Kamal Barakat:

This piece of code is giving Array out of range error only when backtesting.

When EA is running. No errors whatsoever!

int size = CopyRates(symbol, PERIOD_D1, 0, 30, mrate);

if (size >= 30) {

   for (int i = 0; i < 30; i++){

       ArrayCount = VolumeArray.Size();

         

       if ((int)mrate[i].close % 10 != 0) ///////////////////////////////////////////////////// this line gives the out of array error
  
          VolumeArray[ArrayCount -1] = ((int)mrate[i].close - ((int)mrate[i].close % 10));
 
       else

          VolumeArray[ArrayCount -1] = (int)mrate[i].close;

       ArrayResize(VolumeArray, ArrayCount + 1);

        
   }

}


Always check the SIZE of the copied elements

 
Fernando Carreiro #:

You are not checking the return value of the CopyRates() function. Don't just assume that it will return 30 elements. It may be less or fail completely. You should also be checking the size of the mrate[] and not hard coding the loop.

Also, why are you typecasting the close price into an integer?


I agree! typcasting a double PRICE to Integer, will destroy the price value. Always!

 
Fernando Carreiro #: You are not checking the return value of the CopyRates() function. Don't just assume that it will return 30 elements. It may be less or fail completely. You should also be checking the size of the mrate[] and not hard coding the loop. Also, why are you typecasting the close price into an integer?

There is a bug in my original post. Here is the correction ...

if( CopyRates( symbol, PERIOD_D1, 0, 30, mrate ) > 0 ) {
   int mrate_count = ArraySize( mrate );
   for( int i = 0; i < mrate_count; i++ ){
      // rest of code
   };
};