• No results found

Program Structure

In document mql4-book (Page 70-75)

In first sections we learned some basic notions of the programming language MQL4. Now let's study how a program is organized in general. To solve this problem we will study its structural scheme.

As already mentioned above, the main program code written by a programmer is placed inside user-defined and special functions. In the section Functions we have discussed the notion and properties of built-in and user-defined functions. Shortly: a user-defined function has a description, function call is used for starting its execution in a program. Any built-in or user-defined function can be executed only after it is called; in such a case the function is said to be called for execution by a program.

Properties of special functions are described in details in the section Special Functions. Here we will study only the main information about them. Special function is a function called to be executed by the client terminal. As distinct from common functions, special functions have only description and special functions call is not specified in a program. Special

functions are called to be executed by the client terminal (there is also a technical possibility to call special functions from a program, but we will consider this method incorrect and will not discuss it here). When a program is started for execution in a security window, the client terminal passes control to one of the special functions. As a result this function is executed.

The rule of programming in MQL4 is the following:

A program code must be written inside functions.

It means, program lines (operators and function calls) that are outside the function cannot be executed. At the attempt to compile such a program, MetaEditor will show the corresponding error message and the executable file *.exe will not appear as a result of compilation.

Let's consider the functional scheme of a common program - Expert Advisor:

Fig. 31. Functional scheme of a program (Expert Advisor).

The largest blocks of a program written in MQL4 are:

1. Head part of a program.

2. Special function init().

3. Special function start().

4. Special function deinit().

5. User-defined functions.

Further we will analyze only the inner content of these functional blocks (integral parts) of a program, while all external objects (for example, informational sphere of the client terminal or hardware) will be out of our scope of interest.

Information Environment of MetaTrader 4 Client Terminal

Information environment of the client terminal MT4 is not a component part of the program.

Information environment is a set of parameters available to be processed by a program. For example, it is a security price that has come with a new tick, volume accumulated at each new tick, information about maximal and minimal prices of history bars, parameters that characterize trading conditions offered by a dealing center, etc. Information environment is always saved and at each new tick is updated by the client terminal connected with the server.

Program Structure Head Part

Head part consists of several lines at the beginning of a program (starting from the first line) that contain some writings. These lines contain general information about the program. For example, this part includes lines of declaration and initialization of global variables (the necessity to include this or that information into the head part will be discussed later). The sign of the head part end may be the next line containing a function description (user-defined or special function).

Special Functions

Usually after the head part of the program special functions are described. The special function description looks like the description of a usual user-defined function, but special functions have predefined names: init(), start() and deinit(). Special functions are a block of calculations and are in relations with the information environment of the client terminal and user-defined functions. Special functions are described in details in the section Special Functions.

User-Defined Functions

The description of user-defined functions is usually given after the description of special functions. The number of user-defined functions in a program is not limited. The scheme contains only two user-defined functions, but a program can contain 10 or 500, or none. If no user-defined functions are used in a program, the program will be of a simplified structure:

head part and description of special functions.

Standard Functions

As mentioned earlier, standard functions can be presented only as a function call. Standard functions, like special and custom functions, have description. However, this description is not given in a program (that is why not included into the scheme). The description of a standard function is hidden, not visible to a programmer and therefore cannot be changed.

Though it is available to MetaEditor. During program compilation, MetaEditor will form an executable file, in which all called standard functions will be executed correctly to the full extent.

Arrangement of Parts in a Program

The head part should be located in first lines. The arrangement of special and user-defined functions descriptions does not matter. Fig. 32 shows a common arrangement of functional blocks, namely - the head part, special functions, user-defined functions. Fig. 33 shows other program structure variants. In all examples the head part comes first, while functions can be described in a random order.

Fig. 32. Usual arrangement of functional blocks in a program (recommended).

Fig. 33. Possible ways of arranging functional blocks in a program (random order).

Please, note:

None of the functions can be described inside another function. It is prohibited to use in a program function descriptions located inside another function.

Below are examples of incorrect arrangement of function descriptions.

Fig. 34. Examples of incorrect arrangement of function in a program.

If a programmer by mistake creates a program where description of any of its functions is located inside the description of another function, on the compilation stage MetaEditor will show an error message and an executable file will not be created for such a program.

Code Execution Sequence

Head Part and Special Functions

From the moment of a program execution start in a security window, lines of the program head part are executed.

After the preparations described in the head part are done, the client terminal passes controlling to the special function init() and the function is executed (control passing is shown at the structural scheme in large yellow arrows). The special function init() is called for execution only once at the beginning of the program operation. Usually this function contains a code that should be executed only once before the main operation of the program starts. For example, when the init() function is executed, some global variables are initialized, graphical objects are displayed in a chart window, messages may be shown. After all program lines in the init() function are executed, the function finishes its execution and control is returned to the client terminal.

The main program operation time is the period of operation of the function start(). In some conditions (see features of special functions in the section Special Functions), including the receipt of a new tick by the client terminal from a server, the client terminal calls for execution the special function start(). This function (like other functions) can refer to the information environment of the client terminal, conduct necessary calculations, open and close orders, i.e. perform any actions allowed by MQL4. Usually when the special function start() is executed, a solution is produced that is implemented as a control action (red arrow).

This control can be implemented as a trading request to open, close or modify an order formed by the program.

After the whole code of the EA's special function start() is executed, the function start() finishes its operation and returns control to the client terminal. The terminal will hold the control for some time not starting any of special functions. A pause appears, during which the program does not work. Later, when a new tick comes, the client terminal will pass control to the special function start() once again, as a result the function will be executed and after its execution is finished, control will be returned to the client terminal. On the next tick the function start() will be started by the client terminal once again.

The process of multiple call of the special function start() by the client terminal will be repeated while the program is attached to a chart and can continue for weeks and months.

During all this period an Expert Advisor can conduct automated trading, i.e. implement its main assignment. At the scheme the process of multiple execution of the function start() is shown by several yellow arrow enveloping the special function start().

When a trader removes an Expert Advisor from a chart, the client terminal once starts the special function deinit(). The execution of this function is necessary for the correct termination of an EA's operation. During operation a program may, for example, create graphical objects and global variables of the client terminal. That is why the code of the function deinit() contains program lines, execution of which results in deletion of

unnecessary objects and variables. After the execution of the special function deinit() is over, control is returned to the client terminal.

Executed special functions can refer to information environment (thin blue arrows at the scheme) and call for execution user-defined functions (thin yellow arrows). Note that special functions are executed after they are called by the client terminal in the order predefined in function properties: first init(), then multiple call of start() and after that deinit(). Conditions, at which the client terminal calls special functions, are described in the section Special Functions.

User-Defined Functions

User-defined functions are executed when call to a user-defined function is contained in some function. In this case control is timely passed to the user-defined function and after the

function execution is over, control is returned to the place of call (thin orange arrows at the scheme). Call of user-defined functions can be contained not only in the description of a special function, but also in the description of other user-defined functions called from it. One user-defined functions may call other user-defined functions - this user-defined functions calling order is permissible and widely used in programming.

User-defined functions are not called for execution by the client terminal. Any user-defined functions are executed inside the execution of a special function that return control to the client terminal. User-defined functions may also request (use) for processing variable values of the client terminal information environment (thin blue arrows at the scheme).

If a program contains description of a user-defined function, but there is no call of this function, this user-defined function will be excluded from the ready program on the compilation stage and will not be used in the program's operation.

Note: special functions are called for execution by the client

terminal. User-defined functions are executed if called from special or other user-defined functions, but are never called by the client terminal. Control action (trading orders) can be formed both in special and user-defined functions.

In document mql4-book (Page 70-75)