How to check if a trade has been closed and get all details about that trade

 
Since checking if a position has just closed  is flawed due to sometimes async trade processing which means all details of the trade may not be available immediately in trade history. As highlighted in this forum post https://www.mql5.com/en/forum/265060

How do I check if there is a new deal "out ticket " that has just been added into trade history? That would be a better way to determine if a trade has just closed and then get all details about that trade right?

 

There are different Trade-Transaction-Types: https://www.mql5.com/en/docs/constants/tradingconstants/enum_trade_transaction_type

You can print out all transaction informations and have a look which will occur, before all informations of a closed position are available in the trade history. E.g. In the OnTradeTransaction-Method you can print out the informations of the transactions and the available historical informations of the position.

I think there will be three transactions. The order which shall close the position will be closed (TRADE_TRANSACTION_ORDER_DELETE), the position will be closed (TRADE_TRANSACTION_DEAL_ADD) and the closing order will be added to history (TRADE_TRANSACTION_HISTORY_ADD). The order / sequence can be different. But if all three transactions have occurred, the history should be available.

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Transaction Types
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Transaction Types
  • www.mql5.com
Trade Transaction Types - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5