MQL4 Book  Programming of Trade Operations

Programming of Trade Operations


When programming trade operations, you should consider the requirements and limitations related to the characteristics of orders and rules accepted in your dealing center, as well as the special features of trade order execution technology. The section provides the detailed description of the order of performing trades and contains a lot of examples that explain the purposes of all trade functions used to form trade orders. The section contains some ready-made scripts intended for restricted application.

  • Common Way of Making Trades
    A trader or an MQL4 program can only order to make trades, whereas the trades as such are registered on the trade server. The intermediary between the trade server and a program is the client terminal. Incorrect orders will be rejected immediately in the client terminal, so you have to obtain an insight into the general order of making trades.
  • Order Characteristics and Rules for Making Trades
    The trade instructions are given using trade orders. In the order, you should specify multiple parameters, one part of which is determined by the current prices and the direction of trade, another part depends on the symbol of the trade. Orders that are delivered in the trade server will be checked in the real-time mode for their compliance with the current situation and with the account state. This is why the knowledge of rules for making trades is necessary.
  • Opening and Placing Orders
    The most important trading function is OrderSend(). It is this function that is used to send requests to the trade server to open a market order or to place a pending one. You can immediately specify the necessary values of StopLoss and TakeProfit. The incorrect values of these parameters, as well as of open price and volume of the order, may result in errors. It is important to process these errors properly. Function MarketInfo() allows you to minimize the amount of such errors.
  • Closing and Deleting Orders. Function OrderSelect
    Market orders can be closed using the function OrderClose(), while pending orders can be deleted with the function OrderDelete(). When sending a request to close or delete an order, you should specify the ticket of this order. We will select the necessary order using the function OrderSelect(). Besides, if there are two opposite orders for a symbol, you can close them simultaneously, one by another, using the function OrderCloseBy(). When executing such trade, you will save one spread.
  • Modification of Orders
    The levels TakeProfit and StopLoss can be modified using the function OrderModify(). For pending orders, you can also change the level of triggering. You cannot modify the volume of pending orders. Modification of market and pending orders will also put certain requirements related to the correctness of this trade operation. It is highly recommended, if you make a trade, that you process the results of this trade, handle the errors.

What's new in MQL5

Added the functions for working with orders, deals and positions to conduct trading operations. An order is a request to buy or sell. The order execution leads to a deal resulting in opening, modifying or closing a position.

If you develop trading robots, pay attention to the new OrderSendAsync function intended for asynchronous trading operations. This function is executed in less than 1 millisecond (it does not wait till the trade server responds to a sent request) and returns control immediately. Low network costs, high Depth of Market refresh rates and asynchronous order sending accelerate trading operations dozens of times. For intraday systems, the speed of trading operations can be a key factor.