Detect event when pending order is placed or removed?

 

Hello everyone,

I'm coding my first EA and I'm trying to detect when a pending order is added or removed from the chart.

I'm currently looking at the function OnTradeTransaction(), particularly at ENUM_TRADE_TRANSACTION_TYPE inside the object MqlTradeTransaction.

I see there are these 2 values that could be handy for the task: TRADE_TRANSACTION_ORDER_ADD and TRADE_TRANSACTION_ORDER_DELETE .

I think these 2 events happen also when I send a market order so I need to find a way to distinguish market orders from pending orders.

Should I simply check order_type in the MqlTradeTransaction object?

Is this approach the correct one to be sure pending orders are being placed and removed?


Many thanks!
 

Please use my TradeTransaction Class

https://www.mql5.com/en/code/24901

TradeTransaction Class
TradeTransaction Class
  • www.mql5.com
A base class to simplify analyzing trade transactions in MQL5.
 

Up, could anyone confirm this is the correct approach to know if a pending order is placed or removed?

My worry is that I could receive this kind of events even if the broker actually refuses the orders.

Thank you

 
amrali:

Please use my TradeTransaction Class

https://www.mql5.com/en/code/24901

Thank you amrali, I'm going to check your class!
 

Just checked amrali class, it's perfect, handles every order and position action without any issue!


Thanks again!
 
Your are so welcome
 
amrali:
Your are so welcome

Amrali, I'm using your code but when TradeTransactionPositionOpened is called the position values (like position price) are all 0.0:

virtual void      TradeTransactionPositionOpened(ulong position, ulong deal)    
{
      //PrintFormat("Position is opened. (position %I64u, deal %I64u)", position, deal);
      
      PositionSelect(position);
      long pos_id = PositionGetInteger(POSITION_IDENTIFIER);
      string pos_symbol = PositionGetString(POSITION_SYMBOL);
      double pos_price = PositionGetDouble(POSITION_PRICE_OPEN);

}

The value pos_price is always 0.0. Do you know why and how can I retrieve the actual values of the position?

Thank you!

 
Axel_Lexa:

Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

 
Keith Watford:

Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

Hello Keith, done, apologize for the format :)


Still trying to understand why the values of the position are zero...

 

UPDATE: My bad I had to use PositionSelectByTicket(position_ticket) to select the open position.


My new problem is related to the previous one: when the position is closed (for example by tp or sl) PositionSelectByTicket() returns false. My guess is because the position has just been closed so it doesn't exist anymore. Is checking the closing deal enough for getting the information like closing price/profit/fees?


Thank you!
Reason: