Array out of range Heiken ashi indicator

 

I am getting  this error on the H ashi indicator:

array out of range in 'heiken_ashi.mq5'      

on this  line:

double haOpen=(ExtOBuffer[i-1]+ExtCBuffer[i-1])/2;

The whole code is shown below:


How could this error be solved?

//+------------------------------------------------------------------+
//|                                                  Heiken_Ashi.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   1
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  DodgerBlue, Red
#property indicator_label1  "Heiken Ashi Open;Heiken Ashi High;Heiken Ashi Low;Heiken Ashi Close"
//--- indicator buffers
double ExtOBuffer[];
double ExtHBuffer[];
double ExtLBuffer[];
double ExtCBuffer[];
double ExtColorBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtOBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtHBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ExtLBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,ExtCBuffer,INDICATOR_DATA);
   SetIndexBuffer(4,ExtColorBuffer,INDICATOR_COLOR_INDEX);
//---
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets first bar from what index will be drawn
   IndicatorSetString(INDICATOR_SHORTNAME,"Heiken Ashi");
//--- sets drawing line empty value
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//--- initialization done
  }
//+------------------------------------------------------------------+
//| Heiken Ashi                                                      |
//+------------------------------------------------------------------+
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 &TickVolume[],
                const long &Volume[],
                const int &Spread[])
  {
   int i,limit;
//--- preliminary calculations
   if(prev_calculated==0)
     {
      //--- set first candle
      ExtLBuffer[0]=Low[0];
      ExtHBuffer[0]=High[0];
      ExtOBuffer[0]=Open[0];
      ExtCBuffer[0]=Close[0];
      limit=1;
     }
   else limit=prev_calculated-1;

//--- the main loop of calculations
   for(i=limit;i<rates_total && !IsStopped();i++)
     {
      double haOpen=(ExtOBuffer[i-1]+ExtCBuffer[i-1])/2;
      double haClose=(Open[i]+High[i]+Low[i]+Close[i])/4;
      double haHigh=MathMax(High[i],MathMax(haOpen,haClose));
      double haLow=MathMin(Low[i],MathMin(haOpen,haClose));

      ExtLBuffer[i]=haLow;
      ExtHBuffer[i]=haHigh;
      ExtOBuffer[i]=haOpen;
      ExtCBuffer[i]=haClose;

      //--- set candle color
      if(haOpen<haClose) ExtColorBuffer[i]=0.0; // set color DodgerBlue
      else               ExtColorBuffer[i]=1.0; // set color Red
     }
//--- done
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Peter Kaiza: double haOpen=(ExtOBuffer[i-1]+ExtCBuffer[i-1])/2;

Do your lookbacks correctly #9#14 & #19. (2016)

 
William Roeder #:

Do your lookbacks correctly #9#14 & #19. (2016)

Thanks, could you  show me  how exactly do you amend the code?


Peter.

 
When you have an index i-1 and at some point i=0,
then i-1= -1

So at the start of the indicator when prev_calculated is zero,  limit is equal to  prev_calculated minus one, then if i is equal to limit, it is -1 which it can't be.

By declaring and initialising Indicator buffers you did provide a buffer for Heiken Ashi OHLC, so far so good. But your loop tries to access them at index -1 which is out of the range of your buffers.
The buffer can only work with 0 to rates_total-1.
 
Tobias Johannes Zimmer #:
When you have an index i-1 and at some point i=0,
then i-1= -1

So at the start of the indicator when prev_calculated is zero,  limit is equal to  prev_calculated minus one, then if i is equal to limit, it is -1 which it can't be.

By declaring and initialising Indicator buffers you did provide a buffer for Heiken Ashi OHLC, so far so good. But your loop tries to access them at index -1 which is out of the range of your buffers.
The buffer can only work with 0 to rates_total-1.

ok thanks so should I put a condition to calculate  only  if i>1?

 
Peter Kaiza #:

ok thanks so should I put a condition to calculate  only  if i>1?

Oh no, I haven't looked right, in your code says:

if(prev_calculated==0){
...

limit=1;   // So you have already done that, I guess... because i starts at 1 according to that predefinition.
}

else limit=prev_calculated-1  // That is, prev_calculated is getting big during the first OnCalculate() event (or tick let's say... 
                              // so i will not be -1 here)

Or have you changed the code, because  now it seems right, also in the debugger I don't get out of range, so... I am not sure if it shows what a HA is supposed to but it works.

Do you still get the error?
 
Peter Kaiza #: Thanks, could you  show me  how exactly do you amend the code?

William provided you with 3 links in his post. You need to click on them, and read their contents.

They provide source code samples and explanations. Please read them carefully and adapt them to your code.

 


I have added 
i >= 1

on the code below and now cant see the error. The EA  brings this error  only when I attach it on the chart. The indicator is pulled to the EA through an Icustom function call


for(i=limit;i<rates_total &&i >= 1&& !IsStopped();i++)
 
Brilliant 😂🤌
Reason: