The process of placing an order in MQL involves several steps. We must determine the following before placing the order:
- The type of order to be placed – buy or sell; stop, market or limit.
- The currency pair to trade – generally the chart that the EA is attached to.
- The lot size. This can either be a fixed lot size, or one that is calculated using a money management routine.
- The order opening price. For market orders, this will be the current Bid or Ask price. For pending orders, the opening price must be a minimum distance from the current price, andshould be above or below the current price as required by the order type.
- The stop loss price. The stop loss can be a predetermined price, an indicator value, a fixed number of pips from the order opening price, or it can be dynamically calculated using a risk management routine. The stop loss can be placed with the order, or it can be added to the order afterward.
- The take profit price. This is generally a fixed number of pips from the order opening price,although it can be calculated using other methods as well. The take profit can be placed with the order, or it can be added to the order afterward.
- Order identifiers such as an order comment, or a “magic number” that identifies an order as being placed by a specific expert advisor.•An optional expiration price for pending orders, if the broker supports it.
The OrderSend() function is used to place orders in MQL. The syntax is as follows:
int OrderSend(stringSymbol, intType, doubleLots, doublePrice,intSlippage, doubleStopLoss, doubleTakeProfit, stringComment= NULL,intMagicNumber= 0, datetimeExpiration= 0, colorArrow= CLR_NONE);
Let’s go over the arguments one by one:
Symbol– A string representing the currency pair to trade, for example GBPUSD. The Symbol() function is used for the current chart’s currency pair.
Type– The type of order to place: buy or sell; market, stop or limit. This is an integer value,represented by the following constants:
- OP_BUY – Buy market order (integer value 0).
- OP_SELL – Sell market order (integer value 1).
- OP_BUYSTOP – Buy stop order (integer value 2).
- OP_SELLSTOP – Sell stop order (integer value 3).
- OP_BUYLIMIT – Buy limit order (integer value 4).
- OP_SELLLIMIT – Sell limit order (integer value 5).
Lots– The number of lots to trade. You can specify mini lots (0.1) or micro lots (0.01) if your broker supports it.
Price– The price at which to open the order. For a buy market order, this will be the Ask. For a sell market order, this will be theBid. For pending orders, this will be any valid price that is above or below the current price.
Slippage– The maximum slippage in points. Use a sufficiently large setting when auto trading. Brokers that do not use slippage will ignore this parameter.
StopLoss– The stop loss price. For a buy order, the stop loss price is below the order opening price, and for a sell order, above. If set to 0, no stop loss will be used.
TakeProfit– The take profit price. For a buy order, the take profit is above the order opening price, and for a sell order, below. If set to 0, no take profit will be used.
Comment– An optional string that will serve as an order comment. Comments are shown under the Trade tab in the Terminal window. Order comments can also be used as an order identifier.
MagicNumber– An optional integer value that will identify the order as being placed by aspecific expert advisor. It is highly recommended that you use this.
Expiration– An optional expiration time for pending orders. Not all brokers accept trade expiration times – for these brokers, an error will result if an expiration time is specified.
Arrow– An optional color for the arrow that will be drawn on the chart, indicating the opening price and time. If no color is specified, the arrow will not be drawn.
The OrderSend() function returns the ticket number of the order that was just placed. If no order was placed, due to an error condition, the return value will be -1. We can save the order ticket to a global or static variable for later use. If the order was not placed due to an error condition, we can analyze the error and take appropriate action based on the returned error code.
Next let’s look at an example of a buy market order.
Placing A Market Order
Here’s an example of a buy market order. We’ll assume that the variablesLotSize, Slippage, BuyStopLoss, BuyTakeProfit and MagicNumber have already been calculated or assigned and are valid.
OrderSend(Symbol(), OP_BUY, LotSize, Ask, Slippage, BuyStopLoss, BuyTakeProfit, "Buy Order", MagicNumber, 0, Green);
Symbol() function returns the current chart symbol. We will be placing orders on the current chart pair 99% of the time. OP_BUY indicates that this is a buy market order.Ask is a predefined variable in MQL that stores the most recent Ask quote. (Remember that buy orders open at the Ask price!)
Slippage is set using an external variable. The slippage parameter is an integer, indicating the number of points to allow for price slippage. If your broker uses 4 digit quotes (2 for Yen pairs), 1 point would be equal to 1 pip. If your broker offers 3 and 5 digit quotes however, then 1 point would be 0.1 pips. In this case, you’d need to add an additional zero to the end of your Slippage setting.
We’ve added the generic comment “Buy Order” to this order. Since there is no expiration for market orders, the Expiration parameter is 0. Finally, we specify the color constant Green to draw a green arrow on the chart.
Here is an example of a sell market order, using the same parameters as above:
OrderSend(Symbol(), OP_SELL, LotSize, Bid,Slippage, SellStopLoss, SellTakeProfit, "Sell Order",MagicNumber,0,Red);
OP_SELL as the order type, to specify a sell market order. We use Bid as the order opening price, to reflect the fact that sell orders open at the Bid price.“Sell Order” is our order comment and we use Red as the arrow color to differentiate from buy orders.