The function is called in EAs when the TradeTransaction event occurs. The function is meant for handling trade request execution results.
[in] MqlTradeTransaction type variable describing a transaction made on a trading account.
No return value
OnTradeTransaction() is called to handle the TradeTransaction event sent by the trade server to the terminal in the following cases:
- sending a trade request from an MQL5 program using the OrderSend()/OrderSendAsync() functions and its subsequent execution;
- sending a trade request manually via the GUI and its subsequent execution;
- activations of pending and stop orders on the server;
- performing operations on the trade server side.
Data on transaction type is contained in the type field of the trans variable. Types of trade transactions are described in the ENUM_TRADE_TRANSACTION_TYPE enumeration:
- TRADE_TRANSACTION_ORDER_ADD – adding a new active order
- TRADE_TRANSACTION_ORDER_UPDATE – changing an existing order
- TRADE_TRANSACTION_ORDER_DELETE – deleting an order from the list of active ones
- TRADE_TRANSACTION_DEAL_ADD – adding a deal to history
- TRADE_TRANSACTION_DEAL_UPDATE – changing a deal in history
- TRADE_TRANSACTION_DEAL_DELETE – deleting a deal from history
- TRADE_TRANSACTION_HISTORY_ADD – adding an order to history as a result of execution or cancelation
- TRADE_TRANSACTION_HISTORY_UPDATE – changing an order in the order history
- TRADE_TRANSACTION_HISTORY_DELETE – deleting an order from the order history
- TRADE_TRANSACTION_POSITION – position change not related to a trade execution
- TRADE_TRANSACTION_REQUEST – notification that a trade request has been processed by the server and the result of its processing has been received.
When handling transactions of TRADE_TRANSACTION_REQUEST type, it is necessary to analyze the second and third parameters of the OnTradeTransaction() function – request and result – to receive additional information.
Sending a buy trade request leads to a chain of trade transactions on a trading account: 1) request is accepted for processing, 2) an appropriate purchase order is created for the account, 3) the order is then executed, 4) the executed order is removed from the list of active ones, 5) adding to the history of orders, 6) the subsequent transaction is added to history and 7) a new position is created. All these stages are trade transactions. The arrival of each such transaction to the terminal is the TradeTransaction event. Priority of these transactions' arrival at the terminal is not guaranteed. Thus, you should not expect that one group of transactions will arrive after another one when developing your trading algorithm.
When transactions are processed by the EA's OnTradeTransaction() handler, the terminal goes on handling the incoming trade transactions. Thus, the trading account status may change at the course of OnTradeTransaction() operation. For example, while an MQL5 program handles adding a new order, it can be executed, deleted from the list of open orders and moved to history. The program is notified of all these events.
Transactions queue length comprises 1024 elements. If OnTradeTransaction() handles yet another transaction for too long, the previous ones can be superseded by new transactions in the queue.
OnTrade() handler is called after the appropriate OnTradeTransaction() calls. In general, there is no exact correlation in the number of OnTrade () and OnTradeTransaction () calls. One OnTrade() call corresponds to one or several OnTradeTransaction calls.
Each Trade event may appear as a result of one or several trade requests. Trade requests are sent to the server using OrderSend() or OrderSendAsync(). Each request can lead to several trade events. You cannot rely on the statement "One request - one Trade event", since the processing of events may be performed in several stages and each operation may change the state of orders, positions and the trade history.
Sample EA with OnTradeTransaction() handler