MQL2 to MQL4 conversion and vice versa!
Hi folks,
In these series of articles I’ll try to remove the stumbling blocks from the road of the programmers who want to convert MQL2 programs to MQL4 and vice versa.
A lot of people find that the MQL2 and the MQL4 are different worlds, because the syntax of MQL2 is based on EasyLanguage while the syntax of MQL4 is very like the C syntax (Besides the new added features to the MQL4 language).
But I think that, although it’s hard to convert between the two languages but it’s not an impossible task. That’s because the both of the two languages talking about the same thing; about creating a program for MetaTrader, so, they are not two separated worlds but they are two faces of the same coin.
We can’t set a series of lessons to study MQL2 like which we assigned to MQL4 but you will know a lot of MQL2 principals while you are skimming these conversion articles.

We will take somewhat a practical approach studding the difference between the two languages, that’s by studding a real example of a program wrote in the both language and observing the conversion operation has been occurred.
Let’s converting!
Our example program:
We are going to work in these articles with an indicator called “3D Oscilator”.
Download the MQL2 source code:
3D Oscilator_MQL2.zip (974 B)
Download the MQL4 source code:
3D Oscilator_MQL4.zip (1009 B)
Note: The 3D Oscilator has been created in MQL2 by Luis Damiani and converted to MQL4 by Ricardo. Ramdass.
Loading the 3D Oscilator(s) in the MT4 and MT3 terminals:
If you are a lucky person like me and have the both of MT4 and MT3 installed in your machine, you can compile the 3D Oscilator and load it in the Terminals. (If you don't have MT3 yet, you can download it from the MetaQuotes download section: http://www.metaquotes.net/downloads/).
To compile the MQL4 version:
1- Place the “3D Oscilator.mq4” file in the Indicators path.
2- Double click it to open it in MetaEditor.
3- Press F5 to compile the program (produced file extension is .ex4).
4- Open MetaTrader and from the Navigator window navigate to the Custom Indicators section and double click the 3D Oscilator.
5- When you get the Properties window (Figure 1) click OK.
Figure 1 - Properties window in MetaTrader 4
To compile the MQL2 version:
The same steps as MQL4, but you will get a Properties window like this:
Figure 2 - Properties window in MetaTrader 3
Figures 3 and 4 shows how the indicator looks like in the both platforms:
Figure 3 - How the indicator appears MetaTrader 4
Figure 4 - How the indicator appears MetaTrader 3
Cracking the MQL2 program:
We are going to crack the MQL2 program section by section and line by line to convert it to the MQL4 program.
/*[[
Name := 3D Oscilator
Author := Luis Damiani
Separate Window := Yes
First Color := lime
First Draw Type := Line
First Symbol := 217
Use Second Data :=yes
Second Color := RoyalBlue
Second Draw Type := Line
Second Symbol := 218
]]*/
The section above is the description section where you put the name of the program, the author name, window type of the indicator, the indicator line properties and symbols etc.
The section when converted to MQL4 it will be dispersed in the program and not in a gathered section like MQL2.
In this section you can find the following commands:
Name: The name of the program.
Author: The author name of the program.
Separate Window: Plot the indicator in the main chart window (No) or in a separate window.
First Color: The color of the first line (Second, Third etc).
First Draw Type: The type of the drawing line; it maybe: Line, Histogram or Symbol (Second, Third etc).
First Symbol: The symbol number from Wingdings table of the drawing line (Second, Third etc).
Use Second Data: Yes means that the indicator calculation results in 2 charts, and not the only one.
Minimum Chart Limits: If the indicator is shown in a separate window this command sets the minimum value drawn on the chart.
Maximum Chart Limits: the maximum value drawn on the chart.
Let's see how we converted these lines to MQL4:
Name := 3D Oscilator
The Name of the program usually wrote in the comment section in MQL4 along with the Author name and the website Link.
//+------------------------------------------------------------------+//| 3D Oscilator.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
Author := Luis Damiani
The Author name wrote in the comment section and in the copyright property directive:
#property copyright "Author - Ricardo. Ramdass - Conversion only"Separate Window := Yes
The equivalent line in MQL4 is the indicator_separate_window property:
property indicator_separate_window
The color of the line in MQL4 set by the indicator_colorN property, where the N is is the number of the line, for example:
#property indicator_color1 //First line color
#property indicator_color2 //Second line color
#property indicator_color8 //Eighth line color
In MQL4 we have to add extra line before the indicator_color property, this is the indicator_buffers property which sets the number of the buffers used in the program, the buffers used for the calculations of the indicators and for drawing lines.
In our program we will use two lines and will not use any buffers for calculation. So we have to set 2 buffers:
#property indicator_buffers 2
And to set the color of the first line we used this line of code:
#property indicator_color1 Yellow
First Draw Type := Line
To set the drawing type of the line in MQL4 you use SetIndexStyle() function and you have to use it inside the Init() function.
In our program we used code like this:
int init() {....
SetIndexStyle(0,DRAW_LINE);
....
}
SetIndexStyle:
void SetIndexStyle( int index, int type, int style=EMPTY, int width=EMPTY, color clr=CLR_NONE)
This function will set the style of the drawn line.
The index parameter of this function ranges from 1 to 7 (that’s because the array indexing start with 0 and we have limited 8 lines). And it indicates which line we want to set its style.
The type parameter is the shape type of the line and can be one of the following shape type’s constants:
DRAW_LINE (draw a line)
DRAW_SECTION (draw section)
DRAW_HISTOGRAM (draw histogram)
DRAW_ARROW (draw arrow
DRAW_NONE (no draw)
The style parameter is the pen style of drawing the line and can be one of the following styles’ constants:
STYLE_SOLID (use solid pen)
STYLE_DASH (use dash pen)
STYLE_DOT (use dot pen)
STYLE_DASHDOT (use dash and dot pen)
STYLE_DASHDOTDOT (use dash and double dots)
Or it can be EMPTY (default) which means it will be no changes in the line style.
The width parameter is the width of line and ranges from 1 to 5. Or it can be EMPTY (default) which means the width will not change.
The clr parameter is the color of the line. It can be any valid color type variable. The default value is CLR_NONE which means empty state of colors.
MQL4 Special functions init(), deinit() and start():
Functions are blocks of code which like a machine takes inputs and returns outputs.In MQL4 there are three special functions which you can't name your functions the same names and have special jobs in MQL4
init():
Every program will run this function before any of the other functions, you have to put here you initialization values of you variables.
start():
Here’s the most of the work, every time a new quotation have received your program will call this function.
deinit():
This is the last function the program will call before it shutdown, you can put here any removals you want.
