Trade Functions in MQL4

This post contains affiliate links. If you use these links to register at one of the trusted brokers, I may earn a commission. This helps me to create more free content for you. Thanks!

Trade Functions will be probably the most important part of the code when creating any automated strategy in MQ4. Let’s look at the list of all trade functions and their description:

Execution ErrorsAny trade operation (OrderSendOrderCloseOrderCloseByOrderDelete 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.
OrderCloseIt closes position. It returns TRUE, if the function has ended successfully. It returns FALSE, if the function fails to end.
OrderCloseByIt 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.
OrderClosePriceIt returns the close price of the selected order.
OrderCloseTimeIt returns the time of closing for the selected order.
OrderCommentIt returns the comment of the selected order.
OrderCommissionIt returns the calculated commission value of the selected order.
OrderDeleteIt deletes the previously placed pending order. It returns TRUE, if the function has ended successfully. It returns FALSE, if the function fails to end.
OrderExpirationIt returns the date of expiration of the selected pending order.
OrderLotsIt returns the amount of lots of the selected order.
OrderMagicNumberIt returns the identifying (“magic”) number of the selected order.
OrderModifyIt 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.
OrderOpenPriceIt returns the open price of the selected order.
OrderOpenTimeIt returns the opening time of the selected order.
OrderPrintIt enters the order information to the journal.
OrderProfitIt 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.
OrderSelectThe 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.
OrderSendThe 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.
OrdersHistoryTotalIt returns the number of closed positions and deleted orders in the history of the current account, loaded to the client terminal.
OrderStopLossIt returns close price of the position when it reaches the loss level (stop loss) of the currently selected order.
OrdersTotalIt returns the total number of open and pending orders.
OrderSwapIt returns the swap value of the currently selected order.
OrderSymbolIt returns the symbol name for the currently selected order.
OrderTakeProfitIt returns the close price when it reaches the profit level (take profit) of the currently selected order.
OrderTicketIt returns the ticket number of the currently selected order.
OrderTypeIt returns the operation type of the currently selected order.

The basic application of the most common trade functions can be found in the post: Basics of Trade Management in MQ4.

Still, have no trading account yet? Open an account at one of my trusted brokers suitable for algorithmic trading completely for free and start testing today!

This post contains affiliate links. If you use these links to register at one of the trusted brokers, I may earn a commission. This helps me to create more free content for you. Thanks!