MQL4 OrderSend Function
Every new trader wanting to code in an MQL4 EA, should be aware of how to use the OrderSend () function to place orders, whether they be market orders or pending stop or limit orders. Learn how to program the EA to place orders in this MQL4 OrderSend Function article.
The OrderSend() function has the following syntax:
For a handy reference, a description of the above parameters can be found in the table below:
Parameters | Description |
---|---|
symbol | Symbol for trading, such as EURUSD. Symbol() represents the currency chart’s pair |
Type | The type of order to place: buy or sell, which can be market, stop or limit There is an integer value that corresponds to each type:
|
Lots | The number of lots to trade. You can specify mini lots (0.1) or micro lots (0.01) if your broker supports micro lots. |
Price | The price at which to open the order. Generally at the Ask for a buy market order, at the Bid for a sell market order. For pending orders it will be at any valid price above or below the current price. |
Slippage | The maximum slippage in points for the order to go through. |
StopLoss | The stop loss price, below the opening price for a buy, and above for a sell. If set to 0, no stop loss used. |
TakeProfit | The take profit price, above the opening price for a buy, and below for a sell. If set to 0, no take profit used. |
Comment | An optional string that will serve as an order comment. Comments are shown under the Trade tab in the Terminal window. To see them in th terminal, you right click on any of the open or closed trades, and in the box that opens, place a check mark beside Comments. They serve as an order identifier. |
MagicNumber | This is an optional integer value that will identify the order as being placed by a specific EA. We recommend using it. |
Expiration | An optional expiration time for pending orders. |
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 (‘ticket’ is the unique number of an order) of the placed order. We can save these order tickets to static variables for later use.
If no order was placed, due to an error condition, the return value wil be -1. This allows us to analyze the error and take appropriate action based on the error code. In order to get information about the reasons for rejection of the trade request, you should use the function GetLastError().
The Market Order
There are three types of orders that can be placed in MetaTrader: market, stop and limit orders. Being the most common, a market order opens a position immediately at the nearest Bid or Ask price.
Here is an example of a buy market order:
Here is an example of a sell market order:
The symbol() function returns the current chart symbol, and most of the time we will be placing orders on the symbol of the current chart. OP_BUY refers to the buy market order, just as OP_SELL would refer to a sell market order. Ask is a predefined variable in MLQ that stores the latest known seller’s price (ask price) of the current symbol.
We generally set the slippage parameter with an external variable (example: extern slippage = 3). The slippage parameter (and corresponding variable) refers to the number of points to allow for price slippage. In other words, it represents the maximum difference in pips for the order to go through. Choosing the right slippage number can be a fine balance: you want to choose a small enough pip value that gives a good price, but at the same time you want to choose a large enough pip value so as not be requoted and miss your price altogether. If you set the slippage to 0, chances are you will be requoted often, and you might miss your price. If you set it to 3 or 4, you will be more likely filled.
The rule for Stoploss and TakeProfit in regards to OB_BUY:
- Ask – StopLoss = you set the StopLoss Below (-) the Ask price
- Ask + TakeProfit = you set the TakeProfit Above (+) the Ask price
The rule for StopLoss and TakeProfit in regards to OB_SELL:
- Bid + StopLoss = you set the StopLoss Above (+) the Bid price
- Bid – TakeProfit = you set the TakeProfit Below (-) the Bid price
For the above rules, we are using external variables for our stop loss and take profit settings, for instance:
extern int StopLoss = 50;
extern int TakeProfit = 100;
In the above example, we want the stop loss to be 50 pips below the Ask for a buy market order, and the take profit to be 100 pips above. However, recently, with the addition of 5 digit brokers, there has been a problem in how the pips for the stop loss and take profit is calculated, as per note below.
In the comment parameter (8th parameter of the OrderSend function) of our example buy and sell orders above, we typed in “EAName” with the idea that you can put the name of your EA in this field. It is thus one way of identifying your EA from the others. In the external MT4 platform, if you look at your terminal that shows your open and closed orders, you can see that the last field displays the comment field. If you do not see it, you can right click anyway on the terminal and put a check mark on Comments. If you are using multiple EAs on the same account, you should give a distinct name for each of your Eas in the comment field, and so when they all start to generate trades, you can differentiate which ones did which, by looking at the comment field in the terminal.
While the comment field helps you to visually differentiate your EAs from each other, the Magic Number that is deployed for its own parameter (9th parameter of the OrderSend function), helps the program differentiate your EAs from each other. In this parameter goes an integer value, such as “1234”. We recommend that you construct an “extern double MagicNumber = 1234” instead, and then place the variable, MagicNumber, into this parameter. The external variable allows you to easily modify the Magic Number. Basically, the Magic Number is a unique number you assign to your orders for the program to distinguish orders opened by your expert advisor and orders that are opened by another expert advisor.