Array out of range ..... again

Here’s the code:

//+------------------------------------------------------------------+
//|                                                   xMeterDiff.mq4 |
//|                                                 Nondisclosure007 |
//|                                               http://no.link.yet |
//+------------------------------------------------------------------+
#property copyright "Nondisclosure007"
#property link      "http://no.link.yet"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   3
//--- plot Up
#property indicator_label1  "Up"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- plot Down
#property indicator_label2  "Down"
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2
//--- plot Change
#property indicator_label3  "Change"
#property indicator_type3   DRAW_HISTOGRAM
#property indicator_style3  STYLE_SOLID
#property indicator_color3  clrDarkSlateGray
#property indicator_width3  2

//--- indicator buffers
double         UpBuffer[];
double         DownBuffer[];
double         ChangeBuffer[];
double         DataBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(3);
   SetIndexBuffer(0,UpBuffer);
   SetIndexLabel(0,"Up-Xmeter");
   SetIndexBuffer(1,DownBuffer);
   SetIndexLabel(1,"Down-Xmeter");
   SetIndexBuffer(2,ChangeBuffer);
   SetIndexLabel(2,"Change-Xmeter");
   SetIndexBuffer(3,DataBuffer);
   SetIndexLabel(3,NULL);
   SetIndexStyle(3,DRAW_NONE); 
   int iDigits=(int)MarketInfo(Symbol(),MODE_DIGITS); 
   IndicatorDigits(iDigits);
   int found=-1;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   int limit=rates_total-prev_calculated;
   int i=0;
   if(prev_calculated>0)
     {
      limit++; 
     }
   for(i=0;i<limit;i++)
     {
      DataBuffer[i]=High[i]-Low[i];
     }
   double Diff=0.0;
   for(i=0;i<limit;i++)
     {
      Diff=DataBuffer[i]-DataBuffer[i+1];
      if(Diff>DataBuffer[i+1])
        {
         UpBuffer[i]=Diff;
         DownBuffer[i]=0.0;
         ChangeBuffer[i]=0.0;
        }
      if(Diff<DataBuffer[i+1])
        {
         DownBuffer[i]=Diff;
         UpBuffer[i]=0.0;
         ChangeBuffer[i]=0.0;
        }
      if(Diff==DataBuffer[+1])
        {
         ChangeBuffer[i]=Diff;
         DownBuffer[i]=0.0;
         UpBuffer[i]=0.0;
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

I get the array out of range error on this line:

Diff=DataBuffer[i]-DataBuffer[i+1];
Now, I think I now why (At the end of the loop, there is no DataBuffer[i+1]). I’m just not sure how to avoid it.

This indicator is finding out what the difference is of a candle’s range - the previous candle’s range. Not all that useful, but I’ve got other things in mind. If this can be solved, then the rest will be easy.

Thanks.

Typical me. I have a “temporary” fix. Would like to know if there’s an easier way. Here’s what’s added:

Added the following global variable:

bool IsInit;

Then set it in OnInit():

   IsInit=true;

Then I added this in the second for loop right at the beginning of it:

      if(IsInit && i>Bars-100)
        {
         IsInit=false;
         break;
        }

line 74

//int limit=rates_total-prev_calculated;
int limit=MathMin(rates_total-1, rates_total-prev_calculated);

Because, on first calculation

preve_calculated = 0

Therefore,

rates_total - preve_calculated = rates_total

DataBuffer[i+1] = DataBuffer[rates_total]

Also you should correct following two lines.

line 11

//#property indicator_buffers 4
#property indicator_buffers 3

line 43

//IndicatorBuffers(3);
IndicatorBuffers(4);