Popular Posts

Editor'S Choice - 2019

MQL4: We write one adviser at once for MT4 and MT5

Greetings, friends!

Many of you probably already faced this inconvenience more than once: they wrote an adviser in mql4, and then I wanted to try playing with MetaTrader 5. Rewriting all the code again is a rather painstaking and lengthy task, and you also have to first deal with the work of the existing one . Writing two different Expert Advisors separately for MT4 and for MT5 is also time-consuming. We will solve this problem today in a new video tutorial.

So what to do? The answer is simple - write one adviser for two terminals at once. After all, for the most part, the mql4 and mql5 languages ​​do not differ so much, many methods and the basic structure of scripts in these languages ​​are similar. If it were possible to write common code in one script, and the different sections could just be somehow rewritten in both languages ​​and forcing the compiler to choose the necessary pieces of code, it would save a lot of time and nerves.

Fortunately, MetaEditor supports preprocessor conditional compilation directives. And today we will look at how this is done using a very simple example.

#ifdef identifier // The code here is compiled if identifier has already been defined for the preprocessor in the #define command. #endif

In the code for MT5, we will replace the variables from MQL4 using the #define directive:

#ifdef __MQL5__ #define OrdersTotal PositionsTotal #endif

In the header of the adviser we place the input variables:

input int Stop = 300; input int Take = 400; input int Slip = 100; input int MN = 123; input double LT = 0.1;

Now you can write methods for buying and selling:

bool Buy (double price, double sl, double tp) {#ifdef __MQL4__ int ticket = OrderSend (Symbol (), 0, LT, price, Slip, sl, tp, "", MN); if (ticket <0) {PrintFormat ("OrderSend error% d", GetLastError ()); } #endif #ifdef __MQL5__ // --- declaration and initialization of the query and result MqlTradeRequest request = {0}; MqlTradeResult result = {0}; // --- request parameters request.action = TRADE_ACTION_DEAL; // type of trade operation request.symbol = Symbol (); // character request.volume = LT; // volume request.type = 0; // type of order request.price = price; // price for opening request.sl = sl; // price StopLoss request.tp = tp; // price TakeProfit request.deviation = Slip; // permissible deviation from the price request.magic = MN; // MagicNumber orders // --- sending a request if (! OrderSend (request, result)) {// if the request could not be sent, print an error code PrintFormat ("OrderSend error% d", GetLastError ()); } // --- information about the operation PrintFormat ("retcode =% u deal =% I64u order =% I64u", result.retcode, result.deal, result.order); #endif // --- return (false); }

The selling method is similar to the buy () method. We will not particularly think about the strategy in this example, we will receive a condition for opening at random and put this code in OnTick ():

int oper = (int) (GetTickCount ()% 2); // the remainder of dividing by two is always either 0 or 1 if (oper == 0) {double price = Ask; double SL = NormalizeDouble (Bid - Stop * _Point, _Digits); double TP = NormalizeDouble (Bid + Take * _Point, _Digits); Buy (Ask, SL, TP); } if (oper == 1) {double price = Bid; double SL = NormalizeDouble (Ask + Stop * _Point, _Digits); double TP = NormalizeDouble (Ask - Take * _Point, _Digits); Sell ​​(Ask, SL, TP); }

Conclusion

That's all. Now you can compile the code in MetaEditor 4 and in the MetaTrader 4 terminal it will work. You can compile the Expert Advisor in MetaEditor 5 and it will work normally in the MetaTrader 5 terminal. Moreover, to change the terminal you do not need to rewrite a single line of code - everything will compile and work fine.

Watch the video: MQL4 Tutorial - Simple Moving Average Crossover Expert Advisor (December 2019).

Leave Your Comment