Array out of range (Only when testing but not on chart)

 

Hi,

 I'm trying to test an EA, on Metatrader5, which includes custom indicator "Highest High", coded as below, on stock data.

The indicator determines the highest of high prices of N recent bars (input HighPeriod in the code).

//+------------------------------------------------------------------+
//|                                                  HighestHigh.mq5 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  Lime

//--- input parameters
input int      HighPeriod = 105;

double      HighestHighBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, HighestHighBuffer, INDICATOR_DATA);
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, HighPeriod);
//---
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
  if (rates_total < HighPeriod)
  return(0);
  
  int first, bar;
  
  if(prev_calculated==0) // check for the first start of the indicator
      first=HighPeriod; // start index for all the bars
   else first=prev_calculated-1; // start index for the new bars
  
  for (bar = first;bar < rates_total;bar++) 
  {
   HighestHighBuffer[bar] = 0;
   double rhigh= high[ArrayMaximum(high,bar-HighPeriod,HighPeriod)];
   HighestHighBuffer[bar] = rhigh;
  }
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }

 

The Highest High indicator works fine without error when using on Chart, whatever the period (HighPeriod) is.

But when testing the EA in Strategy Tester, the error occurs whenever the period is set to be a specific number or above, say 101 bars.

 

 

I notice that the number, which in this case is 101, is the same number as "bars of beginning data" appearing in Journal when the test is started, as in the image below.

 

The number varies upon the stock data. When the data "contains 217 bars of beginning data", the indicator starts to get array out of range error when the HighPeriod input is 217 or above.

As the indicator works normally on chart, I assume the problem is with the Strategy Tester.

 

Do you have any idea about how to make the EA work for any value HighPeriod?

What determines the number of "contains xxx bars of beginning data" ?


Thanks in advance for your help.

 

Try to change:

else first=prev_calculated-1; // start index for the new bars -> to else first=HighPeriod-1;
 
3rjfx:

Try to change:

Thank you, 3rjfx.

I tried that but still got the same result. Still working on it. 

 
anuwat:

Thank you, 3rjfx.

I tried that but still got the same result. Still working on it. 

Watch this video  arrayOutOfRange
arrayOutOfRange
arrayOutOfRange
  • www.youtube.com
http://patreon.com/jimdandy1958.com http://jimdandymql4courses.com http://jimdandyforex.com This video will help you (hopefully) to fix your code to eliminat...
 
yassin.mokni:
Watch this video  arrayOutOfRange

Thanks for your reply yassin.

I have watched the video and try to edit my EA. The array works well when running the auto trading.

 

But the same problem still occurs whenever the EA is tested on Strategy Tester, just as I described.

Would appreciate any further suggestion from you guys.

Thank you much. 

 
anuwat:

Thanks for your reply yassin.

I have watched the video and try to edit my EA. The array works well when running the auto trading.

 

But the same problem still occurs whenever the EA is tested on Strategy Tester, just as I described.

Would appreciate any further suggestion from you guys.

Thank you much. 

Hi anuwat,

I saw the problem is on this code

double rhigh= high[ArrayMaximum(high,bar-HighPeriod,HighPeriod)];

Try to change with this

double rhigh= high[ArrayMaximum(high,MathAbs(bar-HighPeriod),HighPeriod)];
 
3rjfx:

Hi anuwat,

I saw the problem is on this code

Try to change with this

This seems to solve my problems. 

Thank you a lot! 3rjfx.