Download MetaTrader 5

Problems with arrays

To add comments, please log in or register
Create Forum topics and improve your rating!
Rio
30
Rio 2014.03.27 10:57 

I'm still a bit new to MQL4 especially after the build 600 changes

Anyway, I'm not sure if my coding is wrong, if this is a bug, or if I don't understand something... but anyway:

double calc[];

for (int loop = 200; loop >= 0; loop--) {

calc[loop] = iRSI(NULL, timeframe, period, price, loop);

}

Ends up with the calc[] array simply full of zeroes.

However if I define

double calc[200];

Then it works just fine.

Mind you, this is inside an EA and not an indicator. As I am trying to port the code from an indicator directly into an EA, I just don't understand why it's behaving this way, if it's supposed to, or if I should know better.

I also have a similar routine with iMAOnArray which does something similar, except that even when it is defined only the first 13 in the array get a value and everything else is zero.

To me it's making no sense at all.

Alain Verleyen
Moderator
30728
Alain Verleyen 2014.03.27 11:11  
nayphee:

I'm still a bit new to MQL4 especially after the build 600 changes

Anyway, I'm not sure if my coding is wrong, if this is a bug, or if I don't understand something... but anyway:

double calc[];

for (int loop = 200; loop >= 0; loop--) {

calc[loop] = iRSI(NULL, timeframe, period, price, loop);

}

Ends up with the calc[] array simply full of zeroes.

...

You have to give a size to your dynamic array before using it.

   double calc[];

   ArrayResize(calc,201);        // Your are using index 200 to 0, so you need 201 elements
   
   for(int loop=200; loop>=0; loop--) 
     {

      calc[loop]=iRSI(NULL,timeframe,period,price,loop);

     }
Rio
30
Rio 2014.03.27 11:47  

Yes, the ArrayResize does fix it too, but I am still left with the tricky problem of this:

double priceline[];

ArrayResize(priceline,201);

double calc[];

ArrayResize(calc,201);

for (int loop = 200; loop >= 0; loop--) {

calc[loop] = iRSI(NULL, timeframe, period, price, loop);

}

for (loop = 199; loop >= 0; loop--) {

priceline[loop] = iMAOnArray(calc, timeframe, priceline, 0, pricetype, loop);

}

I did some debugging on the output, and while the calc[] array has values in each index of the array from 0 to 200, the priceline[] array has zero in each index from 14 upwards. All indexes from 0-13 have a value.

What's going on?

Rio
30
Rio 2014.03.27 12:06  
Sorry, never mind that. I have my iMAOnArray parameters wrong :(
qjol
3243
qjol 2014.03.27 12:14  

just for information purposes you can use

for (loop = ArraySize(calc).....)
To add comments, please log in or register