MQL4 Reference

MQL4 Program Run

For an MQL4 program to work, it must be compiled (the "Compile" button or F5). It must be compiled without any errors (warnings are allowed, but they must be analyzed). At this, in the corresponding directory, terminal_dir\experts, terminal_dir\experts\indicators, or terminal_dir\experts\scripts, an executable file must be created that has the same name and extension EX4. It is this file that can be launched for execution.

Experts, custom indicators, and scripts are attached to one of the opened charts by dragging with the mouse from the "Navigator" window of the client terminal to the corresponding chart (the Drag’n’Drop technique). MQL4 programs can work only when the client terminal is on.

For an expert to stop working,it must be deleted from the chart using "Expert Advisors – Delete" in the chart context menu. The status of the "Enable Expert Advisors" field influences the running of the expert.

For a custom indicator to stop working, it must be deleted from the chart.

Custom indicators and expert advisors operate until they are explicitly deleted from the chart. Information about the attached experts and custom indicators is saved between client terminal startups. Scripts are executed once and deleted automatically after they have completed their operation, or when the current chart has been closed or changed its status, or when the client terminal has been terminated. Scripts are not launched at the terminal restart since information about them is not saved.

In the same chart, one expert, one script, and an unlimited amount of indicators can work simultaneously.

Program Run

Immediately after the program has been attached to the chart, it starts working with the init() function. The init() function of an expert advisor or a custom indicator attached to the chart will run just after client terminal has started and history data (this concerns only experts, but not indicators) have been loaded additionally, after the symbol and/or chart period have been changed, after the program has been recompiled in MetaEditor, after inputs have been changed from the window of expert or custom indicator settings. An expert will also be initialized after the account has been changed.

Every program attached to a chart completes its work with the deinit() function. The deinit() function runs at the client terminal shutdown, at chart closing, immediately before the symbol and/or chart period is changed, at successful recompiling of the program, at changing of inputs, or at changing of the account. One can see the reason of deinitialization using the UninitializeReason() function during execution of the deinit() function. The deinit() function must be executed within 2.5 seconds. If the function has not completed its execution within this time period, it will be completed forcedly. Scripts are an exception to this rule, as they normally finish their working independently, without any commands from outside. If a script works very long (due to endless loop, foe example), it can be finished by an outside command (at deletion of the script from the chart context menu, at attaching of a new script to the same chart, at closing of the chart, at changing of the symbol and/or chart period). In this case, the deinit() function is limited by 2.5 seconds, too.

At incoming of new quotes, the start() function of the attached experts and custom indicators will be executed. If the start() function launched at the preceding quote was running when a new quote came, the new quote will be skipped by the expert. All new quotes income while the program was being executed are skipped by the program until the current execution of the start() function has been completed. After that, the start() function will be run only when a successive new quote incomes. For custom indicators, the start() function will be launched for recalculation after the current chart symbol or timeframe has been changed independently on new quotes incoming. The start() function will not be run when the expert properties window is open. The latter cannot be opened during the expert execution.

Detaching of the program from the chart, change of symbol and/or chart period, change of the account, closing of the chart, as well as shutdown of the client terminal will interrupt execution of the program. If the start() function was being executed at the moment when the stop working command was given, the time remaining for its work is limited by 2.5 seconds. The program can get to know that it is tried to shut it down with the built-in function of IsStopped() and finish its work correctly.

Execution of scripts does not depend on incoming quotes. At change of symbol and/or chart period, the script will finish its work and be unloaded from the client terminal.

Scripts and experts work in their own thread. Custom indicators work in the main interface thread. If a custom indicator has been called with the iCustom() function, this indicator works in the thread of the program that has called it. Library (imported) functions work in the calling program thread, as well.

Imported functions call

To import functions during execution of an mql4 program, the so-called late binding is used. This means that, until the imported function is called, the corresponding module (ex4 or dll) will not be loaded. MQL4 and DLL libraries are executed in the calling module thread.

It is not recommended to use a fully qualified name of the module to be loaded like 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 made an attempt to load the library from the terminal_dir\experts folder.

System libraries (DLLs) are loaded by the rules of the operation system. If the library has already been loaded (by another expert, for example, or from another client terminal launched at the same time), the library already loaded will be referenced to. Otherwise, the searching will be preformed in the following order:

  • 1. The terminal_dir\experts\libraries directory.
  • 2. The directory from which the terminal_dir client terminal was launched.
  • 3. The current directory.
  • 4. The system directory of windows_dir\SYSTEM32 (or windows_dir\SYSTEM for Win98).
  • 5. The directory where windows_dir operation system was installed.
  • 6. Directories listed in the PATH environment system variable.

If a DLL uses another DLL in its work, the former one will not be loaded if the latter one is unavailable.

Unlike system libraires, custom libraries (MQL4) are loaded for every calling module separately, independently on whether the called library has been loaded by any other module. For example, the caller.ex4 module calls functions from lib1.ex4 and lib2.ex4 libraries. The lib1.ex4 library, in its turn, calls functions from the lib2.ex4 library. In this case, one more copy of the lib1.ex4 library and two copies of the lib2.ex4 library will be loaded, regardless that all calls come from the same caller.ex4 module.

Functions imported from DLL into an mql4 program must provide linkage convention accepted for Windows API functions. To provide such a convention, the key word __stdcall specific for compilers of Microsoft(r) company is used in the source codes of programs written in C or C++ language. The above linkage convention is characterized by the following:

  • – calling function (in our case, it is an mql4 program) must "see" the called (imported from DLL) function prototype in order to put parameters onto the stack in a proper way;
  • – calling function (in our case, it is an mql4 program) puts parameters onto the stack in the reversed order, i.e., from right to left; it is this order in which the imported function reads parameters passed to it;
  • – parameters are passed by their values, except for those explicitely passed by a link (in our case, these are lines);
  • – on reading the parameters passed to it, the imported function will flush the stack by itself.

At describing of the imported function prototype, it is useless to use parameters with default values as all parameters must be passed explicitely to the imported function.

If the call for an imported function failed (expert settings do not allow DLL imports, or the corresponding library could not be loaded for any reason), the expert stops working putting the corresponding message into the "expert stopped" journal. At that, the expert will not be launched until it is re-initialized. An expert can be re-initialized as a result of recompiling or opening of the expert properties table and pressing of OK button.

This article url:

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