Noob Question - How to get notified in the code when an order became closed?

 

Hi

I use my EA to open a couple of orders. How do I get notified when one of the trades gets closed?  What function do I have to use?  I can't find how to do this.

I know that I can set Orderstotal() to give me my maximum of allowed trades in any moment and then once this value drops by 1 I know that one trade has been closed - but which one?? How to immediately get which one was closed right after that happens?

I thought that the function OrderClosePrice() only gave you the close price when it was determined, but it doesn't. It keeps giving you a price which changes with each tick until the order gets closed. That's not good.

Would appreciate some help.

 

If you use OrderClose() to close your orders then you can Send Notification() or Send Email or Alert function when OrderClose==true. Within the Notification, you can include the Order# or other details about the order.

If your orders are being closed through StopLoss or TakeProfit or Expirations then you'll have to code something more. Easiest way is probably to keep a GlobalVariable() of (Last Order Close Time) or (Highest Order Close Time). Your code should check the OrderCloseTime() of recent orders (Obviously sorted by CloseTime). If OrderCloseTime() > GV(Highest_Order_Close_Time){ Send_Notification; } then set the GlobalVariable to the Newer|Last CloseTime.

 

Dennis73:

How do I get notified when one of the trades gets closed?  What function do I have to use?  I can't find how to do this.

I know that I can set Orderstotal() to give me my maximum of allowed trades in any moment and then once this value drops by 1 I know that one trade has been closed - but which one?? How to immediately get which one was closed right after that happens?

I thought that the function OrderClosePrice() only gave you the close price when it was determined, but it doesn't. It keeps giving you a price which changes with each tick until the order gets closed. That's not good.

  1. OrderSelect loop, count how many are open. If its less then previous, a order has closed. Remember the count for next tick.
  2. Use a OrderSelect HISTORY loop to find the newest closed order, the one with the largest OrderCloseTime() or use my functions: Could EA Really Live By Order_History Alone? - MQL4 forum
  3. OrderClosePrice changes until the order gets closed. What's the problem with that?
 
WHRoeder:
  1. OrderSelect loop, count how many are open. If its less then previous, a order has closed. Remember the count for next tick.
  2. Use a OrderSelect HISTORY loop to find the newest closed order, the one with the largest OrderCloseTime() or use my functions: Could EA Really Live By Order_History Alone? - MQL4 forum
  3. OrderClosePrice changes until the order gets closed. What's the problem with that?




I wish the OrderClosePrice was Null or zero until the order becomes closed. That would be very helpful. It would really make things easier:

If I had 3 active trades in every moment I could make a "for" loop that checks those 3 trades in every tick, then if (OrderClosePrice != 0) becomes true = this order has closed, and you can make changes to your EA directly from this position. 

It's weird that the function refers to a "ClosePrice" while the order is not actually closed.


Well, this is what I decided for my case. If I have 3 active trades in every moment then when OrdersTotal() falls to 2 it will open two "if" statements that check the two remaining trades.

if (OrdersTotal()<3 && a==0) {

OrderSelect(0, SELECT_BY_POS, MODE_TRADES);   if necessary - adapt current order depending on the closed one, + conditions that will allow the opening of another trade, if conditions are good a=1; 

OrderSelect(1, SELECT_BY_POS, MODE_TRADES);   if necessary - adapt current order depending on the closed one, + if (a==0) {conditions that will allow the opening of another trade, if they are met, a=1;  }}   }


This is still somehow easier for me then using MODE_HISTORY. I could be wrong.

Thanks for the replies.

 
Dennis73:

I wish the OrderClosePrice was Null or zero until the order becomes closed. That would be very helpful. It would really make things easier:

OrderSelect(0, SELECT_BY_POS, MODE_TRADES);   if necessary - adapt current order depending on the closed one, + conditions that will allow the opening of another trade, if conditions are good a=1; 
You are selecting MODE_TRADES, not MODE_HISTORY - you get ONLY open and pending orders. OrderClosePrice is irrelevant and OrderCloseTime will always be zero, stop looking at them!
Reason: