MQL4 Book  Basics of MQL4  Some Basic Concepts

Some basic concepts

The subject of our interest is a program written in MQL4. Before we start a detailed presentation of the rules of writing programs, it is necessary to describe the basic concepts that characterize a program and its interrelations with the information environment. The MetaTrader 4 Client Terminal works with an online connection. The situation on financial markets changes continually, which affects symbol charts in the client terminal. Ticks provide the client terminal with information about price changes on the market.

The notion of a tick

A tick is an event that is characterized by a new price of the symbol at some instant.

Ticks are delivered to every client terminal by a server that is installed in a dealing center. As appropriate to the current market situation, ticks may be received more or less frequently, but each of them brings a new quote—the cost of one currency expressed in terms of another currency.

An application operating within the client terminal may work over a long period of time, for example, several days or weeks. Each application is executed according to the rules set for programs of a certain type. For example, an Expert Advisor (EA) does not work continuously all the time. An EA is usually launched at the moment when a new tick comes. For this reason, we do not characterize tick as just a new quote, but as an event to be processed by the client terminal.

The duration of an EA's operation depends on what program code is included in the EA. Normal EAs complete one information-processing cycle during some tenths or hundredths of a second. Within this time, the EA can have processed some parameters, made a trading decision, provided the trader with some useful information and so on. Having finished this part of its work, the EA goes to waiting mode until a new tick comes. This new tick launches the EA again, the program makes its appropriate operations again and returns to the waiting mode. The detailed description of how the appearance of a new tick influences program operation follows next.

The notion of control

Control is the term used to speak about the flow of code execution within a program, as well as the flow between the program and the client terminal.

Control is the process of carrying out actions preset by the program algorithm and the client terminal features. Control can be transferred within the program from one code line to another one, as well as from the program to the client terminal.

Control is transferred in a way similar to one speaker giving the floor to another speaker at a meeting. Like speakers at a meeting, the client terminal and the program transfer control to each other. At that, the client terminal dominates. Its status is higher than the status of the program, like the authority of the chairman is higher than that of an ordinary speaker.

Before the program is launched, the control is under the supervision of the client terminal. Once the program is launched and a new tick is received, the client terminal transfers the control to the program. The program code starts to be executed at this moment.

The client terminal, after it has transferred the control to the program, does not stop its operation. It continues working with maximal performance during the entire period of time it is launched on the PC. The program can only start operating at the moment when the client terminal has transferred control to it (like the chairman of a meeting controls the meeting all the time it is going on, whereas the current speaker takes the floor for only a limited period of time).

After it has completed its operation, the program returns control to the client terminal and cannot launch itself on its own. However, while the program has control, the program determines when to return control to the client terminal. In other words, the client terminal cannot regain control from the program by itself. Dynamic actions of the user (for example, forced termination of the program) are an exception.

When discussing the matters of performance and internal structures of programs, we are mostly interested in the part of control that is transferred within a program. Let us refer to the following figure, Figure 2. It shows the general nature of transferring control to, from, and within a program. The circles shown in the figure characterize some small, logically completed fragments of a program, whereas the arrows between the circles show how control is transferred from one fragment to another.

Fig. 2. Transferring Control in a Program

Figure 2 Transferring control in a program.

An executing program, which is the one that has accepted control from the client terminal, starts to take some actions according to its inherent algorithm. The program contains lines of code, which determine the order of program execution. The general order of program execution consists in sequential transfer of control from one line to another in the top-down direction. In following sections, we will consider what can be written in program lines of code, and according to what rules it can be written.

Here, it is only important to emphasize that every logically completed fragment is executed—for example, some mathematical calculations are made, a message is displayed on the screen, a trade order is formed and so on. Until the current fragment of the program is executed, it retains the control. After it has been fully completed, the control is transferred to another fragment. Thus, control within a program is transferred from one logically completed fragment to another as they are executed. As soon as the last fragment is executed, the program will return control to the client terminal.

The notion of comment

A program consists of two types of records: the code that makes up the program itself, and the explanatory texts to the program code.

A comment is an optional and non-executable part of a program that explains the code.

So, comment is an optional part of a program. It means that a ready program will work according to its code irrespective of whether there are comments in it or not. However, comments facilitate understanding of the program code very much. There are one-line and multi-line comments. A one-line comment is any sequence of characters following a double slash (//) on the same line. The sign of a one-line comment is ended by line feed. A multi-line comment starts with the characters of /* and is ended by */ (see Figure 3).

icon_information

Comments are used to explain the program code. A good program always contains comments.

Fig. 3. Exemplary Comments in a Program.

Figure 3 Example of comments in a program

Comments are widely used in coding. They are usually displayed in gray. We will use comments, too, in order to explain our codes and make them more intelligible.