|
Structure of a Trade Transaction (MqlTradeTransaction)
When performing some definite actions on a trade account, its state changes. Such actions include:
The following trade transactions are performed as a result of these actions:
For example, when sending a market buy order, it is handled, an appropriate buy order is created for the account, the order is then executed and removed from the list of the open ones, then it is added to the orders history, an appropriate deal is added to the history and a new position is created. All these actions are trade transactions.
Special OnTradeTransaction() handler is provided in MQL5 to get trade transactions applied to an account. The first parameter of the handler gets MqlTradeTransaction structure describing trade transactions.
struct MqlTradeTransaction
|
Fields Description
Field |
Description |
---|---|
deal |
Deal ticket. |
order |
Order ticket. |
symbol |
The name of the trading symbol, for which transaction is performed. |
type |
Trade transaction type. The value can be one of ENUM_TRADE_TRANSACTION_TYPE enumeration values. |
order_type |
Trade order type. The value can be one of ENUM_ORDER_TYPE enumeration values. |
order_state |
Trade order state. The value can be one of ENUM_ORDER_STATE enumeration values. |
deal_type |
Deal type. The value can be one of ENUM_DEAL_TYPE enumeration values. |
time_type |
Order type upon expiration. The value can be one of ENUM_ORDER_TYPE_TIME values. |
time_expiration |
Pending order expiration term (for orders of ORDER_TIME_SPECIFIED and ORDER_TIME_SPECIFIED_DAY types). |
price |
Price. Depending on a trade transaction type, it may be a price of an order, a deal or a position. |
price_trigger |
Stop limit order stop (activation) price (ORDER_TYPE_BUY_STOP_LIMIT and ORDER_TYPE_SELL_STOP_LIMIT). |
price_sl |
Stop Loss price. Depending on a trade transaction type, it may relate to an order, a deal or a position. |
price_tp |
Take Profit price. Depending on a trade transaction type, it may relate to an order, a deal or a position. |
volume |
Volume in lots. Depending on a trade transaction type, it may indicate the current volume of an order, a deal or a position. |
position |
The ticket of the position affected by the transaction. |
position_by |
The ticket of the opposite position. Used when closing a position by an opposite one, i.e. by a position of the same symbol that was opened in the opposite direction. |
The essential parameter for received transaction analysis is its type specified in type field. For example, if a transaction is of TRADE_TRANSACTION_REQUEST type (a result of handling a trade request by the server has been received), the structure has only only one field that is filled completely - type. Other fields are not analyzed. In this case, we may analyze two additional request and result parameters submitted to OnTradeTransaction() handler, as shown below.
Having data on a trading operation type, you can decide on the analysis of the current state of orders, positions and deals on a trading account. Remember that one trade request sent to the server from the terminal can generate several new transactions. The priority of their arrival at the terminal is not guaranteed.
MqlTradeTransaction structure is filled in different ways depending on a trade transaction type (ENUM_TRADE_TRANSACTION_TYPE):
TRADE_TRANSACTION_ORDER_* and TRADE_TRANSACTION_HISTORY_*
The following fields in MqlTradeTransaction structure are filled for trade transactions related to open orders handling (TRADE_TRANSACTION_ORDER_ADD, TRADE_TRANSACTION_ORDER_UPDATE and TRADE_TRANSACTION_ORDER_DELETE) and orders history (TRADE_TRANSACTION_HISTORY_ADD, TRADE_TRANSACTION_HISTORY_UPDATE, TRADE_TRANSACTION_HISTORY_DELETE):
TRADE_TRANSACTION_DEAL_*
The following fields in MqlTradeTransaction structure are filled for trade transactions related to deals handling (TRADE_TRANSACTION_DEAL_ADD, TRADE_TRANSACTION_DEAL_UPDATE and TRADE_TRANSACTION_DEAL_DELETE):
TRADE_TRANSACTION_POSITION
The following fields in MqlTradeTransaction structure are filled for trade transactions related to changing the positions not connected with deals execution (TRADE_TRANSACTION_POSITION):
Position change (adding, changing or closing), as a result of a deal execution, does not lead to the occurrence of TRADE_TRANSACTION_POSITION transaction. |
TRADE_TRANSACTION_REQUEST
Only one field in MqlTradeTransaction structure is filled for trade transactions describing the fact that a trade request has been processed by a server and processing result has been received (TRADE_TRANSACTION_REQUEST):
Only type field (trade transaction type) must be analyzed for such transactions. The second and third parameters of OnTradeTransaction function (request and result) must be analyzed for additional data. |
Example:
input int MagicNumber=1234567; |
See also