MQL4 Book  Standard Functions  Trade Functions

Trade Functions


All trade functions can be divided into two groups - functions that form trade orders and functions that return some order characterizing values. MQL4 has only five functions that form and send trade orders to a server:

  • OrderSend() - market order opening and pending order placing;
  • OrderClose() - market order closing;
  • OrderCloseBy() - closing of opposite market orders;
  • OrderDelete() - deleting pending orders;
  • OrderModify() - modifying all types of orders.

The order of using functions listed above is described in the Programming of Trade Operations section in details. All the other functions do not form trade orders but their usage is often necessary. For example, it is sometimes necessary to close orders in some priority sequence. To do it, you should analyze the characteristics of every order in the program, namely - order type, lots amount, stop-orders location, etc. Let's examine some functions that allow to get the information about an order.

OrderTotal() Function

int OrdersTotal()

The function returns the total number of opened and pending orders.

OrderTakeProfit() Function

double OrderTakeProfit()

The function returns the value of the declared price when the profit level (take profit) of the current selected order is reached. The order must be previously selected using the OrderSelect() function.

OrderProfit() Function

double OrderProfit()

Returns the net profit value (without regard to swaps and commissions) of the selected order. It is the unrealized profit for the opened orders and fixed profit for the closed orders. The order must be previously selected using the OrderSelect() function.

OrderLots() Function

double OrderLots()

Returns the amount of lots of a selected order. The order must be previously selected using the OrderSelect() function.

The fragment of the program that calculates declared close price TakeProfit, order profit and the amount of lots is shown below:

for (int i=0; i<OrdersTotal(); i++)         // For all orders
{
if((OrderSelect(i,SELECT_BY_POS)==true) // If next exists
{
double ТР = OrderTakeProfit(); // TakeProfit of order
double Profit= OrderProfit(); // Order profit
double Lots = OrderLots(); // Amount of lots
//......TP and profit values usage in the program.....
}
}
// End of the cycle body

It is clear that every considered function (OrderTakeProfit (), OrderProfit() и OrderLots()) does not have any adjustable parameters, i.e. denotation of, for example, number of the order, to return the value corresponding with the characteristics of this individual order is not involved.

To calculate the characteristics of an individual order (declared price of one of the stop-orders, order profit and amount of lots in this context) you should select the necessary order first; this will inform the program about the order to perform calculations with. In order to do it you should execute the OrderSelect() function before starting the calculations (see Closing and Deleting Orders). The trade functions executed after that will return the values that correspond with the selected order characteristics.

The correct evaluation of one or another order characteristics by the programmer is no little significant. For example, when solving the problem of order closing sequence, you should set a calculation criterion for which order should be closed earlier and which one - afterwards. Let's take a look to the simple task.


Problem 40. Two Buy orders are currently opened on a single symbol. The first one is opened at the price of 1.2000 at 0.5 lot, the second one is opened at the price of 1.3000 at 1 lot. The current price is 1.3008. The trade criterion for Buy orders closing has triggered. It is necessary to make a right decision, namely to decide which order should be closed as the first and which one as the second.

Obviously, the profit from the first order makes 108 points, whereas that of the second one is 8 points. Although the first order is opened at a smaller amount of lots, it has the larger profit than the second one, namely the profit of the first order is $540 and the profit of the second order is $80. Closing the first order may seem to be preferable, at the first sight, because it has greater profit. However, it is a misthought. It is necessary to examine the possible case scenario to make a correct decision.

The order closing sequence would not matter, if the price were known not to change during the period of orders closing. However, the price may change during the execution of the instruction to close one of the orders. So the order that can bring more loss, at a negative scenario, should be closed first. If the price sinks one point down, the profit of the first order will decrease by $5, whereas that of the second one will do by $10. Obviously, the second order would bring more loss, so it should be closed first. In such a way, the amount of lots has the dominant significance when deciding about order closing sequence. Profitable case scenario cannot be considered here, because trading develops with the trade criteria in the program, and this time the criterion of closing Buy orders has triggered.

You should consider the other order characteristics if it is necessary to choose between two orders with the same amount of lots. For example, you can consider the distance between the current price and the StopLoss value of each order. At the same time, you should analyze which of the orders would bring more loss, in case of fast price moving. The answer is obvious, as well: the one (from both orders that are opened at the same amount of lots) that has its StopLoss level further from the current price.

Thus you can analyze the priority and all the other parameters of orders and compile the priority-oriented list of criteria to consider with when making the decision about closing orders. It is not difficult to identify the criteria that should not be considered. It is open price (and the related profit from order), for example. The amount of money that the trader have at the moment is shown in the Equity column of the client terminal. The source of this value is not important at that, neither it is a result of loss from one ore more orders, nor it is a result of a profit.

All the necessary characteristics of an order can be received using the corresponding trade functions:

Trade Functions


Function Summary Info
Execution Errors Any trade operation (OrderSend, OrderClose, OrderCloseBy, OrderDelete or OrderModify functions) can unsuccessfully end for a score of reasons and return either the negative ticket number or FALSE. You can find out the reason of failure by using the GetLastError function. Every error should be processed in its own way. The table below contains the general recommendations.
OrderClose It closes position. It returns TRUE, if the function has ended successfully. It returns FALSE, if the function fails to end.
OrderCloseBy It closes one open position with the other that is opened in the opposite direction for the same symbol. It returns TRUE. if the function has ended successfully. It returns FALSE, if the function fails to end.
OrderClosePrice It returns the close price of the selected order.
OrderCloseTime It returns the time of closing for the selected order.
OrderComment It returns the comment of the selected order.
OrderCommission It returns the calculated commission value of the selected order.
OrderDelete It deletes the previously placed pending order. It returns TRUE, if the function has ended successfully. It returns FALSE, if the function fails to end.
OrderExpiration It returns the date of expiration of the selected pending order.
OrderLots It returns the amount of lots of the selected order.
OrderMagicNumber It returns the identifying ("magic") number of the selected order.
OrderModify It modifies the parameters of the previously opened orders and pending orders. It returns TRUE if the function has ended successfully. It returns FALSE, if the function fails to end.
OrderOpenPrice It returns the open price of the selected order.
OrderOpenTime It returns the opening time of the selected order.
OrderPrint It enters the order information to the journal.
OrderProfit It returns the net profit (without regard to swaps and commissions) of the selected order. It is the unrealized profit for the opened orders and fixed profit for the closed orders.
OrderSelect The function chooses the order to work with subsequently. It returns TRUE if the function has ended successfully. It returns FALSE, if the function fails to end.
OrderSend The main function for opening orders and placing pending orders. It returns the number of the ticket that was assigned to the order by the trade server, or -1, in case of failing to end the operation.
OrdersHistoryTotal It returns the number of closed positions and deleted orders in the history of the current account, loaded to the client terminal.
OrderStopLoss It returns close price of the position when it reaches the loss level (stop loss) of the currently selected order.
OrdersTotal It returns the total number of open and pending orders.
OrderSwap It returns the swap value of the currently selected order.
OrderSymbol It returns the symbol name for the currently selected order.
OrderTakeProfit It returns the close price when it reaches the profit level (take profit) of the currently selected order.
OrderTicket It returns the ticket number of the currently selected order.
OrderType It returns the operation type of the currently selected order.

To get the detailed description of this and other functions, you should refer to the Documentation at MQL4.community, at MetaQuotes Ltd. website or at the "Help" section of the MetaEditor.