Please help "Array out of range"

 

Hello,


I'm new to coding. I tried to make an indicator which is basically enveloppes but with standard deviation.

I followed jimdandy's tutorial : https://www.youtube.com/watch?v=itfBRhu3p8g


And coded this :


#property strict

#property indicator_chart_window


input int period = 28;


double MA_Top[];

double MA_Mid[];

double MA_Bot[];


double Std_Dev[];


int OnInit()

  {

  

   SetIndexBuffer(0, MA_Top);

   SetIndexBuffer(1, MA_Mid);

   SetIndexBuffer(2, MA_Bot);

  

   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 1, clrBlack);

   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 1, clrBlack);

   SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, 1, clrBlack);

   

   SetIndexLabel(0, "Top");

   SetIndexLabel(1, "Mid");

   SetIndexLabel(2, "Bot");

  

   SetIndexDrawBegin(0, period);

   SetIndexDrawBegin(0, period);

   SetIndexDrawBegin(0, period);

  

   return(INIT_SUCCEEDED);

   

  }


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 countedBars = IndicatorCounted();

  if(countedBars < 0) return(-1);

  if(countedBars > 0) countedBars--;

  

  int uncountedBars = Bars - countedBars;

   

     for(int i = 0; i < uncountedBars; i++)

     {

         Std_Dev[i] = iStdDev(NULL, 0, period, 0, 0, 0, i);

              Print("ERROR");

         MA_Top[i] = iMA(NULL, 0, period, 0, 0, 0, i) + Std_Dev[i];

         MA_Mid[i] = iMA(NULL, 0, period, 0, 0, 0, i);

         MA_Bot[i] = iMA(NULL, 0, period, 0, 0, 0, i) - Std_Dev[i];

       

     }

    

   return(rates_total);

  }


It should work but I get the error "array out of range in 'STDDEV-ENVELOPPES.mq4' (69,17)".

It comes from the first array in the for loop, but I can't find why.


Can someone please help me ?


Thanks !



 
  1. When you post code please use the SRC button !
               General rules and best pratices of the Forum. - General - MQL5 programming forum
  2. double Std_Dev[];
    int OnCalculate(...){
       :
             Std_Dev[i] = iStdDev(NULL, 0, period, 0, 0, 0, i);
    The array has no size, why do you expect anything else?