Opening a Trade Using OrderSend
In the last module, we went over the basic Expert Advisor and how to create input parameters. If you’d like to review that module, click here: The Expert Advisor.
Today we are going to do something more useful with an Expert Advisor: open a trade. The MetaTrader function used to open a trade is called “OrderSend”. This table details each of the parameters required to set when using the function.
|The symbol of the currency.
|The type of order to open. See the table below.
|The number of lots to open.
|The preferred open price.
|The number of points the preferred open price may slip.
|The stoploss set in the order on the broker’s server. This value must be calculated using the price and the currency point value.
|The takeprofit set in the order on the broker’s server. This value must be calculated using the price and the currency point value.
|A text comment that can be seen from within the MetaTrader platform when viewing open orders.
|A unique number that can be used to identify the trade.
|An expiration time – only used for pending orders.
|The color of the arrow that will appear on the price chart when the OrderSend function is successful.
Most of these parameters are pretty straight forward, but a few require a bit of an explanation.
The “symbol” parameter is the name of the currency pair. You can use the MT function “Symbol()” which will automatically use the currency of the chart that the EA is running on. Or you can even define any name in quotes, for example “EURUSD”. (Note: Opening a currency different than the one that the EA is running requires some special techniques which I’ll cover in a later module.)
The “cmd” parameter is the command that tells the broker’s server what you want to do. The choices for a market order are OP_BUY or OP_SELL – open a Buy or Sell trade.
The “volume” parameter is the number of lots you’d like to open or close.
The “price” parameter is the preferred price. On MT, I’ve found this value should be set to the “Ask” price for opening Buy orders and the “Bid” price for opening Sell orders. Any other values seem to cause the function to fail.
The “slippage” parameter is how many points you’ll allow the trade to slip and still complete the order. For example, suppose you’d like to buy the EURUSD at 1.3595 and the slippage is set to 3 – you might pay as much as 1.3595+3=1.3598. (This may look bad, but if you set your slippage too small you may not get the trade at all.)
The “stoploss” parameter is the limit for how far the trade can go against you before the broker’s server closes the position. (Note: Your EA does not need to be running for the stoploss to work. This is a value stored on the broker’s system.) This value will be lower than your open price for Buy orders and greater than the open price for Sell orders.
Both the stoploss and takeprofit parameters must be in exact terms of the price, so you need to do some math here. For example, if you’d like a stoploss of 15 points, you need to subtract 15 from the open price (for an open price of 1.3595):
stoploss = 1.3595 – 0.0015 = 1.3580
Fortunately, there is an easy way to get the 0.0015 number using the “Point” function: 15 * Point = 0.0015
This way, you can work with a number like 15 instead of 0.0015.
(I personally think the OrderSend function could have been written to simply accept a relative value like 15 for the stoploss instead of requiring an absolute price value like 1.3580).
The “takeprofit” parameter is the limit of how far a trade can go in your direction before it closes for a profit. This value will be higher than your open price for Buy orders and less than the open price for Sell orders. The rest of this parameter is just like the stoploss.
The “comment” parameter can be any text. It is saved with the trade and appears in the comment window in the trading terminal. I usually put the name of the EA in this field so when I look at my open trades I can see what EA has opened each.
The “magic” parameter is a number that you can assign to the trade so that you can identify it later. We’ll see exactly how in a later module on the OrderSelect function.
The “expiration” parameter holds an expiration time for pending orders. It has no meaning for market orders. (In my experience it does not work for pending orders either.)
The “arrow_color” parameter allows you to choose the color of the arrow that is drawn on the chart when the trade is opened.
Now that we have described all of the input parameters, I’ll make a quick note about the “return value”. The OrderSend function will return a value back after it has finished running. In MQL, like most languages, a negative number is a bad thing and zero or a positive number is usually a good thing – indicating the function executed successfully.
We’ll capture and check the value return from the OrderSend function so we’ll know if it worked and actually opened a trade successfully. Note: All of the information about the OrderSend function is available from within the MetaEditor. The help file is very useful. Even as an experienced MQL programmer, I refer to the help often.
(From within the MetaEditor, select View->Navigator and then use the search option to find your information.) So, let’s see what the MQL code looks like.
Here is the MQL programming code for the OrderSend function.
// the stoploss
extern int stoploss=200;
// the takeprofit
extern int takeprofit=200;
// the number of lots
extern double lots = 1.0;
// this function will be called each time new price data arrives from the broker
int status =
OrderSend( Symbol(), // the synbol for this chart
OP_BUY, // a buy order
lots, // number of lots
Ask, // use the ask price for a BUY
3, // allow the price up to move 3 points
Ask – (stoploss*Point), // stop
Bid + (takeprofit*Point), // limit
“My Simple EA”, // comment to see in TerminalCompany
8675309, // a unique # to id this trade
0, // expiration, doesn’t work
Blue // a blue arrow
if( status < 0 )
Comment(“OrderSend Failed!! Error=”, GetLastError());
Here are some common error codes returned from the OrderSend function that you may come across as you develop your EA’s:
ERR_INVALID_PRICE – usually the wrong preferred price: For Buy, use Ask, for , use Bid.
ERR_INVALID_STOPS – usually bad math with the stoploss or takeprofit values, or they are zero and the broker will not accept zero.
ERR_INVALID_TRADE_VOLUME – the lot value is invalid, usually a problem with mini or micro lots. If you have trouble, try setting this to 1.0 to see if the error goes away.
ERR_NOT_ENOUGH_MONEY – self explanatory, if you are testing using a demo account this can happen often – just open another demo account.
This wraps up our lesson on opening a trade with an Expert Advisor. (To open a Sell trade, change the “cmd” parameter to OP_SELL and reverse the math on the stoploss and takeprofit.)
This limited amount of information is dangerous, so be careful with this code and please, only run this on a demo account!
Next module we’ll learn how to use technical indicators!