Array out of range error

Hi can someone please provide a fix for the array of range error i am getting any help will be sincerely appreciated :slight_smile: ,my code looks like this

    //+------------------------------------------------------------------+
    //|                                            Mcgee's indicator.mq4 |
    //|                                                  McNilly Goshomi |
    //|                                         mcnillygoshomi@gmail.com |
    //+------------------------------------------------------------------+
    #property copyright "McNilly Goshomi"
    #property link      "mcnillygoshomi@gmail.com"
    #property version   "1.00"
    #property strict
    #property indicator_chart_window
    #property indicator_buffers 2
    #property indicator_color1 clrGreen
    #property indicator_color2 clrRed
    #property  indicator_width1 2
    #property  indicator_width2 2
    //--- input parameters
    extern double bmi=25.0;
    double up[];
    double down[];

    //+------------------------------------------------------------------+
    //| Custom indicator initialization function                         |
    //+------------------------------------------------------------------+
    int OnInit()
      {
       bmi*=.01;
    //--- indicator buffers mapping
          SetIndexBuffer(0,up);
          SetIndexStyle(0,DRAW_ARROW);
          SetIndexArrow(0,233);
          SetIndexLabel(0,"Up Arrow");

          SetIndexBuffer(1,down);
          SetIndexStyle(1,DRAW_ARROW);
          SetIndexArrow(1,234);
          SetIndexLabel(1,"Down Arrow");
    //---
       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 = MathMax(rates_total-prev_calculated,2);
             for (int i=1;i<limit;i++)
             {
              double total=High[i]-Low[i];
              double body=MathAbs(Open[i]-Close[i]);
              double maxSize=total*bmi;
              if (body<maxSize
                  &&Open[i]<Close[i]
                  &&High[i]-Open[i]<maxSize
                  &&Low[i]<Low[i+1]
                  &&Low[i]<Low[i+2])
                  up[i]=Low[i];
          else if(body<maxSize
                  &&Open[i]>Close[i]
                  &&High[i]-Close[i]<maxSize
                  &&Low[i]<Low[i+1]
                  &&Low[i]<Low[i+2])
                  up[i]=Low[i];
          else if(body<maxSize
                  &&Open[i]>Close[i]
                  &&Open[i]-Low[i]<maxSize
                  &&High[i]>High[i+1]
                  &&High[i]>High[i+2])
                  down[i]=High[i];
          else if(body<maxSize
                  &&Open[i]<Close[i]
                  &&Close[i]-Low[i]<maxSize
                  &&High[i]>High[i+1]
                  &&High[i]>High[i+2])
                  down[i]=High[i];
             }
    //---


    //--- return value of prev_calculated for next call
       return(rates_total);
      }
    //+------------------------------------------------------------------+
    ```

Hi @Mcnilly_Goshomi. Array out of range is a common error. Somewhere in your code you are accessing an array element that does not exist. For example, if an array has 10 elements, you are trying to get its 11th element. Your code fragment is big. I suggest you share small code fragments so people can actually read it. Also, search the site for ‘Array out of range’ i’m sure there are other posts about this error

1 Like

Hey @baarang_solio thanks so much for getting back to me,i did finally get to fix it :slight_smile:

1 Like