how can i fix array out of range error?

 

i have a angle checking mql4 source but it have array out of range error.

i can't figure out what is problem.

i can guess problem is come from loop but can't fix error.

i did several modification like i+1 or so where possibly can fix error but no lucky.

i guessing error come around here

MALast= varMA[i+nb_0];

anyone help me much appreciate!!


#property indicator_chart_window
#property indicator_color1 CLR_NONE
#property indicator_buffers 3
#property indicator_color2  CLR_NONE
 
extern int             ma_per = 10;
extern int             ma_met = 3;
extern color          col_ang = clrYellow;
extern int           ExtDepth = 21;
extern int                ext = 0;
extern int           Complect = 0;
double varMA[], value[];

//-------------------------------------------------------------------------
int init()
  {
   IndicatorBuffers(3);   
   IndicatorDigits(Digits+1);   
   SetIndexBuffer(0,varMA);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1, value);

   return(0);
  }

//-------------------------------------------------------------------------
int deinit()
  {
   ObjectDelete("AngleA "+Complect);
   ObjectDelete("AngleText "+Complect);
   Comment("");
   return(0);
  }
  
//-------------------------------------------------------------------------
int start()
  {
   int i, limit, counted_bars=IndicatorCounted();
   limit = Bars-counted_bars-1; 
      if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   double MALast, MACurr;

   for(i=limit; i >=0; i--)
   {
      varMA[i] = iMA(NULL,0,ma_per,0,ma_met,PRICE_CLOSE,i);
      int nb_0 = GetZZbar(ext);
      
      MALast= varMA[i+nb_0];
      MACurr= varMA[i+1];
 
      if (ObjectFind("AngleA "+Complect)<0)
      {
        ObjectCreate("AngleA "+Complect,OBJ_TRENDBYANGLE,0,Time[i+nb_0],MALast,Time[i+1],MACurr);
        ObjectSet("AngleA "+Complect,OBJPROP_STYLE,2);
      }
      
      if (ObjectFind("AngleText "+Complect)==-1)
          ObjectCreate("AngleText "+Complect,OBJ_TEXT,0,0,0);
      ObjectSet("AngleA "+Complect,OBJPROP_TIME2,Time[i+1]);
      ObjectSet("AngleA "+Complect,OBJPROP_PRICE2,MACurr);
      ObjectSet("AngleA "+Complect,OBJPROP_TIME1,Time[i+nb_0]);
      ObjectSet("AngleA "+Complect,OBJPROP_PRICE1,MALast);
      ObjectSet("AngleA "+Complect,OBJPROP_COLOR,col_ang);
      value[i] = (double)ObjectGet("AngleA "+Complect,OBJPROP_ANGLE);
      if(value[i]>90)
        value[i] = value[i]-360;
      value[i+1] = value[i];
      ObjectSetText("AngleText "+Complect,DoubleToStr(value[i],2),8,"Verdana",col_ang);
      ObjectMove("AngleText "+Complect,0,Time[0]+8*Period()*60,ObjectGetValueByShift("AngleA "+Complect,i)); 
   }
   Comment(value[0]);
   
   return(0);
  }

//-------------------------------------------------------------------------
int GetZZbar(int ne=0)
 {
  double zz;
  int    j, k=iBars(NULL, 0), ke=0;
  
  for (j =1; j <k; j++)
   {
    zz=iCustom(NULL, 0, "ZigZag", ExtDepth,5,3, 0,j);
    if (zz !=0)
     {
      ke++;
      if (ke > ne) 
        return(j);
     }
   }
  
  
  return(-1);
 }
 
//-------------------------------------------------------------------------
Timeline for how can i fix array error in mql4 source?
Timeline for how can i fix array error in mql4 source?
  • stackoverflow.com
