How to convert MT4 Expert Advisor into MT5 format | Forex Robot

Posted by Fx Online Expert on

 

How to convert MT4 Expert Advisor into MT5 format | BUY Forex EA It seems that Metaquotes Inc, the company behind the revolutionary MetaTrader series, has finally decided to make the MT5 its software for the future. The company has finally hinted that even though its breakthrough MetaTrader 4 platform (MT4) would continue to run, it would be ending any further software updates for the MT4. MetaQuotes Inc. would henceforth be focusing all its efforts on the MetaTrader 5 platform (MT5).

This announcement was made by Anthony Papaevagorou, head of sales at MetaQuotes, at the Finance Magnates London Summit. Since its introduction in 2010, sales of the MT5 platform have been dwarfed by the MT4 … until now. Since July 2016, sales of the MT5 have been steadily outstripping that of the MT4. Metaquotes, therefore, wants to build on this new-found success of its latest platform and intends to focus exclusively on growing the demand and usage of the MT4 among its clients.

This development as announced by a company insider is going to change the landscape of MetaTrader usage as from 2017. It is important for all users of this platform, traders and programmers alike, to start getting in tune with what the new MT5 platform offers. One of the attractions of the MetaTrader platforms is the ability to run expert advisors and indicators, allowing traders to trade on autopilot. This has also created a market for programmers who work to create this software using the inbuilt programming languages.

The biggest question which confronts traders and programmers alike with this planned shift is this: how can experts coded in MQL4 language (the language on which the MT4 runs) be made to run on the MT5 platform, seeing that the MT4 and MT5 do not run on

This question is made more pertinent by the following facts:

  • The MT4 and MT5 do not run using the same programming languages. Therefore, experts that are coded to work on the MT4 will not work on the MT5.
  • Metaquotes has made a significant number of changes to the functionality of the MT5, giving it functional advantages over the MT4.
  • With the latest announcement regarding the end of future updates to the MT4, the software seems destined to become a relic of history in the years to come.

There is still a significant amount of time within which brokers, traders, codebase users, and programmers would have to migrate fully from the MT4 to the MT5, but it is pertinent to address the biggest issues right now, which is on how to make MT4 expert advisors work on MT5. Is this even possible? This article will reveal the facts behind.

Converting an MT4 EA to Work on MT5

Technically speaking, it is not possible to use an MT4 EA on MT5 the way it is. Any conversions will require some elements of modification to the existing MQ4 source file of the EA so that it can work on MT5. Performing these modifications require a good knowledge of the differences between the MQL4 and MQL5 language and environments.

I will now demonstrate how to convert an MT4 EA to MT5 format using an existing MT4 EA, the Moving Average.mq4 EA. I chose this EA because it comes pre-installed with every MT4 installation and mostly every programmer knows this EA.

Converting MT4 EA to MT5 EA, by Using The Example “Moving Average.mq4”

  1. Open the source file “MQL4\Experts\Moving Average.mq4” in MetaEditor 4 and select and copy all text (Ctrl + A, Ctrl + C). You can open the MetaEditor 4 by pressing the F4 key when the MT4 client terminal is open.
  1. It is assumed that you have downloaded the MT5 desktop terminal from your preferred broker. Open the instance of the MT5 client and open MetaEditor 5 to create a new EA (template) as shown in the image below:

HOW TO CONVERT MT4 EXPERT ADVISOR INTO MT5 FORMAT | BUY FOREX EA

 

  1. Assign a name to your EA template by filling the “Name” field. For example, you can use the name “SimpleEA” or simply name the file with something else. See the snapshot below:

HOW TO CONVERT MT4 EXPERT ADVISOR INTO MT5 FORMAT | BUY FOREX EA

After completing this action, you will get the template for your future EA to be built on MT5.

  1. Select all text (Ctrl + A) on the template and delete them all, then paste (Ctrl + V) recently copied text from MetaTrader 4 as shown in the image below:

