Help with Array Out of Range - Solved

 

Hey all,

I'm getting an array out of range on this custom indi. I can't seem to figure out why. Any help is greatly appreciated!

//HullAMA.mq4

#property copyright "Copyright © 2004, by konKop,wellx"
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Sienna
#property indicator_color2 DeepSkyBlue
#property indicator_color3 Gold

//---- input parameters
extern int       ShowBars=100;
extern int       periodAMA=9;
extern int       nfast=2;
extern int       nslow=30;
extern double    G=2.0;
extern double    dK=2.0; 

//---- buffers
double HullAMAbuff[];
double kAMAbuff1[];
double kAMAbuff2[];
double kAMAupsig[];
double kAMAdownsig[];
double Calc[];

//+------------------------------------------------------------------+

double slowSC,fastSC;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE,0,2);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,159);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,159);
   SetIndexBuffer(0,HullAMAbuff);
   SetIndexBuffer(1,kAMAupsig);
   SetIndexBuffer(2,kAMAdownsig);
   SetIndexBuffer(3,kAMAbuff1);
   SetIndexBuffer(4,kAMAbuff2);
   SetIndexBuffer(5,Calc);
   
   IndicatorDigits(Digits);
   
//----
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate (const int rates_total,      // size of input time series
                 const int prev_calculated,  // bars handled in previous call
                 const datetime& time[],     // Time
                 const double& open[],       // Open
                 const double& high[],       // High
                 const double& low[],        // Low
                 const double& close[],      // Close
                 const long& tick_volume[],  // Tick Volume
                 const long& volume[],       // Real Volume
                 const int& spread[])        // Spread
   {
   int    periodAMA2 = floor(periodAMA/2);
   int    i,pos=0,pos2=0,pos3=0,pos4=0;
   double noise=0.000000001,AMA,AMA0,signal,ER;
   double dSC,ERSC,SSC,ddK;
    
   slowSC=(2.0 /(nslow+1));
   fastSC=(2.0 /(nfast+1));
   pos = rates_total - prev_calculated ;
   if(pos - periodAMA - 2 < 0) {pos = rates_total - prev_calculated;}
   if(pos - periodAMA - 2 >= 0){pos = rates_total - prev_calculated - periodAMA - 2;}
   pos2=pos; pos3=pos; pos4=pos;
   AMA0=Close[pos+1];
   while (pos>=0)
     {
      if(pos==ShowBars-periodAMA-2) AMA0=Close[pos+1];
      signal=MathAbs(Close[pos]-Close[pos+periodAMA]);
      noise=0.000000001;
      for(i=0;i<periodAMA;i++)
       {
        noise=noise+MathAbs(Close[pos+i]-Close[pos+i+1]);
       };
      ER =signal/noise;
      dSC=(fastSC-slowSC);
      ERSC=ER*dSC;
      SSC=ERSC+slowSC;
      AMA=AMA0+(MathPow(SSC,G)*(Close[pos]-AMA0));
      kAMAbuff1[pos]=AMA;

      /*
      ddK=(AMA-AMA0);
      if ((MathAbs(ddK)) > (dK*Point) && (ddK > 0)) kAMAupsig[pos] =AMA; else kAMAupsig[pos]=0;
      if ((MathAbs(ddK)) > (dK*Point) && (ddK < 0)) kAMAdownsig[pos]=AMA; else kAMAdownsig[pos]=0;
      */ 

      AMA0=AMA;
      pos--;
     };
     
   while (pos2>=0)
     {
      if(pos2==Bars-periodAMA2-2) AMA0=Close[pos2+1];
      signal=MathAbs(Close[pos2]-Close[pos2+periodAMA2]);
      noise=0.000000001;
      for(i=0;i<periodAMA2;i++)
       {
        noise=noise+MathAbs(Close[pos2+i]-Close[pos2+i+1]);
       };
      ER =signal/noise;
      dSC=(fastSC-slowSC);
      ERSC=ER*dSC;
      SSC=ERSC+slowSC;
      AMA=AMA0+(MathPow(SSC,G)*(Close[pos2]-AMA0));
      kAMAbuff2[pos2]=AMA;

      /*
      ddK=(AMA-AMA0);
      if ((MathAbs(ddK)) > (dK*Point) && (ddK > 0)) kAMAupsig[pos2] =AMA; else kAMAupsig[pos2]=0;
      if ((MathAbs(ddK)) > (dK*Point) && (ddK < 0)) kAMAdownsig[pos2]=AMA; else kAMAdownsig[pos2]=0; 
      */
     
      AMA0=AMA;
      pos2--;
     };
     
     while (pos3>=0)
     {
      Calc[pos3] = (2*kAMAbuff1[pos3])-kAMAbuff2[pos3];
      pos3--;
     };
     
     while (pos4>=0)
     {
      HullAMAbuff[pos4] = iMAOnArray(Calc,0,floor(MathSqrt(periodAMA)),0,MODE_LWMA,pos4);
      
      if(pos4==Bars-periodAMA-2){AMA0=Close[pos4+1];}else{AMA0=HullAMAbuff[pos4+1];}
      ddK=(HullAMAbuff[pos4]-AMA0);
      if ((MathAbs(ddK)) > (dK*Point) && (ddK > 0)) {kAMAupsig[pos4] =AMA;}  else {kAMAupsig[pos4]=0;}
      if ((MathAbs(ddK)) > (dK*Point) && (ddK < 0)) {kAMAdownsig[pos4]=AMA;} else {kAMAdownsig[pos4]=0;}
      
      pos4--;
     }
     
//----
   return(rates_total);
  }
 

OK... I figured it out...

I was missing

IndicatorBuffers(6);
In the OnInit().
 
Hiltos: I'm getting an array out of range on this custom indi. I can't seem to figure out why.
   pos = rates_total - prev_calculated ;
:
   AMA0=Close[pos+1];
First pass prev_calculated is zero and pos therefor equals the number of bars. Close index is [zero to Bars - 1]. You index out of range.