This article will reveal the facts behind the conversion of the expert advisors made to run on MT4 so they can also work on the MT5 platform. In other words, this article explains how to convert MQL4 code into MQL5 language code as easy as possible.
After years of what could be described as a stall in the progress of the MT5 platform, 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:
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.
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”
After completing this action, you will get the template for your future EA to be built on MT5.
You will get something like what is shown in the snapshot below:
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.
You will get only 2 errors after compilation:
This also highlights another difference between the MT4 and MT5 platforms, and that is the differences in the indicator’s calls.
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:
int MAHandle = INVALID_HANDLE;
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
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.
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:
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
…..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:
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.
Download the attached file above, which are the EA source codes for MT4 and MT5 used for this illustration.
What is Metatrader 4? MetaTrader 4 is one of the leading platforms in the world…
In this MT4 tutorial, Rimantas explains how to download MetaTrader 4 and open Forex demo…
There you'll find video tutorials for the Equity Sentry software for MT4. (more…)
There you'll find video tutorials on how to install and use the Trader On Chart…
There you'll find video tutorials on how to install and use the Trendline Trader EA…
Installing and running multiple MT4 client terminal instances on the same machine is easier than…
View Comments
Thank you for sharing this.
Ravi Chandiran
Simply amazing Rimantas! You have done it again.
I had one of my EA's written in cAlgo 3 years ago because I knew MT4 was going, but it was expoensive coding. I am still struggling understand C# as I never grew up with it.
I have 3 very complex profitable EA's and now I stand chance of extending their life instead of saying goodbye to these great EA's. If they were not veru good I would say goo riddance to Metaquotes, but you have now given me a project.
Thank you.
I forgot to ask, can you suggest an extensive mq4.mqh file source to download?
Thanks.
Glad I can help ;-)
The mq4.mqh file is included in the article. Find the download link at the bottom. I do not have any other library for this, but this one seems to do the work so far.
This is very helpful tutorial. very well explained. I just tried it and it worked!
Thank you for sharing..
Thanks for your work! :)
Thanks for the feedback ;-)
can we convert mt5 to mt4?
Congratulations on the article. Very enlightening.
I would like to convert an indicator from MT4 to MT5. What would be the process?
Hi, thanks for your feedback. The conversion process is not simple that I could explain in the comments. Try following the steps in the article because they are similar to the steps when converting the indicator from MT4 to MT5. Or you can try hiring a coder.
Hi Rimantas
Great article & Tutorial!
Will you please do the same & write a tutorial for converting MT4 indicators to work in MT5?
This would be greatly appreciated not only by me but I'm certain many others would like this.
Thank you!
Gary
I will see what I can do about it. No promises though.
Awesome article!
Hello, I have a mql4 indicator. I did exactly what was described, but I got 52 errors. Do you have any help with this?
It appears a lot of things has changed again in MQL5 since we created this tutorial. I would recommend hiring a coder: https://4xdev.com/?referrer=infoeacoder
Is this your company as well or just your referral link?
It's a referral link.
Great article, thank you very much for the effort, I hope your last recommendation will reach to the Meta trader programmer to add these futures to their next version of MT5.