MQL4 Reference

MQL4 Basics:Preprocessor

Preprocessor is a special subsystem of MQL4 compiler that is intended for preparation of the program source code immediately before the program is compiled.

Preprocessor allows enhancement of the source code readability. The code can be structured by including of specific files containing source codes of MQL4 programs. The possibility to assign mnemonic names to specific constants contributes to enhancement of the code readability.

Preprocessor also allows determining of specific parameters of MQL4 programs.

If the # symbol is used in the first line of the program, this line is a preprocessor directive. A preprocessor directive ends with a line feed character.

Constant declaration

Using the #define construction, one can define the symbolic name or symbolic constant at the program start to be the specific symbol string. Later on, the compiler will replace all appearances of this name without quotation marks with the corresponding string. In fact, this name can be replaced by any absolutely arbitrary text, not necessary with digits:

#define identifier value

The constant identifier conforms to the same rules as those regulating names of variables. The value can be of any type:

#define ABC          100
#define PI           0.314
#define COMPANY_NAME "MetaQuotes Software Corp."
void ShowCopyright() { Print("Copyright © 2001-2007, ",COMPANY_NAME); Print(""); }

Controlling compilation

Every MQL4 program allows to specify additional specific parameters named #property that help client terminal in proper servicing for programs without the necessity to launch them explicitly. This concerns external settings of indicators, first of all.

#property identifier value
Constant Type Description
link string a link to the company website
copyright string the company name
stacksize int stack size
library   a library; no start function is assigned, non-referenced functions are not removed
indicator_chart_window void show the indicator in the chart window
indicator_separate_window void show the indicator in a separate window
indicator_buffers int the number of buffers for calculation, up to 8
indicator_minimum double the bottom scaling limit for a separate indicator window
indicator_maximum double the top scaling limit for a separate indicator window
indicator_colorN color the color for displaying line N, where N lies between 1 and 8
indicator_widthN int width of the line N, where N lies between 1 and 8td>
indicator_styleN int style of the line N, where N lies between 1 and 8
indicator_levelN double predefined level N for separate window custom indicator, where N lies between 1 and 8
indicator_levelcolor color level line color
indicator_levelwidth int level line width
indicator_levelstyle int level line style
show_confirm void before script run message box with confirmation appears
show_inputs void before script run its property sheet appears; disables show_confirm property


#property link        ""
#property copyright   "MetaQuotes Software Corp."
#property library
#property stacksize   1024

Compiler will write the declared values in the settings of the executed module.

Including of files

The #include command line can be placed anywhere in the program, but usually all inclusions are placed at the beginning of the source code. Call format:

#include <file_name>
#include "file_name";


#include <WinUser32.mqh>
#include "mylib.mqh"

Preprocessor replaces this line with the content of the file WinUser32.mqh. Angle brackets mean that the WinUser32.mqh file will be taken from the default directory (usually terminal_directory\experts\include). The current directory is not searched.

If the file name is enclosed in quotation marks, the search will be performed in the current directory (where the main file of the source code is located). The standard directory is not searched in.

Importing of functions

Functions are imported from compiled MQL4 modules (*.ex4 files) and from operating system modules (*.dll files). The module name is specified in the #import directive. For compiler to be able to form the imported function call and pass parameters in a proper way, the full description of functions is needed. Functions descriptions follow the #import "module name" immediately. The new #import command (can be without parameters) completes the imported functions description block.

#import "file_name"
    func1 define;
    func2 define;
    funcN define;

Imported functions must have their unique names. Functions having the same names cannot be imported simultaneously from different modules. Imported functions names may not coincide with those of built-in functions.

Since the imported functions are out of the module to be compiled, the compiler cannot check correctness of parameters passed. This is why, to avoid runtime errors, it is necessary to declare the compliance of types and order of parameters precisely. The parameters passed to imported functions (both from EX4 and from DLL modules) cannot have values by default.


#import "user32.dll"
   int    MessageBoxA(int hWnd, string lpText, string lpCaption, int uType);
#import "stdlib.ex4" string ErrorDescription(int error_code); int RGB(int red_value, int green_value, int blue_value); bool CompareDoubles(double number1, double number2); string DoubleToStrMorePrecision(double number, int precision); string IntegerToHexString(int integer_number);
#import "ExpertSample.dll" int GetIntValue(int); double GetDoubleValue(double); string GetStringValue(string); double GetArrayItemValue(double arr[], int, int); bool SetArrayItemValue(double& arr[], int,int, double); double GetRatesItemValue(double rates[][6], int, int, int); int SortStringArray(string& arr[], int); int ProcessStringArray(string& arr[], int); #import

For importing of functions during execution of an mql4 program, the so-called late binding is used. This means that until the imported function has not been called, the corresponding module (ex4 or dll) will not be loaded.

It is not recommended to use the fully qualified name of loaded module appearing as Drive:\Directory\FileName.Ext. MQL4 libraries are loaded from the terminal_dir\experts\libraries folder. If the library has not been found, there will be an attempt to load a library from the terminal_dir\experts folder.

This article url:

Please login to post comment, if you haven't account you can register free