Starting to write a program in MQL4, the programmer 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 3 types of application programs:
Expert Advisors, scripts, and custom indicators. Any program developed by a programmer will belong to one of these types. They all have their purposes and special features. Let's consider these types in more details.
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 Expert Advisors is the programmed control over trades. Expert Advisors are coded by users. There are no built-in EAs in the client terminal.
Script is a program coded in MQL4 and executed by the client terminal only once. Scripts are intended to perform any allowed operations that should be executed only once. Scripts are coded by users. They are not delivered with the client terminal as built-in programs.
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 dependences as lines. Indicators cannot trade. There are two types of indicators: technical (built-in) indicators and custom indicators. Indicators are considered in more details in the sections of Usage of Technical Indicators and Creation of Custom Indicators.
The programmer chooses the type of the program to be written depending on what is the purpose of this specific program and on properties and limitations of programs of different types.
Properties of Programs
Launching a Program for Execution
There is a criterion that distinguishes Expert Advisors and custom indicators from scripts. This is their run duration. In the section Some Basic Concepts, we mentioned already that programs were launched the amount of time that is multiple of the amount of ticks.
This statement is true for EAs and custom indicators, but it is false for scripts.
Expert Advisor 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, and, upon completion, passes the control to the client terminal, i.e., switches to the tick-waiting mode.
If a new tick comes when the program is being executed, this does not make any effect on the program execution - the program continues being executed according to its algorithm and 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, but only those that income when the control is in the client terminal and when the program is in the tick-waiting mode.
The new tick launches the program for execution. Thus, an Expert Advisor 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 multiple of the amount of incoming ticks).
Besides, an Expert Advisor 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 Expert Advisor makes necessary preparations (function init()) and switches to the tick-waiting mode to start function start(). Unlike EAs, a custom indicator both executes function init() and calls function start() one time to make the first, preliminary calculation of the indicator value. Later on, at a new tick, the program is launched by calling only function start(), i.e., operators are executed according to the algorithm of function start().
Script. Unlike Expert Advisors 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 on time. 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, for example, to open or close orders, to display texts on the screen, to install graphical objects, etc.
The differences in execution of Expert Advisors, scripts and custom indicators is determined by the properties of their special functions the will be considered in more details in the section Special Functions.
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, close, or modify orders. Trading instructions are formed in the programs using built-in functions that we call "trading functions".
Only Expert Advisors and scripts have the right to 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.
The programs also differ from each other by the amount of programs of different types simultaneously attached to a symbol window.
Expert Advisor. You can attach only one EA in one symbol window; the simultaneous use of several Expert Advisors is prohibited.
Script. You can attach only one script in one symbol window; the simultaneous use of several scripts is prohibited.
Custom indicator. You can attach several indicators in one symbol window simultaneously, they will not interfere with each other.
The 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 indicator in one symbol window at the same time, or one EA and one indicator. 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 the programs of the same type in different windows of one symbol. For example, if you want to launch two Expert Advisors 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 Expert Advisors 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 instruct to close orders. This may cause occurring of 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 GlobalVariables.
Calling Programs for Execution
Programs of all types can only be executed at the user's will. In MQL4, you cannot call an Expert Advisor, a script, or an indicator for execution programmatically.
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. The referring to function 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).
Table 2. Main properties of Expert Advisors, scripts, and custom indicators.
|Property of the Program
||Over a long period
||Over a long period
|Displaying of lines
|Simultaneous use of several programs of the same type
|Calling for execution programmatically
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.