Seeking help to debug array out of range error ...

 

Please help me debug "array out of range in 'FxEATestHighLow2.mq5' (68,20)" error in the following code ...

//+----------------------------------------------------------------------------------------------------------+
//| FxEATestHighLow2.mq5
//+----------------------------------------------------------------------------------------------------------+
  input uint SlowLength = 21; // Slow Zigzag Period
  input uint FastLength = 8;  // Fast Zigzag Period

  double Major_SwingLow[];   // 0 Buffer No
  double Major_SwingHigh[];  // 1 Buffer No
  double Minor_SwingLow[];   // 2 Buffer No
  double Minor_SwingHigh[];  // 3 Buffer No
//+----------------------------------------------------------------------------------------------------------+
//| Expert initialization function
//+----------------------------------------------------------------------------------------------------------+
int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }
//+----------------------------------------------------------------------------------------------------------+
//| Expert de initialization function
//+----------------------------------------------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+----------------------------------------------------------------------------------------------------------+
//| Expert tick function
//+----------------------------------------------------------------------------------------------------------+
void OnTick()
  {
    int index    = 1;
  //---
    Get_SwingHL();
    for(int i = 1000; i >= 0; i--)
      Print("Major High [",Major_SwingHigh[i],"] Low [",Major_SwingLow[i],"]");
  } // END Of expert advisor
//+----------------------------------------------------------------------------------------------------------+
//+----------------------------------------------------------------------------------------------------------+
//| Function: SwingHighLow()
//+----------------------------------------------------------------------------------------------------------+
bool Get_SwingHL()
  {
    //+----------------------------------------------+
      int barStart    = 0;
      int barLookBack = 1500;     // (limit)
      int ZigZagUp, ZigZagDn, Swing_p, Swing_n, limit;
      double BarHigh, BarLow, HHigh, LLow;
      int min_rates_total = int(MathMax(SlowLength,FastLength))+1;
      limit   = barLookBack - min_rates_total;
    //+----------------------------------------------+
      double High[], Low[];
      ArraySetAsSeries(High,true);
      int copiedH = CopyHigh(_Symbol,PERIOD_M1,barStart,limit,High);
      if(copiedH < 0)
        Print(__FUNCTION__," ",__LINE__,": Error copying High[]");
      int copiedL = CopyLow(_Symbol,PERIOD_M1,barStart,limit,High);
      if(copiedL < 0)
        Print(__FUNCTION__," ",__LINE__,": Error copying Low[]");
      ArraySetAsSeries(Low,true);
  //+----------------------------------------------------------------------------------------------------------+
  //| Calculate MAJOR Swing Low/High
  //+----------------------------------------------------------------------------------------------------------+
    Swing_p = 0;
    Swing_n = 0;
    ZigZagUp = limit;       // (zu)
    ZigZagDn = limit;       // (zd)
    BarHigh  = High[limit]; // (BH)
    BarLow   = Low[limit];  // (BL)
    for(int i = limit; i >= 0 && !IsStopped(); i--)
      {
        Major_SwingLow[i]  = NULL;
        Major_SwingHigh[i] = NULL;
        
        HHigh = High[ArrayMaximum(High,i+1,SlowLength)];
        LLow  = Low[ArrayMinimum(Low,i+1,SlowLength)];
        if((Low[i] < LLow) && (High[i] > HHigh))
          {
            Swing_p = 2;
            if(Swing_n ==  1) ZigZagUp = i + 1;
            if(Swing_n == -1) ZigZagDn = i + 1;
          }
        else
          {
            if(Low[i]  < LLow)  Swing_p = -1;
            if(High[i] > HHigh) Swing_p =  1;
          }

        if(Swing_p != Swing_n && Swing_n != 0)
          {
            if(Swing_p == 2)
              {
                Swing_p = -Swing_n;
                BarHigh = High[i];
                BarLow  = Low[i];
              }
            if(Swing_p == 1)
              {            
                if(BarLow == Low[ZigZagDn])
                  Major_SwingLow[ZigZagDn]   = BarLow;
                else
                  Major_SwingLow[ZigZagDn-1] = BarLow;
              }
            if(Swing_p == -1)
              {
                if(BarHigh == High[ZigZagUp])
                  Major_SwingHigh[ZigZagUp]   = BarHigh;
                else
                  Major_SwingHigh[ZigZagUp-1] = BarHigh;
              }
           BarHigh = High[i];
           BarLow  = Low[i];
          }
        if(Swing_p ==  1)
          {
            if(High[i] >= BarHigh)
              {
                BarHigh  = High[i];
                ZigZagUp = i;
              }
          }
        if(Swing_p == -1)
          {
            if(Low[i] <= BarLow)
              {
                BarLow   = Low[i];
                ZigZagDn = i;
              }
          }
        Swing_n = Swing_p;
     }
  //---
    return(true);
  } // END Of method Get_SwingHL()
//+----------------------------------------------------------------------------------------------------------+
 
BarHigh  = High[limit]; // (BH)

I think that you will find that the array size is limit, so elements 0 to limit-1

Why have you posted in "Trading Systems"? Your question does not relate to the section.

I will move your topic to the EA section.

In future please post in a relevant section.

 
Keith Watford:

I think that you will find that the array size is limit, so elements 0 to limit-1

Why have you posted in "Trading Systems"? Your question does not relate to the section.

I will move your topic to the EA section.

In future please post in a relevant section.

Thanks Keith for highlighting to set ArraySize.

I have updated the code with following, however it did not help. Still getting the same error.

Will take care in future about relevant section.

      double High[], Low[];
      ArrayResize(High,barLookBack);
      ArraySetAsSeries(High,true);
      int copiedH = CopyHigh(_Symbol,PERIOD_M1,barStart,limit,High);
      if(copiedH < 0)
        Print(__FUNCTION__," ",__LINE__,": Error copying High[]");
      ArrayResize(Low,barLookBack);
      ArraySetAsSeries(Low,true);
      int copiedL = CopyLow(_Symbol,PERIOD_M1,barStart,limit,High);
      if(copiedL < 0)
        Print(__FUNCTION__," ",__LINE__,": Error copying Low[]");
 

The changes that you have made are not relevant.

BarHigh  = High[limit]; // (BH)

I think that you will find that the array size is limit, so elements 0 to limit-1

so High[limit] will be out of range.

Reason: