Problem with arrays

 

Hi There

I'm having major difficulties with certain arrays in mql4...

double x1=OrdersHistoryTotal()-1;

TS[x1]=11;

Alert ("TS X1 = "+TS[x1]);

…but the output produces: "Alert: TS X1 = 0.00000000"

Why?

 

Probably because your array is not big enough, the array index you're trying to use could be way up there if you have a lot of closed orders in the history, or if you have no orders in the history you would be trying to use array index of -1 which would also not work.

   double TS[];
   int x1=OrdersHistoryTotal()-1;
   ArrayResize(TS,x1+1);

   TS[x1]=11;

   Alert ("TS X1 = ",TS[x1]);
   Alert ("OrdersHistoryTotal = ",OrdersHistoryTotal());
 
You also should not index an array with a double. Use an int.
 
PFUNK:
double x1=OrdersHistoryTotal()-1;
TS[x1]=11;
  1. Make sure you know the problems with history Could EA Really Live By Order_History Alone? - MQL4 forum especially note Order History sort by closing date - MQL4 forum for resizing the arrays.

dabbler:
You also should not index an array with a double. Use an int.
Can't - It won't compile.
 
it compiles and works the way i did it with an int above, I tested it in a script.
 
dabbler:

Since I know you are a competent programmer this answer has stumped me. Did you mean that you can't use an int to set the size of an array, as in

I think WHR means you can't use a double . . . if you try to it won't compile.

'[' - array index is to be an integer

 
RaptorUK:

I think WHR means you can't use a double . . . if you try to it won't compile.

'[' - array index is to be an integer

Yes, I just tried it :-)

Thanks.

Sometimes WHR is so terse it's almost impossible to decipher. I read it as you couldn't use an int as an index, as did SDC.

So, the OP's code didn't even compile? He must have re-typed it wrong rather than copy/paste.

 
Yep, the OP hasn't been back so we may never know ;-)
 
Ok, So I've lost the OrdersHistoryTotal(); … but still getting 0 on Alert ouput. Am missing something really basic…
x1=3;//OrdersHistoryTotal()-1;
    TS[x1]={11};//TrailingStop;
    Alert ("TS X1 = "+TS[x1]);

I should be getting 11 on Alert output

 
dabbler:

Yes, I just tried it :-)

Sometimes WHR is so terse it's almost impossible to decipher. I read it as you couldn't use an int as an index, as did SDC.
In 2009.10 I had coded:
double idx = ...
double val = Array[idx];

And it wouldn't compile; said something like array index must be an integer.

Perhaps my comment is no long true.

 
PFUNK:
Ok, So I've lost the OrdersHistoryTotal(); … but still getting 0 on Alert ouput. Am missing something really basic…

I should be getting 11 on Alert output

How are you declaring the array ? are you using ArrayResize ? you can't just do this . . .

double TS[];

you can do this . . .

double TS[29];

. . . or this . . .

double TS[];

ArrayResize(TS, 28);
Reason: