- Date Type Structure
- Indicator Parameter Structure
- History Data Structure
- Order Book Structure
- Trade Request Structure
- Request Check Result Structure
- Trade Request Result Structure
- Trade Transaction Structure
- Price Data Structure
- Economic Сalendar structures
Structure of a Trade Transaction (MqlTradeTransaction)
When performing some definite actions on a trade account, its state changes. Such actions include:
- Sending a trade request from any MQL5 application in the client terminal using OrderSend and OrderSendAsync functions and its further execution;
- Sending a trade request via the terminal graphical interface and its further execution;
- Pending orders and stop orders activation on the server;
- Performing operations on a trade server side.
The following trade transactions are performed as a result of these actions:
- handling a trade request;
- changing open orders;
- changing orders history;
- changing deals history;
- changing positions.
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):
- order - order ticket;
- symbol - order symbol name;
- type - trade transaction type;
- order_type - order type;
- orders_state - order current state;
- time_type - order expiration type;
- time_expiration - order expiration time (for orders having ORDER_TIME_SPECIFIED and ORDER_TIME_SPECIFIED_DAY expiration types);
- price - order price specified by a client;
- price_trigger - stop limit order stop price (only for ORDER_TYPE_BUY_STOP_LIMIT and ORDER_TYPE_SELL_STOP_LIMIT);
- price_sl - Stop Loss order price (filled, if specified in the order);
- price_tp - Take Profit order price (filled, if specified in the order);
- volume - order current volume (unfilled). Initial order volume can be found in the orders history using HistoryOrders* function.
- position - the ticket of the position that was opened, modified or closed as a result of order execution. It is only filled for market orders, not filled for TRADE_TRANSACTION_ORDER_ADD.
- position_by - the ticket of the opposite position. It is only filled for the close by orders (to close a position by an opposite one).
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):
- deal - deal ticket;
- order - order ticket, based on which a deal has been performed;
- symbol - deal symbol name;
- type - trade transaction type;
- deal_type - deal type;
- price - deal price;
- price_sl - Stop Loss price (filled, if specified in the order, based on which a deal has been performed);
- price_tp - Take Profit price (filled, if specified in the order, based on which a deal has been performed);
- volume - deal volume in lots.
- position - the ticket of the position that was opened, modified or closed as a result of deal execution.
- position_by - the ticket of the opposite position. It is only filled for the out by deals (closing a position by an opposite one).
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):
- symbol - position symbol name;
- type - trade transaction type;
- deal_type - position type (DEAL_TYPE_BUY or DEAL_TYPE_SELL);
- price - weighted average position open price;
- price_sl - Stop Loss price;
- price_tp - Take Profit price;
- volume - position volume in lots, if it has been changed.
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):
- type - trade transaction type;
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