MQL4 Reference Updated MQL4
What's New in MQL4
Starting from build 600, MQL4 programming language has been completely revised reaching the level of MQL5 - now you can develop trading robots in MQL4/5 using the unified MetaEditor development environment, single style, libraries and debugging tools.
MQL4 is popular among automated system developers due to the ease of learning and a huge amount of code generated for many years of using MetaTrader 4 terminal. However, the language also has some drawbacks arising from its main advantage - a simple programming language does not allow development of complex systems and hinders porting of debugged libraries from high-level languages. Therefore, we decided to implement in it the maximum possible amount of MQL5 language functions and features fully preserving MQL4 functionality. In other words, all powerful MQL5 functions, including ООP and the native code compiler, will become available in MQL4.
To achieve this, we have developed a unified compiler that automatically supports both MQL4 and MQL5 languages. MetaEditor will also become a unified application both for MetaTrader 4 and MetaTrader 5 platforms. Thus, it will be possible to compile both MQL4 and MQL5 from any version. MQL5 Storage also becomes available for work.
Protection of MQL4 applications rises to MQL5 level. New EX4/EX5 files are provided with a serious and completely revised protection. This means that the Market of secure EX4 applications also becomes available to MetaTrader 4.
Besides, MQL4 now features new graphical objects and new functions for working with charts. MQL5 Standard Library is to be ported to MQL4 providing developers with new possibilities in creating their own graphical interfaces and trading libraries. Now, you can create full-fledged applications in MetaTrader 4 using the resources.
Changes in MQL4 Language
Added new char, short, long, uchar, ushort, uint, ulong and double data types. This will allow transferring codes from other C++ like languages. Data of various types is processed at different rates. Integer data is the fastest one to be processed. A special co-processor is used to handle the double-precision data. However, due to the complexity of the internal representation of floating-point data, it is processed slower than integer one. Typecasting has also been implemented.
Strings are now presented in Unicode format, though they were in ANSI format (single byte ones) before. That should be considered if the program uses DLLs and passes string variables to them.
Predefined Volume variable is now of long type. The time series for accessing the volumes also consist of long type arrays. It is recommended to use explicit casting of data having this type to the target type in old MQL4 programs to avoid type overflow error.
ООP allows developing programs using classes. This facilitates debugging and development of large applications, as well as provides ability to reuse previously generated code multiple times due to inheritance. However, that does not mean that you cannot generate your MQL4 code in procedure-oriented style as before. You can develop your programs as you did in the past if you don't need the new features.
init(), deinit() and start() predefined functions have remained for compatibility, however, OnInit(), OnDeinit(), OnStart(), OnCalculate() and OnTick() ones can now be used instead. Besides, new predefined OnTimer(), OnChartEvent() and OnTester() handler functions have been implemented. In the previous MQL4, predefined functions could have any parameters and any return type, and they could be called by their names, not signatures. In the new MQL4, all predefined functions should strictly correspond to their signatures. In other words, they should have precisely defined set of parameters and return type.
Now, variable names cannot contain special characters and points, and new MQL4 language keywords cannot be used as names. Old MQL4 programs can be recompiled with the new compiler in order to easily correct all such errors while following the compiler's messages.
The Precedence rule now matches C language standards. If you are unsure, you can insert parentheses in old MQL4 apps to clearly indicate the priority to increase reliability.
Shortened conditions check is now used in logical operations, unlike the old MQL4 version where all expressions have been calculated and the check has been performed afterwards. Suppose there is a check of two conditions with the use of logical AND:
If condition1 expression is false, calculation of condition2 expression is not performed, as false && true result is still equal to false.
ArrayCopyRates() function behavior has changed. In the previous MQL4 versions this function was used for copying price series to array double. Now, if you need to receive a time series, use the arrays of the MqlRates structure elements:
Also the new function format can be used for virtual copying, when there is no actual copying, and accessing the copied values you actually access the price data.
To provide compatibility with old MQL4 programs, the previous call format is also preserved, but now it leads to actual copying of data into a double-type array.
This means that when the values in the time series change (new bars are added, restructuring, the last bar's Close price is uodated), you must re-copy the required data into the dest_array. The receiver array will be automatically allocated for the required number of copied bars, even if it was declared statically.
Changed RateInfo history data storage format. RateInfo structure was presented as follows in the old version:
In the new format, RateInfo structure features fields for storing spread and trading volume:
Thus, if MQL4 programs contain DLLs for passing/accepting price data, the corresponding functions in the source codes should be rewritten and recompiled considering format changes to ensure proper operation.
In file operations, the number of simultaneously opened files can now reach 64 ones, while there could be no more than 32 ones in the old MQL4. Until recently, the files were always opened in FILE_SHARE_READ or FILE_SHARE_WRITE mode. Now, the necessary opening mode should be specified explicitly.
For FileWrite(), FileWriteArray(), FileWriteDouble(), FileWriteInteger() and FileWriteString() functions the type of returned value has been changed from int to uint. The functions return the number of bytes, actually written or 0 in case of error (in old version of MQL4 the negative number is returned in case of error).
Working with functions, scope of variables and memory release in local arrays has also been changed. Since the number of changes is large enough, the new #property strict property has been introduced to provide maximum compatibility with the previous approach to developing MQL4 programs. When creating new MQL4 application using MQL wizard, this property is always added to the template.
The string representation of datetime type depends on compilation mode:
The table below contains the differences between MQL4, new MQL4 without using strict and new MQL4 with specified strict compilation mode:
When compiling libraries in the strict mode, the export modifier should be added for each exported function, otherwise the function will not be accessible from outside.
* Please pay special attention to "Array out of range" error - many old custom indicators will display this error in strict mode of the new compiler when launched on the chart. It is recommended to find the cause and eliminate it.
** In the new MQL4 and MQL5, FILE_SHARE_READ and FILE_SHARE_WRITE flags are responsible for the files shared use mode. There were no such files in the old MQL4.
Changes in File Structure
In the previous builds of MetaTrader 4 client terminal (509 and older), all MQL4 applications were stored in the following subdirectories of <terminal_installation_folder>\experts\ root directory:
When updating MetaTrader 4 terminal from build 509 to the newer version, all MQ4, MQH and EX4 files from standard root directories of the previous version are automatically copied and relocated to the appropriate folders. Subfolders additionally created by a user, as well as files contained there are not processed. They should be relocated to the new place manually if necessary.
In some cases, you may need to edit the path in #property include for included files (if relative paths have changed) in the source files. Please note that MetaEditor's root directory is now <data_folder>\MQL4\. All programs should be located in the correct subdirectories.
You can find the data folder (<data_folder>) for each copy of MetaTrader 4 terminal on your computer via the terminal menu or in MetaEditor: File - Open Data Folder.