Every new trader wanting to code in MQL4 should be aware of how to use the OrderSend () function to place orders, whether they be market orders or pending stop or limit orders.
The OrderSend() function has the following syntax:
For a handy reference, a description of the above parameters can be found in the table below:
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().
Note on Bid and AskThe 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:
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.
Note on 5 digit brokers regarding SlippageIf 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:
The rule for StopLoss and TakeProfit in regards to OB_SELL:
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 ValueIn 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 NumbersJust 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++)
if (OrderMagicNumber() == MagicNumber)
A good example of this combination code in context is to look at the custom closed function