Download MetaTrader 5

Array out of range. But why?

To add comments, please log in or register
Gordon Gekko
623
Gordon Gekko  

Hi Experts,

Strange thing for me. I have a very short code. There is no error when compiled and runs perfectly on the charts.

But, when I compile it with Debug, it gives an "Array out of range" error. I don't understand, why?

Can you help me? Short code attached.

Thank you!

Files:
s_ma.mq4 4 kb
whroeder1
15697
whroeder1  
Add print statements and track down the line the index and the array size.
Gordon Gekko
623
Gordon Gekko  
WHRoeder:
Add print statements and track down the line the index and the array size.
I've already done it, but as I saw, everything was OK.
Gordon Gekko
623
Gordon Gekko  
Experts, any help in this?
Gordon Gekko
623
Gordon Gekko  

OK, I see nobody wants to download the code.

Here is the problematic code-section:

double Get_s_ma(string symbol,int tframe,int s_ma_period,ENUM_MA_METHOD s_ma_method,ENUM_APPLIED_PRICE s_ma_price,int shift)
  {
   double s_ma;
   int i;
   int P_s_ma=(int)MathFloor(MathSqrt(s_ma_period));
   int MedP_s_ma=(int)MathFloor(s_ma_period/2);
   double Val1[];
   ArrayResize(Val1,P_s_ma);
   ArraySetAsSeries(Val1,true);
   ArrayInitialize(Val1,0);

   for(i=0; i<P_s_ma; i++)
     {
      Val1[i]=2*iMA(symbol,tframe,MedP_s_ma,0,s_ma_method,s_ma_price,shift+i)-iMA(symbol,tframe,s_ma_period,0,s_ma_method,s_ma_price,shift+i);
     }
   s_ma=iMAOnArray(Val1,0,P_s_ma,0,MODE_LWMA,0);

   return(s_ma);
  }

No problem during compiling, but problem during debugging (array out of range).

I'm unable to find what the problem is.

And you?

honest_knave
Moderator
2333
honest_knave  

Print the value of P_s_ma

At a guess that is 0, which means you resize your array to 0 and then try to add something to it in your for loop.

Gordon Gekko
623
Gordon Gekko  
honest_knave:

Print the value of P_s_ma

At a guess that is 0, which means you resize your array to 0 and then try to add something to it in your for loop.

It cannot be 0, because s_ma_period cannot be less than 2 (there is a limitation).

So, P_s_ma is minimum 1.

honest_knave
Moderator
2333
honest_knave  
OK, but have you printed it to prove that?
Gordon Gekko
623
Gordon Gekko  
honest_knave:
OK, but have you printed it to prove that?
Of course, yet before.
honest_knave
Moderator
2333
honest_knave  

The problem seems to be that ArrayResize is not working (which I figured out with 2 print statements...)

Why ArrayResize is not working under debug, I have no idea. I suggest you contact the service desk as it seems like a bug.

double Get_s_ma(string symbol,int tframe,int s_ma_period,ENUM_MA_METHOD s_ma_method,ENUM_APPLIED_PRICE s_ma_price,int shift)
  {
   double s_ma;
   int i;
   int P_s_ma=(int)MathFloor(MathSqrt(s_ma_period));
   printf("P_s_ma is %i",P_s_ma);
   int MedP_s_ma=(int)MathFloor(s_ma_period/2);
   double Val1[];
   ArrayResize(Val1,P_s_ma);
   printf("Val1 size is %i",ArraySize(Val1));
   ArraySetAsSeries(Val1,true);
   ArrayInitialize(Val1,0);

   for(i=0; i<P_s_ma; i++)
     {
      Val1[i]=2*iMA(symbol,tframe,MedP_s_ma,0,s_ma_method,s_ma_price,shift+i)-iMA(symbol,tframe,s_ma_period,0,s_ma_method,s_ma_price,shift+i);
     }
   s_ma=iMAOnArray(Val1,0,P_s_ma,0,MODE_LWMA,0);

   return(s_ma);
  }

 

 

Gordon Gekko
623
Gordon Gekko  
honest_knave:

The problem is ArrayResize is not working (which I figured out with 2 print statements...)

Why ArrayResize is not working under debug, I have no idea. I suggest you contact the service desk as it seems like a bug.

 

Strange. I got 1 for P_s_ma with using the smallest possible value 2 for s_ma_period. It really seems that ArrayResize is not working.

int P_s_ma=(int)MathFloor(MathSqrt(s_ma_period));
int MedP_s_ma=(int)MathFloor(s_ma_period/2);
Print(IntegerToString(MedP_s_ma),"/",IntegerToString(P_s_ma));



123
To add comments, please log in or register