Keep order info beyond closing of order

 

Hello forum,

So I am looking to incorporate the following logic into my EA:

1)Record time of opening Order A

2)keep it and work with it....... until

3)new Order B is opened.

I did 1) via selecting the order in MODE_TRADES and recording the time etc., but wih 2) I got stuck - I thought a static variable under start() function might work, but then I realised it gets zeroed once Order A is closed. Is there a way to block and record the open time of an Order beyond the closing of that order? (I am looking for some simple logic, of counting orders or variables, etc - something like while... no new Order B is Open, keep last recorded time of Order A. I do not want to go into selecting orders from History --too painful : )

Dan

 
Dannoo007:

I thought a static variable under start() function might work, but then I realised it gets zeroed once Order A is closed.

Not sure what you mean by this ? the variable will only change if you assign a new value to it . .
 
Dannoo007:

Hello forum,

So I am looking to incorporate the following logic into my EA:

1)Record time of opening Order A

2)keep it and work with it....... until

3)new Order B is opened.

I did 1) via selecting the order in MODE_TRADES and recording the time etc., but wih 2) I got stuck - I thought a static variable under start() function might work, but then I realised it gets zeroed once Order A is closed. Is there a way to block and record the open time of an Order beyond the closing of that order? (I am looking for some simple logic, of counting orders or variables, etc - something like while... no new Order B is Open, keep last recorded time of Order A. I do not want to go into selecting orders from History --too painful : )

Dan

The reason you got stuck is probably a bug in your code ( and by "probably" I mean certainly!)

There is no point in saving information about an open position because the terminal does all that for you. You could save the ticket number in a static or global variable so you don't have to look through the OrdersTotal list.

 

If you save the open time of an order in a static variable while the order is open, the static variable will only lose that data if you over write the same variable with the open time of the next order. you could use two variables one for the current order and one for the previous order and in your close order code do something like

bool response = OrderClose(ticket,lot,Bid,0,Lime);
if(response == true)
{previous = current;
 current = 0;
}
Thats assuming you only have one order open at a time if you use multiple orders at the same time you should work with an array to manage order information the mql4 book has a good example of using an array for that.
 
drop the == true entirely. Would you ever say
if ( (2+2 == 4) == true) ..
Of course not. If (bool) or if (!bool) is sufficient. Always test return codes.
Drop variable
Use readable names (isX/hasX)
if (!OrderClose(ticket,lot,Bid,0,Lime))
    Alert("OrderClose failed: ", GetLastError());
else{previous = current;
 current = 0;
}
bool hasClosed = OrderClose(ticket,lot,Bid,0,Lime);
if (!hasClosed)
    Alert("OrderClose failed: ", GetLastError());
else{previous = current;
 current = 0;
}
 

Simply put the variable of your choice int or datetime ( bool can do the job as well as it is not stacked to 1 or 0 ) out of tasks (like start), ex.:

/* return; }..tasks.. */
int stored;
/* or int stored[10]; */
/* ... out of tasks like ... int start() {... */