Download MetaTrader 5

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

To add comments, please log in or register
Anuwat Tanasittipan
133
Anuwat Tanasittipan 2014.12.18 11:59 

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.

Roberto Jacobs
47172
Roberto Jacobs 2014.12.19 04:36  

Try to change:

else first=prev_calculated-1; // start index for the new bars -> to else first=HighPeriod-1;
Anuwat Tanasittipan
133
Anuwat Tanasittipan 2014.12.23 04:35  
3rjfx:

Try to change:

Thank you, 3rjfx.

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

The Xlearner
222
The Xlearner 2014.12.23 16:00  
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...
Anuwat Tanasittipan
133
Anuwat Tanasittipan 2014.12.26 05:20  
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. 

Roberto Jacobs
47172
Roberto Jacobs 2015.01.03 19:50  
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)];
Anuwat Tanasittipan
133
Anuwat Tanasittipan 2015.01.07 10:40  
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.

To add comments, please log in or register