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()
.
As mentioned before, the MQL4 OrderSend()
function lets you to send orders from your MetaTrader platform to the broker.
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:
EURUSD
, usually in an EA you will use Symbol()
to trade with the current instrument.Digits
. Failure to normalize may cause the order to be rejected.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.
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.
If you want to get news of the most recent updates to our guides or anything else related to Forex trading, you can subscribe to our monthly newsletter.