Array Out of Range Error

 

Hi Erveryone,

I am new to MT4 programming, and just try to learning and creating my own EA with my logic, and for that I needed to calculate Fibo levels between 2 assigned prices, I tried to search a lot, but I couldn't find an inbuilt function or class, though I found some OBJFIBO but couldn't understand how to use it, so I have 2 queries:

1. Can I get Fibo values by a function by just passing high, low and trend values.

2. if not I tried to create custom code for the same, below is the code snippet I am using and compiler shows no error, but when I try to run the code the debugger says "Array out of Range" if someone can help me on the same please.

TIA

double ma9, ma23, ma50, ma100, ma150, ma200, tp, sl, lastbid, lastask, fibouplevels1[], fibodownlevels1[], fiblevels1[];
int ordertype1;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
   ArrayResize(fiblevels1,50,10);
   ArrayResize(fibodownlevels1,50,10);
   ArrayResize(fibodownlevels1,50,10);
   ArrayInitialize(fiblevels1,EMPTY_VALUE);
   ArrayInitialize(fibouplevels1,EMPTY_VALUE);
   ArrayInitialize(fibodownlevels1,EMPTY_VALUE);
   fiblevels1[1]=0.0;
   fiblevels1[2]=0.236;
   fiblevels1[3] = 0.382;
   fiblevels1[4] = 0.5;
   fiblevels1[5] = 0.618;
   fiblevels1[6] = 0.786;
   fiblevels1[7] = 0.886;
   fiblevels1[9] = 1.0;
   fiblevels1[9] = 1.130;
   fiblevels1[10] = 1.270;
   fiblevels1[11] = 1.382;
   fiblevels1[12] = 1.5;
   fiblevels1[13] = 1.618;
   fiblevels1[14] = 1.786;
   fiblevels1[15] = 1.886;
   fiblevels1[16] = 2.0;
   fiblevels1[17] = 2.130;
   fiblevels1[18] = 2.270;
   fiblevels1[19] = 2.382;
   fiblevels1[20] = 2.5;
   fiblevels1[21] = 2.618;
   fiblevels1[22] = 2.786;
   fiblevels1[23] = 2.886;
   fiblevels1[24] = 3.0;
   int i=1;
   double swingdiff = SwingHigh - SwingLow;
   for(i=1; i <= 24; i++){
      if(i==1){
         fibouplevels1[i] = SwingLow;
         fibodownlevels1[i] = SwingHigh;
      } else {
         fibouplevels1[i] = SwingLow + (swingdiff * fiblevels1[i]);
         fibodownlevels1[i] = SwingHigh - (swingdiff * fiblevels1[i]);
         Alert("Fib Up Level =" , fibouplevels1[i]);
      }
   }
   return(INIT_SUCCEEDED);
  }
 
ArrayResize(fiblevels1,50,10);

To be honest I don't actually know what happens when you resize an array to 50, but set the reserve to 10, but it seems a very strange thing to do.

If you know that you are going to use 25 (you don't use [0]), then why not set the size accordingly?

I know that it is not obvious, but topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I will move your topic to the MQL4 and Metatrader 4 section.

 

Thanks for your quick response, I removed that reserve size optional parameter b making code as:


ArrayResize(fiblevels1,50);
ArrayResize(fibodownlevels1,50);
ArrayResize(fibodownlevels1,50);


But I still get the same error.

 
ArrayResize(fiblevels1,50);
ArrayResize(fibodownlevels1,50);
ArrayResize(fibodownlevels1,50);

Maybe because you don't size fibouplevels1 /

 
How Silly I am, really, a copy paste can kill you sometime, thanks for pointing it out, it worked for me.
Reason: