Single post

Transfer from MQL4 to MQL5


The mql5 programming language is the next step of evolution of the previous mql4 programming language on the basis of which a lot of forex indicators, forex scripts and forex expert advisors have been created. In spite of maximal compatibility of both the new and the old programming languages, there are several differences between them. So if you want to transfer your forex software from mql4 to mql5, you should know these differences.


In this article we’ve collected the information that will help you to adapt your codes, written in the mql4 programming language, to the mql5 programming language


First of all, the following should be mentioned:



  • Functions start(), init() and deinit() are absent;

  • The number of indicator buffers is unlimited;

  • Dll loading occurs right after the forex expert advisor loading (or any other forex software created for the metatrader 5 trading terminal);

  • Logical conditions test is shortened;

  • Current execution stops at exceeding the limits of the array (critically – with error display);

  • Precedence of operators like in C++;

  • Implied type coercion works (even from a line to a number);

  • Local variables are not initialized automatically (except lines);

  • General local massifs are destroyed automatically.

Special functions init, start and deinit


There were only three predefined functions in mql4 that could be present in a code of forex indicators, forex scripts and forex expert advisors (without taking into account include files*.mqh and library files. There are no such functions in mql5 but there are their analogues. Here is correspondence of functions:


 mql4 – init, start, deinit
 mql5 – OnInit, OnStart, OnDeinit


OnInit and OnDeinit in the mql5 programming language play the same role as init and deinit in the mql4 programming language of the metatrader 5 trading terminal. They are intended for realization of the code that should be executed at initialization and deinitialization of mql5 forex software. You can either rename these functions as appropriate or leave them as is but add function call in the corresponding places.


An example:


void OnInit() 
 {


 //— function call at initialization


 init();


 }


 void OnDeinit()


 {


 //— function call at deinitialization


 deinit();


 //—


 }


The function start is replaced by OnStart only in scripts, and in case of forex robots and forex indicators you should rename it (OnTick and OnCalculate respectively). You should place the code that should be executed at work of metatrader 5 program in these three functions:


mql5 program  basic function
Script   OnStart
Indicator  OnCalculate
Expert advisor  OnTick


If there is no basic function in the code of the executed indicator ir script or the name of this function differs from the requirements, function call is nit carried out. I.e. if there is no OnStart function in the script source code, it means that this code will be compiled as an advisor.


If there is no OnCalcultae function in the indicator source code, compilation of such indicator is impossible.

theme by teslathemes