Download MetaTrader 5

array out of range error

To add comments, please log in or register
billworld2
23
billworld2  

An MQL4 function I've created and used before in prior MT4 versions is now yielding an "array out of range error" in my EA in latest version of MT4. I've searched around and can't find an answer. The error is referencing the line:

 Level = Levels[j]; 

after the word "Levels"

How do I fix this? 

 Thanks

 Bill 


//-----------------------------------
// RainbowTop() Function
//-----------------------------------
double RainbowTop(int BarsBack)
   {
   double   MA1 = NormalizeDouble(iMA(Symbol(), Period(), 8, 0, MODE_EMA, PRICE_CLOSE, BarsBack), Digits());
   double   MA2 = NormalizeDouble(iMA(Symbol(), Period(), 13, 0, MODE_EMA, PRICE_CLOSE, BarsBack), Digits());
   double   MA3 = NormalizeDouble(iMA(Symbol(), Period(), 21, 0, MODE_EMA, PRICE_CLOSE, BarsBack), Digits());
   double   MA4 = NormalizeDouble(iMA(Symbol(), Period(), 34, 0, MODE_EMA, PRICE_CLOSE, BarsBack), Digits());
   double   MA5 = NormalizeDouble(iMA(Symbol(), Period(), 55, 0, MODE_EMA, PRICE_CLOSE, BarsBack), Digits());

   double Levels[5];  
   Levels[0] = MA1;
   Levels[1] = MA2;
   Levels[2] = MA3;
   Levels[3] = MA4;
   Levels[4] = MA5;
  
   double TopLevel = 0;
   double Level = 0;
      
   for(int j=0; j<=ArraySize(Levels); j++)
      {
      Level = Levels[j];
      if (Level > TopLevel)      
         {
         TopLevel = Level;
         }
      }
   return(TopLevel);
   }


 

Fernando Carreiro
3922
Fernando Carreiro  
billworld2: An MQL4 function I've created and used before in prior MT4 versions is now yielding an "array out of range error" in my EA in latest version of MT4. I've searched around and can't find an answer. The error is referencing the line:

 Level = Levels[j]; 

after the word "Levels"

How do I fix this?

...

for(int j=0; j<=ArraySize(Levels); j++)

...
It should be "j < ArraySize(Levels)" and not "<=".
billworld2
23
billworld2  
Fernando Carreiro:
It should be "j < ArraySize(Levels)" and not "<=".
That works. Thanks Fernando!
To add comments, please log in or register