Stack Overflow | The World’s Largest Online Community for Developers
 
  1. Don't post pictures of code, they are too hard to read.

  2.    for(i=limit; i >=0; i--){
          ⋮
          MALast= varMA[i+nb_0];
    When IndicatorCounted is zero, limit is Bars-1. I plus anything is beyond limit and array exceeded.

  3. Fill your array and then Do your lookbacks correctly.

  4. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 2016.05.11

 
William Roeder:
  1. Don't post pictures of code, they are too hard to read.

  2. When IndicatorCounted is zero, limit is Bars-1. I plus anything is beyond limit and array exceeded.

  3. Fill your array and then Do your lookbacks correctly.

  4. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 2016.05.11

i changed this 


 limit = Bars-counted_bars-1; to 


 limit = Bars-counted_bars; 
but still array error. what is wrong? thanks 
 
james272:

i changed this 


#property strict
#property indicator_chart_window
#property indicator_buffers 2
input int             ma_per = 10;
input int             ma_met = 3;
input color          col_ang = clrYellow;
input int           ExtDepth = 21;
input int                ext = 0;
input int           Complect = 0;
double varMA[], value[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int BarReduction=0;
int OnInit()
  {
//--- indicator buffers mapping
  SetIndexBuffer(0,varMA);
  SetIndexStyle(0,DRAW_NONE,EMPTY,EMPTY,clrNONE);
  SetIndexBuffer(1,value);
  SetIndexStyle(1,DRAW_NONE,EMPTY,EMPTY,clrNONE); 
  BarReduction=(int)MathMax(ExtDepth,ma_per)+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-BarReduction;
   double MALast, MACurr;

   for(int i=limit; i>=0; i--)
   {
   varMA[i]=iMA(NULL,0,ma_per,0,ma_met,PRICE_CLOSE,i);
   int nb_0=GetZZbar(ext);
   //if zzbar exists   
   if(nb_0>-1)
   {
      MALast= varMA[i+nb_0];
      MACurr= varMA[i+1];
 
      if(ObjectFind("AngleA "+IntegerToString(Complect))<0)
      {
        ObjectCreate("AngleA "+IntegerToString(Complect),OBJ_TRENDBYANGLE,0,Time[i+nb_0],MALast,Time[i+1],MACurr);
        ObjectSet("AngleA "+IntegerToString(Complect),OBJPROP_STYLE,2);
      }
      
      if (ObjectFind("AngleText "+IntegerToString(Complect))==-1)
          ObjectCreate("AngleText "+IntegerToString(Complect),OBJ_TEXT,0,0,0);
      ObjectSet("AngleA "+IntegerToString(Complect),OBJPROP_TIME2,Time[i+1]);
      ObjectSet("AngleA "+IntegerToString(Complect),OBJPROP_PRICE2,MACurr);
      ObjectSet("AngleA "+IntegerToString(Complect),OBJPROP_TIME1,Time[i+nb_0]);
      ObjectSet("AngleA "+IntegerToString(Complect),OBJPROP_PRICE1,MALast);
      ObjectSet("AngleA "+IntegerToString(Complect),OBJPROP_COLOR,col_ang);
      value[i] = (double)ObjectGet("AngleA "+IntegerToString(Complect),OBJPROP_ANGLE);
      if(value[i]>90)
        value[i] = value[i]-360;
      value[i+1] = value[i];
      ObjectSetText("AngleText "+IntegerToString(Complect),DoubleToStr(value[i],2),8,"Verdana",col_ang);
      ObjectMove("AngleText "+IntegerToString(Complect),0,Time[0]+8*Period()*60,ObjectGetValueByShift("AngleA "+IntegerToString(Complect),i)); 
    }
   //if zzbar exists ends here 
   }  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
  
int GetZZbar(int ne=0)
 {
  double zz;
  int    j, k=iBars(NULL, 0), ke=0;
  
  for (j =1; j <k; j++)
   {
    zz=iCustom(NULL, 0, "ZigZag", ExtDepth,5,3, 0,j);
    if (zz !=0)
     {
      ke++;
      if (ke > ne) 
        return(j);
     }
   }
  
  
  return(-1);
 }
 

You have to check if the nb_0 bar exists (not -1) before you proceed with calculations .

What was happening : your loop reached 0 (i=0) and if GetZZbar returned -1 then you were requesting 

Time[i+nb_0] //Time[-1] 
MALast= varMA[i+nb_0] //varMA[-1]