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?
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
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!
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 }; };

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
This piece of code is giving Array out of range error only when backtesting.
When EA is running. No errors whatsoever!