Press "Enter" to skip to content

Metatrader 4 to 5 for programmers

So one of the issues I’ve had with building robots is simply that MetaTrader 4 is 32 bit. This means it doesn’t take advantage of the memory or speed of 64 bit and everything has to be “thunked” up to it. So I thought the upgrade path would be pretty easy, I mean, they are made by the same company, they have the same name, the only difference apart from being 64 bit is that they have a different number at the end.

How wrong can you be?

Instead of maintaining compatibility with legacy MT4 code it’s a completely different world. It has whizzy new functions but sadly it means rewriting your code. I did try out some online converters but they were either gone or didn’t work and to take advantage of the new functionality you really need to do a rewrite.

In my opinion MetaTrader have missed a huge trick by not making MT5 run MT4 code. It means masses of legacy software is just too big to translate (or too  expensive), it means for me that I have to maintain two separate code bases for my MagiTrade indicator and Trading Robot.  Not much fun.

Warning

Some of the tutorials out there simply don’t work and the problem with search engines is that there is a lot of confusion over the MQL4 / MQL5 languages.

Changes in your code.

Make it easy for yourself! Firstly using the trade libraries makes life a hell of a lot easier than trying to use the Position functions.

There are some C libraries included in the MetaTrader 5 package, lets initialise them first…

#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh> 
CPositionInfo posi; 
CTrade trade;
CSymbolInfo symb;

These basically set up your trading libraries and make things a million times easier.

Let’s use the libraries in a routine.

This routine basically closes any trades with a particular symbol and order type.

   void closeTrades(string pair,int direction)
     {
      string ordersymbol;
     // Get the number of positions
 int numberOfTrades=PositionsTotal();
      double price=0;
      ulong myticket=0;

      for(int x=0;x<numberOfTrades;++x)
        {
         // Fill the buffer by loading the buffer with the current trade
         posi.SelectByIndex(x);
         // Get the ticket Number
         myticket=posi.Ticket();
         // Get the Symbol
         ordersymbol=posi.Symbol();
         // See if we need to close the trade
         if(posi.Magic()==magicNumber && pair==posi.Symbol() && direction==posi.PositionType())
           {
            
            // Close the trade using the C function
            trade.PositionClose(myticket);
            // you can use other functions like trade.closePartial - you can see them simply by using autocomplete.  

          
           }

        }

     }


So you can see the same routine written in MQL4 for the old metatrader and how it works, it’s actually more effort in MQL4.

 void closeTrades(string pair,int direction) 
     {
      int numberOfTrades=OrdersTotal();
      double price=0;
      for(int x=0;x<numberOfTrades;++x)
        {
         if(OrderSelect(x,SELECT_BY_POS)==true)
           {
            if(OrderMagicNumber()==magicNumber && pair==OrderSymbol() && direction==OrderType())
              {
               price=MarketInfo(OrderSymbol(),MODE_ASK);
               if(OrderType()==OP_SELL) 
                 {
                  price=MarketInfo(OrderSymbol(),MODE_BID);
                 }
               if(!OrderClose(OrderTicket(),OrderLots(),price,10,clrRed)) 
                 {

                 }

               return;
              }
           }
        }


     }

This should give any programmer what they need to successfully convert their code from MT4 to MT5, there are a whole load of other differences but EA wise this should give you a head start.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    (C)2015-18 UK Bitcoinblog.com/Paul Clevett all rights reserved.