OrderSend()
is a function in MQL4 that allows you to submit orders from MetaTrader to your broker.
When you create an expert advisor, in many cases, probably the majority, you want it to trade for you. We will see in this article how to submit an order using the MQL4 language and the function OrderSend()
.
OrderSend MQL4 Function
As mentioned before, the MQL4 OrderSend()
function lets you to send orders from your MetaTrader platform to the broker.
MQL4 OrderSend Parameters
OrderSend needs several arguments. Here they are in detail:
int OrderSend( string symbol, // symbol int cmd, // operation double volume, // volume double price, // price int slippage, // slippage double stoploss, // stop loss double takeprofit, // take profit string comment=NULL, // comment int magic=0, // magic number datetime expiration=0, // pending order expiration color arrow_color=clrNONE // color );
It is worth to spend some time learning more about some of the arguments, this will allow to avoid future errors. The required parameters are in bold:
- symbol is simply the instrument that you want to trade, for example
EURUSD
, usually in an EA you will useSymbol()
to trade with the current instrument. - cmd is the type of order to submit and can be OP_BUY or OP_SELL for market buy and sell orders, OP_BUYLIMIT, OP_SELLLIMIT, OP_BUYSTOP, OP_SELLSTOP are also available for limit and stop orders.
- volume is the position size calculated in lots.
- price, stoploss, and takeprofit are indeed the prices you want to open, set stop-loss and take-profit. With these prices it is important to remember that the open price must be the current price (unless you are submitting a pending order) and that stop loss and take profit cannot be too close to the open price or the order won't be accepted. These prices must also be normalized to the number of decimals accepted by the broker, which is identifiable with the native variable
Digits
. Failure to normalize may cause the order to be rejected. - slippage is the acceptable difference between the open price requested by the client and the actual price on the server. For example, a slippage of 3 pips allows an order submitted at a price of 1.1980 to be accepted if the price on the server differs of max 3 pips, so between 1.1983 and 1.1977.
- comment can be a comment or a note on the order
- magic is a number that you can assign to the order to identify it, for example you want to use the same magic number for all the orders opened by a specific EA in a currency pair. This will allow you to identify between all your orders which ones are executed by the EA.
- expiration is the date and time to keep a pending order valid. When the order expires, it will be removed from the execution queue.
- arrow_color is the color of the arrow to draw on the chart when the order is executed.
OrderSend()
is defined as an integer function meaning it return an int
value. This value is the ticket number for the order if it is submitted successfully or -1 if the order submission fails. You can use GetLastError()
to see what went wrong.
MQL4 OrderSend Example
Let's look at this simple example with a script. In the script, we also include a function to normalize the digits for all three price levels: entry, stop-loss, and take-profit.
#property copyright "EarnForex.com" #property link "https://www.earnforex.com" #property version "1.00" #property strict // We define the basic data. double StopLoss=30; // Stop-Loss in pips double TakeProfit=50; // Take-Profit in pips double LotSize=0.05; // Lot size in lots double Slippage=5; // Slippage in pips double Command=OP_BUY; // Type of order, BUY double CalculateNormalizedDigits() { if(Digits<=3){ return(0.01); } else if(Digits>=4){ return(0.0001); } else return(0); } //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { // The data is prepared to be used in the OrderSend function, // so we calculate the SL and TP prices and normalize them. // We also normalize the Slippage in case the decimals are 3 or 5. double nDigits=CalculateNormalizedDigits(); double OpenPrice=NormalizeDouble(Ask,Digits); double StopLossPrice=NormalizeDouble(Ask-StopLoss*nDigits,Digits); double TakeProfitPrice=NormalizeDouble(Ask+TakeProfit*nDigits,Digits); if(Digits==3 || Digits==5){ Slippage=Slippage*10; } // We define a variable to store and store the result of the function. int OrderNumber; OrderNumber=OrderSend(Symbol(),Command,LotSize,OpenPrice,Slippage,StopLossPrice,TakeProfitPrice); // We verify if the order has gone through or not and print the result. if(OrderNumber>0){ Print("Order ",OrderNumber," open"); } else{ Print("Order failed with error - ",GetLastError()); } }
Running the script on the EUR/USD chart results in the following output:
This guide is only an introduction about how to submit an order with MetaTrader and MQL4 OrderSend()
function.
When you create your own expert advisor, you will realize that there is more to be considered and to be checked before submitting an order.
You can use our MT4 EA Template that has most of the code required to create an EA along with some functional examples.