Hi,
I thought that we would start with a function with a bit of meat in it, so this is the OrderSend_() function which replaces the built in Empty4 OrderSend() function.
There are a couple of ways to create orders in MT5 but I think that this is the most transparent way. Another way is to use pre-defined MT5 classes which I think adds another element of complexity to this project and I am trying to keep it as MT4ish as possible.
The example script that calls the function creates two sell orders, one is a market order and the second is a pending order.
MT5 isn’t as straight forward as Empty4 when it comes to placing orders as as they are divided onto separate pools of Orders and Positions. When they go to the broker they become a deal. My goal is to create functions where the Empty4 coder doesn’t need to worry about these different types.
Below is the code for the OrderSend_() function. I have added quite a few comments for those that are interested to read them but it isn't necessary to do that, or to understand the function code at all for that matter. The purpose of this project is to make your Empty4 code work on MT5 without needing to know a lot of the ins and outs of the language differences.
Code: Select all
/*
Send pending and market orders
*/
int OrderSend_(string symbol, ENUM_ORDER_TYPE cmd, double volume, double price, ulong slippage, double stoploss, double takeprofit, string comment=NULL, int magic=0, datetime expiration=0, color arrow_colour=clrNONE)
{
int ticket = -1;
#ifdef __MQL4__
// Nothing needs changing for MQL4
ticket = OrderSend(symbol, cmd, volume, price, (int)slippage, stoploss, takeprofit, comment, magic, expiration, arrow_colour);
#endif
#ifdef __MQL5__
// We need to create structures to send input properties to the server and also to return result values from the server.
MqlTradeRequest request={};
MqlTradeResult result={};
// This is what type of order we want to place. DEAL = Market Order and PENDING = Pending order
ENUM_TRADE_REQUEST_ACTIONS action;
if(cmd <= ORDER_TYPE_SELL)
action = TRADE_ACTION_DEAL; // The equivalent of the Empty4 market order
else
action = TRADE_ACTION_PENDING; // The equivalent of the Empty4 pending order
// Add the properties to the sending structure
request.action = action;
request.symbol = symbol;
request.volume = volume;
request.type = cmd;
request.price = price;
request.deviation = slippage;
request.magic = magic;
request.sl = stoploss;
request.tp = takeprofit;
request.type_filling = ORDER_FILLING_IOC;
request.comment = comment;
if(OrderSend(request, result))
// Retieve the ticket number from the result structure. The ticket number is actually a long type so to avoid a
// compiler warning we explicitly convert it to an int
ticket = (int)result.order;
else
// retcode adds a little more information if there is an error so we might as well report it
Print("OrderSend error: retcode_external = "+IntegerToString(result.retcode_external)+" retcode = "+IntegerToString(result.retcode));
#endif
return ticket;
}
The calling script is probably a bit more interesting. All of the code works in Empty4 and MT5 as is.
I think that the main changes to standard Empty4 code will be the missing predefined variables in MT5. E.G. the following variables are not in MT5
Bid, Ask, Digits and Point.
The code change to get these values is the same in Empty4 and MT5 and is as follows.
Code: Select all
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
// SymbolInfoInteger() actually returns a long but will will it return an int so that the compiler doesn't give a warning
int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
The other "gotcha" is the different properties for the OrderSend() cmd parameter.
OP_BUY, OP_SELL, OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT and OP_SELLSTOP are not in MT5 and need to be replaced with
ORDER_TYPE_BUY, ORDER_TYPE_SELL, ORDER_TYPE_BUY_LIMIT, ORDER_TYPE_SELL_LIMIT,ORDER_TYPE_BUY_STOP and ORDER_TYPE_SELL_STOP and are of type ENUM_ORDER_TYPE.
***** Installation Instructions *****
* Copy all three files to the Empty4 and MT5 Scripts folder.
* For Empty4 compile theOrderSend.MQ4 file
* For MT5 compile theOrderSend.MQ5 file
*****************************************************
If you have any questions or suggestions please let me know.
You do not have the required permissions to view the files attached to this post.