HOW TO CONVERT MT4 EXPERT ADVISOR INTO MT5 FORMAT | BUY FOREX EA

You will get something like what is shown in the snapshot below:

HOW TO CONVERT MT4 EXPERT ADVISOR INTO MT5 FORMAT | BUY FOREX EA

MetaTrader 5 has other orders system (select, send, open, close…) methods, but in order to use the simplest method of conversion from MT4 to MT5 EA, it is better to use one library – mq4.mqh. This library allows the programmer or user to work with the orders in MQL5 (such as the MT5-hedge) in the same way as in the MQL4. In other words, the library enables the order language system (OLS) to become identical to MQL4.

This library covers only the order systems. The mq4.mqh library file is available from selected programmers online. The one used in this example has been obtained from a programmer and a little functionality added, and all these have been combined into a single file.

  1. Make sure an instance of the MT5 platform is open. Once you have your MT5 platform open, you can then click on File -> Open data folder ” ……\MQL5\Include” and drop the library file (mq4.mqh) file to the folder.

 

HOW TO CONVERT MT4 EXPERT ADVISOR INTO MT5 FORMAT | BUY FOREX EA

  1. Refresh the MT5 platform by closing it and opening it again, then open MetaEditor5. Open the source code of the expert advisor and in the source code of SimpleEA.mq5 add the string: #include <mq4.mqh>

HOW TO CONVERT MT4 EXPERT ADVISOR INTO MT5 FORMAT | BUY FOREX EA

  1. Once you have added the string to the source code, you can compile the updated source code of SimpleEA.mq5 by just pressing the “F7” button, or you can press the button which says “Compile” on the ToolBar of the MetaEditor 5:

You will get only 2 errors after compilation:

HOW TO CONVERT MT4 EXPERT ADVISOR INTO MT5 FORMAT | BUY FOREX EA

This also highlights another difference between the MT4 and MT5 platforms, and that is the differences in the indicator’s calls.

  • In MetaEditor 4, you just write iMA(symbol,timeframe,”settings ma”, shift), where “shift” is a number of a price candle, example 0, last candle on the chart and iMA() is the return value of moving average on 0-candle.
  • On MetaEditor5 you need to create handles for each indicator or EA and functions for getting value from the EA or indicator by this handle.

Changing the MT4 Source Code to a Workable MT5 Version: Step-by-step Guide

This brief section explains what you need to add the source code for MT4 to make it work in MT5. These changes apply only to the indicator. In other words, you need to change only one line in the source code.

Change from:

ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);

to:

