MQL4 Book  Basics of MQL4  Program Types

Program types

When starting to write a program in MQL4, you must, first of all, answer the question about what type of programs will it be. The contents and functionality of the program fully depend on this. In MQL4, there are three types of application programs: EAs, scripts, and custom indicators. Any program you develop will belong to one of these types. They all have their purposes and special features. Let us consider these types in detail.

An Expert Advisor (EA) is a program coded in MQL4 and called by the client terminal to be executed at every tick. The main purpose of EAs is the programmed control over trades. EAs are coded by users. There are no built-in EAs in the client terminal.

A script is a program coded in MQL4 and executed by the client terminal only once. Scripts are intended to perform operations that should be executed only once. Scripts are coded by users. They are not delivered with the client terminal as built-in programs.

A custom indicator is a program coded in MQL4 and called by the client terminal to be executed at every tick. It is basically intended for graphical displaying of preliminarily calculated data, such as lines. Indicators cannot trade. There are two types of indicators: technical (built-in) indicators and custom indicators. Indicators are considered in detail in Usage of technical indicators and Creation of custom indicators.

You choose the type of the program to write depending on the purpose of the specific program and on the properties and limitations of the different types of programs.


Properties of programs

Launching a program for execution

There is a criterion that distinguishes EAs and custom indicators from scripts. This is their run duration. In Some basic concepts, we mentioned already that programs are launched as often as there is a tick. This statement is true for EAs and custom indicators, but it is false for scripts.

EA and custom indicator. Once you have attached a program (EA or custom indicator) to the symbol window, the program makes some preparations and switches to the tick-waiting mode. As soon as a new tick comes, the program will be launched by the client terminal for execution, then it makes all necessary operations prescribed by its algorithm. Upon completion, the program passes the control to the client terminal, that is it switches to the tick-waiting mode.

If a new tick comes when the program is being executed, this does not have an effect on the program execution—the program continues being executed according to its algorithm, and the program passes the control to the client terminal only upon completion. This is why not all the ticks result in launching an EA or a custom indicator. Only those ticks that come in when the control is in the client terminal and when the program is in the tick-waiting mode call the EA or custom indicator.

The new tick launches the program for execution. Thus, an EA or a custom indicator can operate within a long period of time, being attached to the symbol window and starting to run from time to time (as often as a new tick comes in while the program is in tick-waiting mode).

Besides, an EA differs from an indicator by the execution order at the first launch of the program. This difference is determined by the specific properties of special functions in the program of a certain type (see Special functions). Once attached to the symbol window, an EA makes the necessary preparations contained within init() function. Then, it switches to the tick-waiting mode, calling the start() function only upon the first incoming tick. Unlike EAs, a custom indicator both executes init() function and calls start() function one time to make the first calculation of the indicator value. Later on, at a new tick, the program is launched by calling only start() function. That is, operators are executed according to the algorithm of start() function.

Script. Unlike EAs or indicators, a script will be launched for execution immediately after it has been attached to a symbol window, without waiting for a new tick. The entire code of the script will be executed once. After all program lines have been executed, the script finishes its operations and is unloaded from the symbol window. A script is helpful if you want to make one-time operations, such as to open or close orders, to display texts on the screen, to install graphical objects and so on.

The differences in execution of EAs, scripts, and custom indicators is determined by the properties of their special functions which will be considered in detail in Special functions.


Trading

One of the main criteria that mark the above programs is the possibility to make trading instructions. A trading instruction is a control that a program passes to the trading server in order to open, to close, or to modify orders. Trading instructions are formed in the programs using built-in functions that we call "trading functions."

Only EAs and scripts can use trading functions (only if the corresponding option is enabled in the EA/script settings). It is prohibited to use trading functions in custom indicators.


Simultaneous use

The programs also differ from each other by the amount of programs of different types simultaneously attached to a symbol window.

EA. You can attach only one EA in one symbol window; the simultaneous use of several EAs in one window is prohibited.

Script. You can attach only one script in one symbol window; the simultaneous use of several scripts in one window is prohibited.

Custom indicator. You can attach several indicators in one symbol window simultaneously; they will not interfere with each other.

Programs of all types can be launched simultaneously in one symbol window provided their compliance with the limitations of each type. For example, you can launch one EA, one script, and several indicators in one symbol window at the same time. However, you may not launch several EAs or scripts in one symbol window, whatever programs of other types are launched simultaneously.

At the same time, you may simultaneously launch programs of the same type in different windows of one symbol. For example, if you want to launch two EAs for one symbol, you can launch one EA in one window of this symbol and another one in another window of the same symbol. In this case, your EAs will work simultaneously. However, you must take into consideration that EAs launched in this manner may form contradictory trading instructions. For example, one of them can give instructions to open orders, whereas the other one can give instructions to close orders. This may cause a long sequence of useless trades that results in the total loss.

The programs of all types can create global variables available for all other programs launched in the client terminal, including those launched in the windows of different symbols. This allows the machine to coordinate the simultaneous operations of all programs. The order of using global variables will be specially considered in the section Global variables.


Calling programs for execution

Programs of all types can only be executed at the user's will. In MQL4, you cannot call an EA, a script, or an indicator for execution from within a program.

The only exemption is the built-in function iCustom() that allows you to refer to a custom indicator for some data, and to the functions of technical indicators. Referring to iCustom() or to the functions of technical indicators does not result in displaying the lines of indicators in the symbol window (see Simple programs in MQL4).

Property of the Program

EA

Script

Indicator

Run duration

Over a long period

One time

Over a long period

Trading

Allowed

Allowed

Prohibited

Displaying of lines

No

No

Yes

Simultaneous use of several programs of the same type in one symbol window

Prohibited

Prohibited

Allowed

Calling for execution from within a program

Prohibited

Prohibited

Prohibited

Table 2 Main properties of EAs, scripts, and custom indicators.

Thus, if we want a program that would manage trading according to a certain algorithm, we should write an EA or a script. However, if we want to have a certain dependence graphically displayed, we should use an indicator.