Monday, September 20, 2010

MQL4 and MQL5: Differences at a glance

MQL5 is the new generation language for the new platform MetaQuotes Software Corp. , Metatrader 5 . The developers of the most popular trading platform in Forex are designed to maintain maximum compatibility and mql5 MQL4 but still indicators, expert advisors and scripts written in MQL4 will have to migrate to the new language mql5 or not work due to some important differences between them. In this article I’ll leave some of the most important and general differences to consider when you prepare to upgrade your mql5 programs.

The differences appear at first sight mql5 code are:

In mql5 not have to declare the functions start (), init () and deinit () (function start () was essential for in MQL4).

The number of buffers in the indicators are not limited (in MQL4 the maximum amount of buffers for an indicator was 8).

Mql5 provides automatic conversions between types of variables involved, even in a numeric string.

Local variables are initialized other than string.

Local Arrays are automatically deleted from the cache after use.
Operator precedence as in C + +.
Checking the logical conditions faster and shorter.
Qe speciales replace functions init, start and deinit

In MQL4 contained three features that could be using either a script, an indicator or an Expert Advisor. In mql5 there are many more features and can not all be used interchangeably in any program.

The function init () has an analogous MQL4 mql5 the function OnInit () function start () has its analogue in OnStart () and deinit () in OnDeinit ().

The OnInit and OnDeInit features have the same functions as init and deinit in MQL4, designating them the code that must be executed to initialize or a program written in fading mql5. Therefore these functions passed to mql5 MQL4 can simply rename or call the former functions as follows:

void OnInit () (
init ();
OnDeinit void ()
deinit ();

You can not do the same for the function start (), since no function mql5 OnStart () that can be used in all types of programs mql5 but there are several functions depending on the type, so for example OnStart () is analogous start () in scripts, OnCalculate () is the main function indicators could and replaced the start () and onTick () it is for the expert advisors. Regarding the above in this paragraph on the role OnStart () I should mention that if a script does not function contine OnStart () code will be compiled as an Expert Advisor. Likewise if an indicator does not contain OnCalculate () as an indicator compilation is impossible.

Predefined Variables MQL5

In MQL5 peredefinidas have Periodic variables such as Bid, Ask and Bars (these must be obtained through those corresponding predefined functions) and others such as Point and Digits have a slightly different expression. MQL5 predefined variables are:
_Digits: Returns the decimal number dei available in the price of contización the symbol of the current graphic.

Point: Pip stores the size of the symbol on the current chart.
LastError: Contains the code of the last error occurred during program execution mql5. Default is the only variable that can be changed through programming mql5 as it can be reset using ResetLastError ().

Period: Stores the current period of a graphic that is running the program.
Symbol: Contains the name of the symbol of the current graphic.
StopFlag: It contains the end point in the execution of a program mql5. When Metatrder five tries to stop a program mql5 _StopFlag changes to true.
UninitReason: Contains the reason code to stop the execution of a program mql5.

Variables exteranas

In MQL4 external variables are declared in the initial part of the program by the name “extern”. In MQL5 this has changed and also external variables are declared at the beginning of the program but with the name “input”.

Timeseries Access

A major change is that there is mql5 timeseries predefined as Open, High, Low, Close, Volume and Time. To access these data is necessary to establish the corresponding functions described in Access to Timeseries and Indicator Data .

Expert Advisors

In MQL4 EA was required Parq even contain the start () function is executed every tick received in Metatrader terminal. In mql5 new events have been made to call the execution of a certain code. This can be used in an EA:

OnTick: analogous to the start of MQL4 function is executed every time you get a new tick.

OnTimer: runs on the basis of a “timer” every so often a time established through EventSetTimer () that will be declared in advance for example in OnInit ().
OnChartEvent: generated by events occurring when the user interacts with the terminal as keystrokes, movements and mouse clicks, movements of objects in the chart with the mouse or creation / deletion of objects. OnChartEvent also run through your call EventChartCustom () function to be processed by OnChartEvent.
OnBookEvent I have not been able to understand that it is this event, I will continue studying for acutalizar this data.

OnTrade: event generated following completion of a transaction between the terminal and the server (eg submission, amendment or cancellation of an order).

OnTester: Event generated when we pass the final test of an EA in eStrategy tester.
You OnCalculate is only for OnStart only indicators and scripts.

Custom Indicators

The main novelty in the MQL5 indicators is that, unlike MQL4, the number of buffers is not limited. This feature will greatly increase the possibilities for developers, but with increasing use of buffers increases memory and the PC needed to run the program.

Another difference to note is the increase of available styles for displaying the indicator on the screen. While MQL4 offered six types, MQL5 offers up to 18. This adds to the great change that has taken on representation of the indicators and graphical objects.

Also changes the direction of indexing of the elements of the buffer indicator. Because the buffer is going to have the conduct of an array type variable, we will go closer in time with increasing the position of the indexed column systems. For example, in the zero position MQL4 return the current value of the indicator while MQL5 zero position is the value of the first calculation, the most ancient data. In MQL5 therefore must specify the type of data to be stored in an array and bind this array to a given buffer and use the access to timeseries described a few paragraphs above.

All these changes make the algorithm for calculating the indicdores in MQL5 recostruído have to be almost entirely if they come from a program written in MQL4.


In my opinion, the language has improved greatly and will greatly increase the chances of the indicators, and scripts Adviors Experts for Metatrader Metatrader 5 on 4 but for users in general without much or any programming knowledge will be very difficult to cope with new language, MQL5 is more sophisticated, more powerful and therefore more complicated.