MQL: Orders, Trades and Positions - The #1 Blog on trading, personal investing! Best Tips for Beginners

Header Ads

MQL: Orders, Trades and Positions


Let us start with orders.
  • Orders are trade requests accepted by the trade server. If the request is invalid, it will be rejected.
    To avoid the difficulty of filling in the trade request, I will later show you how this can be done using the standard libraries making it all much easier.
    There are 2 types of orders: market (for immediate execution) and pending.
Market orders represent instructions to sell or buy a certain amount of a specified financial instrument at the current market price.
Pending orders represent instructions to execute the trade subject to certain conditions. Pending orders have a certain expiration time upon which they are deleted.
  • Trades represent the results of the execution of orders (instructions to execute a trade). Every trade is based on a certain single order, whereas a single order can result in multiple trades. For example, an order to buy 10 lots can be executed by a partial execution of a number of consecutive trades. Trades are always stored in the trade history and cannot be modified. The terminal displays trades in the "History" tab.
  • Positions represent the outcome of orders in action. Only one position, either Long or Short, can be opened for each single symbol.
To make it clearer, let me illustrate it with an example: we open a long position of 1 lot, i.e. we place an order at the current market price (for instance) and the size of 1 lot. If the request is valid, it will be sent to the server for processing. As soon as the processing is complete, a position with the order parameters will appear in the "Trade" tab of the terminal. Assume, we then decide to open another long position, also sized at 1 lot. Following the processing of the order, we will not see two orders in the "Trade" tab but rather one position sized at 2 lots. I.e. the position is the outcome of execution of a number of orders.
Let us now proceed to practice. The following structure fields need to be filled in, in order to make a request:
struct MqlTradeRequest
{
ENUM_TRADE_REQUEST_ACTIONS action; // Type of action
ulong magic; // Expert Advisor ID (magic number)
ulong order; // Order ticket
string symbol; // Trade instrument
double volume; // Requested trade size in lots
double price; // Price 
double stoplimit; // StopLimit level of the order
double sl; // Stop Loss level of the order
double tp; // Take Profit level of the order
ulong deviation; // Maximum allowed deviation from the requested price
ENUM_ORDER_TYPE type; // Order type
ENUM_ORDER_TYPE_FILLING type_filling; // Order type by execution
ENUM_ORDER_TYPE_TIME type_time; // Order type by duration
datetime expiration; // Order expiration time (for orders of the ORDER_TIME_SPECIFIED type)
string comment; // Comment to the order
};
Since there are various orders, each order type has its own set of mandatory parameters. I will not address these fields at length. The website offers plenty of information on this matter. If even one of the mandatory parameters for a certain order type is not specified or specified incorrectly, the request will fail.
The above structure is laid out here only to better demonstrate the difficulty arising when filling it in.