int OnInit()
{
MAHandle = iMA(NULL, 0, MovingPeriod, MovingShift, MODE_SMA, PRICE_CLOSE);
if(MAHandle == INVALID_HANDLE)
{
Print(“Error creating MA indicator”).

The steps in conducting these changes are now described below:

  1. To begin, you have to create a variable Integer (int) type for MA-indicator handle and assign it a value known as INVALID_HANDLE. This is demonstrated below:

int MAHandle = INVALID_HANDLE;

HOW TO CONVERT MT4 EXPERT ADVISOR INTO MT5 FORMAT | BUY FOREX EA

Expert advisors and indicators have an OnInit() section for initializations in runtime. It has the form:

int OnInit()
{
// your code here 
return(INIT_SUCCEEDED);
}

You can read about this using the Help File. The Help File on MetaEditor5 is accessed by pressing the “F1” key. The MQL5 Community website also has a document which shows a lot of information that can be found in the Help File. This document can be accessed using this link:

https://www.mql5.com/en/docs/basis/function/events

  1. The next step is to add this code into the text (that is, the source code) of our EA, if this section does not exist (press Alt + M from MetaEditor 5).

HOW TO CONVERT MT4 EXPERT ADVISOR INTO MT5 FORMAT | BUY FOREX EA

In the list of functions, we can see that there are 5 functions. However, the OnInit() function doesn’t exist by default, therefore we have to create it by ourselves by writing it manually. This is shown in the image below.

You can press (Alt + M) on your computer keyboard so you can see the OnInit() function added to the source code.

HOW TO CONVERT MT4 EXPERT ADVISOR INTO MT5 FORMAT | BUY FOREX EA

  1. In section OnInit(), you can write the following code:

MAHandle = iMA(NULL, 0, MovingPeriod, MovingShift, MODE_SMA, PRICE_CLOSE);
if(MAHandle == INVALID_HANDLE)
{
Print(“Error creating MA indicator”);
return (INIT_FAILED);
}

Usually, in the MT4 MetaEditor, you would be required to write the code like this:

However, for the MT5, the code is written as a handle for an indicator. Therefore in MT5, you write the code as follows:

  1. In the next step, you will need to write a function to get value from Moving Average EA by using the handle shown in the image below:

We mentioned earlier that you need to create a function for getting value for the indicator or EA when using the MetaEditor5. The Function for getting values contains a string known as CopyBuffer(). The CopyBuffer copies data from indicator to your array. CopyBuffer gets data of a specified buffer of a certain indicator in the necessary quantity. You can get more information about CopyBuffer by reading the document available on this link:

https://www.mql5.com/en/docs/series/copybuffer

The code is written below as follows:

CopyBuffer( indicator_handle
[in] The indicator handle, returned by the corresponding indicator function.
         buffer_num
[in] The indicator buffer number.
         start_pos
[in] The position of the first element to copy.
         count
[in] Data count to copy.
         buffer[]
[out] Array of double type.
)

MA has 1 buffer which is numbered “0”. You need to copy one value from MA.

In the MT4 MetaEditor, this is written as follows:

The last “0” corresponds to the value of MA on the zero candle (the last candle on the chart).

In the MT5 MetaEditor, this is written as follows:

Copy one value, from index position to the MA array

  1. We now move to clear the 2 errors initially shown when compiling the code. You start this process by double clicking on the first record error as shown in the image below.

…..and you will be moved to where the error is located in the MT5 source code for your EA. The error code is located on line 114 for this example. See image below:

The line in the source code which contains this error has to be deleted and replaced with a string that contains the MaGet(0) code. This is illustrated in the image below. Notice that the error code is underlined in red ink, and the ma MaGet(0) code which is used to replace it is shown underneath.

Once you have replaced the error code with the MaGet(0) string, you can compile the code once more by pressing the F7 key. When the source code is recompiled, we see that only one error remains as shown below:

In order to replace the remaining error code, we go through the same sequence we used in clearing the first error code. Double click on the line containing the error code (i.e. on line 140 for this example), comment and replace as shown in the image below.

Again, the error code is underlined with green ink, and the string under it shows the code to be used to replace it and effect the correction of the error.

The string used for this replacement is:

if (Open[1]>MAGet(0) && Close[1]<MAGet(0)

Once you have commented and replaced the error code, you have to recompile the code. So once more, you compile the source code by pressing the F7 key on your computer keyboard to do this. You can see then that all errors have been taken care of. Zero errors in the code, as shown in the image below:

This snapshot below shows the difference between the MT4 and MT5 source codes for indicator calls and the conversion process from MT4 to MT5.

Now that we are done with the entire process of converting our MT4 EA to the version that can be used on MT5, we can now run it on our MT5 platform as shown below:

Conclusion

Without the process of using the library and a template as described above, it would be impossible to run an MT4 expert advisor on MT5. Rather, this would require that the programmer builds the entire program from scratch on the MetaEditor 5 programming interface. As usage of the MT5 increases in 2017 and beyond, it is likely that newer approaches to converting the source codes for MT4 indicators for usage on the MT5 platform will be developed. The MT5 has undergone several modifications to its structure and function and as Metaquotes Inc continues its march to creating a platform that will far exceed the performance of the MT4, programmers will need to keep updating their knowledge to match the latest modifications.


Share this post



← Older Post Newer Post →


Leave a comment

Please note, comments must be approved before they are published.

prooffactor.com