Some information about the migration from MQL4 to MQL5:

MQL5 is the development of its predecessor - the MQL4 language, in which numerous indicators, scripts, and Expert Advisors were written. Despite the fact that the new programming language is maximally compatible with the previous-generation language, there are still some differences between these languages. And when transferring programs these differences should be noted.

Information intended to facilitate the adaptation of codes to the new MQL5 language for programmers who know MQL4:

First it should be noted:

• The new language does not contain functions start(), init() and deinit();
• The number of indicator buffers is not limited;
• dll is downloaded immediately after downloading an Expert Advisor (or any other mql5 program);
• Check of logical conditions is shortened;
• When limits of an array are exceeded, the current performance is terminated (critically - with the output of an errors);
• Precedence of operators like in C + +;
• The language offers the implicit type cast (even from string to a number);
• Local variables are not initialized automatically (except for strings);
• Common local arrays are automatically deleted.

Special Functions init, start and deinit
The MQL4 language contained only three predefined functions that could be used in the indicator, script or Expert Advisor (not taking into account the include files *.mqh and library files). In MQL5 there are no such functions, but there are their analogues. The table shows the approximate correspondence of functions.

metatrader 5 - mql5

Functions OnInit and OnDeinit perform the same role as init and deinit in MQL4 - they are designed to locate the code, which must be performed during initialization and deinitialization of mql5 programs. You can either just rename these functions accordingly, or leave them as they are, but add calls of these functions in corresponding places. The start function is replaced by OnStart only in scripts. In Expert Advisors and indicators it should be renamed to OnTick and OnCalculate, respectively.

Example:

void OnInit()
{
//--- Functions is called to initialize
init();
}
void OnDeinit(const int reason)
{
//--- Call the function with deinitialization
deinit();
//---
}
If the indicator or script code does not contain the main function, or the function name differs from the required one, the call of this function is not performed. It means, if the source code of a script doesn't contain OnStart, such a code will be compiled as an Expert Advisor. If an indicator code doesn't contain the OnCalculate function, the compilation of such an indicator is impossible.

Predefined Variables
In MQL5 there are no such predefined variables as Ask, Bid, Bars. Variables Point and Digits have a slightly different spelling.

metatrader 5 - mql5

Access to Timeseries
In MQL5 there are no such predefined timeseries as Open [], High [], Low [], Close [], Volume [] and Time []. The necessary depth of a timeseries can now be set using corresponding functions to access timeseries.

Expert Advisors
Expert Advisors in MQL5 do not require the obligatory presence of the function for handling the events of a new tick receipt - OnTick, as it was in MQL4 (the start function in MQL4 is executed when you receive a new tick), because in MQL5 Expert Advisors can contain pre-defined handler functions are several types of events:

• OnTick – receipt of a new tick;
• OnTimer – timer event;
• OnChartEvent – events of input from the keyboard and mouse, events of a graphic object moving, event of a text editing completion in the entry field of the LabelEdit object;
• OnBookEvent – event of Depth of Market status change.

Custom Indicators

In MQL4, the number of indicator buffers is limited and can't exceed 8. In MQL5 there are no such limitations, but it should be remembered that each indicator buffer requires allocation of a certain part of memory for its location in the terminal, so the new possibility should not be abused.
MQL4 offered only 6 types of custom indicator plotting; while MQL5 now offers 18 drawing styles. The names of drawing types haven't changed, but the ideology of the graphical representation of indicators has changed significantly.
The direction of indexing in indicator buffers also differs. By default, in MQL5 all the indicator buffers have the behavior of common arrays, i.e. 0 indexed element is the oldest one in the history, and as the index increases, we move from the oldest data to the most recent ones.
The only function for working with custom indicators that was preserved from MQL4 is SetIndexBuffer. But its call has changed; now you should specify type of data to be stored in an array, linked to the indicator buffer.
Properties of custom indicators also have changed and expanded. New functions for accessing timeseries have been added, so the total calculation algorithm must be reconsidered.

Graphical Objects
The number of graphical objects in has increased significantly MQL5. But now there is a limitation - functions for working with graphical objects can't be used in custom indicators.
Besides, graphical objects can now be positioned in time with the accuracy of a second in a chart of any timeframe - now object anchor points are not rounded off to the bar opening time in the current price chart.
For objects Arrow, Text and Label now way of binding can be indicated, and for Label, Button, Chart, Bitmap Label and Edit chart corner, to which the object is anchored, can be set.

Source: MQL5 Reference

3 comments

Paul said... @ November 19, 2009 at 5:19 PM

Thanks for highlighting the launch of the MQL5 site, Peter.

I know that this topic has been discussed extensively elsewhere, but it's amazing that the article above focuses on the programming language changes only, and omits to mention arguably the biggest change in the transition from MetaTrader 4 to MetaTrader 5, which is the management of open trades as positions.

From my experiences as I write a Virtual Order Manager, I can definitely say that this will often require the most coding changes in the conversion of an MQL4 EA to MQL5.

Unknown said... @ December 31, 2009 at 11:50 AM

Hi to all,

I am a novice in the world of TRADING, and at this moment I am experimenting the beta version of metatrader 5, that in my opinion (although not to perceive very much of programming, MQL), seems to be very good. But I only want to give to a suggestion since Metatrader 5 is in experimental phase, think that this is possible and that it would go to benefit a little more Metatrader 5 in the way of visualization of the graphs , a tool as the, PAN Tool and the Area Zoom Tool, I think to be two tools very used by traders, in other softwares and that allow to fast access specify zones of the graph. I leave this suggestion in the hope that the creators of this good software, think and try to incorporate this type of tools, to complement it all the existing others already, in this new Metatrader 5.


My sincereous regards to all

Unknown said... @ February 1, 2010 at 5:38 AM

I am a beginner in the world of trading and a very raw beginner in the world of coding.

End of 2009 I had just started getting my head around the basics of MQL4 and coded some indicators to find out that MQL5 / MT5 was to be released in 2010.

This article on the basics is very informative for me.

Thanks... Keep it up...

Best regards

Post a Comment