Login
Sign Up
OR
Forgotten Password
Login
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
English
中文
日本語
ID
Vietnam
한국어
Filipino
   Academy Menu

MQL4 OrderSend Function

If you need free forex signals or any assistance regarding forex, contact Tg:Joanne0fx

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:

int OrderSend (string Symbol, int Type, double Lots, double Price, int Slippage, double StopLoss, double TakeProfit, string Comment = NULL, int MagicNumber = 0, datetime Expiration = 0, col Arrow = CLR_NONE);

For a handy reference, a description of the above parameters can be found in the table below:

ParametersDescription
symbolSymbol for trading, such as EURUSD. Symbol() represents the currency chart’s pair
TypeThe 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:
  • 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)

LotsThe number of lots to trade. You can specify mini lots (0.1) or micro lots (0.01) if your broker supports micro lots.
PriceThe 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.
SlippageThe maximum slippage in points for the order to go through.
StopLossThe stop loss price, below the opening price for a buy, and above for a sell. If set to 0, no stop loss used.
TakeProfitThe take profit price, above the opening price for a buy, and below for a sell. If set to 0, no take profit used.
CommentAn 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.
MagicNumberThis is an optional integer value that will identify the order as being placed by a specific EA. We recommend using it.
ExpirationAn optional expiration time for pending orders.
ArrowAn 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.

Note on Bid and Ask

The Bid price is what you see on the MT4 charts, and the Ask price is just a few pips above the bid price, with the difference between them being the spread. We open buy orders and close sell orders on the Ask price; we open sell orders and close buy orders on the Bid price.

Here is an example of a buy market order:

OrderSend (Symbol(), OP_BUY, Lots, Ask, Slippage, Bid-StopLoss *Point, Bid+TakeProfit*Point, “EAName”, MagicNumber, 0, Blue)

Here is an example of a sell market order:

OrderSend (Symbol(), OP_SELL, Lots, Bid, Slippage, Ask+StopLoss *Point, Ask-TakeProfit*Point, “EAName”, MagicNumber, 0, Blue)

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.

Note on 5 digit brokers regarding Slippage

If your broker uses 4 digits quotes (or 2 for Yen pairs), 1 point = 1 pip; however, if your broker uses 5 digit quotes (or 3 for Yen pairs), then 1 point =0.1 pips, in which case you would need to add an additional zero the end of your Slippage setting. It is thus useful for the code to auto-check for 4 or 5 digit brokers and make the appropriate adjustments.

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.

Note on 5 Digit Brokers Regarding Point Value

In order to convert the above integers into their appropriate fractional value, we need to multiply our external StopLoss and TakeProfit variable by the Point. Point is a predefined variable in MQL that returns the smallest price unit of a currency, depending on the number of decimal places, making a Point = 0.001 for 4 decimal quotes. But recently, many brokers have adopted fractional pip price quotes, with 3 and 5 decimal places, making a Point = 0.00001 for 5 decimal quotes. The problem in this case is that the above Stoploss would be calculated as 5 pips from opening price instead of 50 pips. That is not what we want. It is thus useful for the code to auto-check for 4 or 5 digit brokers and make the appropriate adjustments.

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.

Note on Magic Numbers

Just indicating a unique integer value in the Magic Number parameter field is not enough, by itself, for your program to differentiate open orders. Your magic number must also be included with the OrderSelect() function and OrderMagicNumber() function combination,whenever your code tries to reference your open trades.

The combination looks like this:

int total = OrdersTotal();
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == MagicNumber)

A good example of this combination code in context is to look at the custom closed function.

CONTINUE TO SITE