Programming language MQL4
Writing a program in any programming language requires a full understanding of how and where it will work, and what will be the user group. If with the MQL4 programs’ users everything is more or less clear, then with the first question should be studied out. We will make a comparison of the usual program, working at our computer, with program, written in MQL4. First of all it should be noted who is carrying the instructions of the one or another program.
In the case, when usual program is run at the computer, its instructions are executed directly by processor and operating system. If such program needs to read a file from disk, then it refers a request to the operating system kernel and then the kernel executes the code, responsible for the file’s reading. If such program needs to sum up two figures, then it refers to the processor with a request to sum up two figures. In such a way, the usual program is executed, appealing directly to the processor and operating system. In other words, the operating system is a program’s context and the processor is an execution’s mean of the directives.
Now, let have a look at the case, when the program is written in MQL4. We can not just run such program on our computer, if there is not installed MetaTrader 4. This means that this program, actually, is not a full separate executable module. The most of the things which program wants to do are executed not by the program, but by the MQL-programs’ interpreter of the client terminal MateTrader 4. When MQL-program needs to read a file from disk, it does not request such capacity from the operating system, as it will be done by a “full” program, but asks interpreter to request the operating system and to read the file. If MQL-program needs to sum up two figures, it can not command processor to sum up these two figures, but it can ask interpreter to request the processor and only then the processor will sum up two figures. MQL-program is run and executed by the interpreter, embedded to the client terminal. Is it good or bad? For the programs oriented to the work only with MetaTrader 4, this variant fits, like no other. Just due to such organization terminal work with user programs, all services, available in MetaTrader 4, can be easily placed to MQL-programs’ disposal. The interpreter traps all function calls of MetaTrader and executes them with the help of client terminal’s kernel. Let us name the function package and facilities, provided by MQL-programs of MetaTrader, as program interface. And note, that study of the programming language is, generally, the study of interfaces, available for the software implementation of the plans.
Besides the fact that for MQL-programs the interface of the terminal is available, to the new version of the language, the possibility to execute functions, which are compiled to the so-called dynamically uploaded libraries, was added, this allows to execute functions written in other programming languages. In such a way the writing of your own dynamically uploaded library allows to create by yourself the user interfaces and thus to extend the functionality of MetaTrader. However this thing can be discussed only when all programming basics in MQL4 will be learnt; now, it should be noted that a lot of operating system’s functions are available for use only by this method. We advise you look to the WinUser32.mqh file, distributed with MetaTrader 4, in order to see the whole list of these functions.
Now it is time to discuss the types of the programs, which we can write in MQL4, they are divided on three types:
- - Advisor;
- - User indicator;
- - User script.
These three types of programs are provided by the wizard of the program creation in MetaEditor (by accelerating keys of Ctrl+N).
Advisor – MQL-program, which is attached to a certain chart and is executed at every tick, i.e. after each price change. Advisors usually are used for writing a mechanical trading system. As a matter of fact the name “advisor” tells about the fact that such program is made for advising something to do or not to do at every price variance. Such program as advisor has a unique option – to make transactions during its execution. Other types of the user programs do not have the same option. Moreover, the advisor can make deals really as well as virtually. In the last case, it points at chart the place where the deal could be made, it sort of advises to do it.
The user indicator – MQL-program, which allows us to write our own technical indicator. It is a usual situation when traders want to have a bigger number of indicators, rather than it is provided by the terminals’ developer. MQL-programs of “user indicator” kind are called to save this situation. In these programs we can draw our own charts and put text and symbol marks on the quotations’ chart. Indicators, as well as advisors, are attached to the chart of the some trading tool for displaying and are executed at every price change. The differences are that the user indicators can not make deals.
As it was noticed before, advisors and indicators are executed at every price movement. This means that when we attach indicator or advisor to some chart, MetaTrader puts it on the certain queue and when the price changes, all programs which are in this queue are executed one-by-one.
The user script – MQL-program, which is written for the running of certain actions and is executed only once. To do the script for a second time, it is needed to start it once again, because it will not be done by itself, as it is going with advisors and indicators.
As we see, all three types of MQL-programs are differ, by the methods of interaction with executing environment (i.e. MetaTrader), as well as by their purpose.
Now, let have a look at program structure.
As a rule, for program writing it is needed to use the wizard of advisors creation (by accelerating keys of Ctrl+N). Depending on the program type, after all steps of the advisors creation wizard are done, in the case if advisor or user indicator were chosen, the text with three empty functions will appear or with one function, if the script were chosen.
First of all, it is worth to say that the programs in MQL4 are clearly structured and their execution is divided into several steps.
Advisor and indicators contain in their programs three steps of execution, which are represented by the functions init(), star() and deinit(). Scripts are consist only of the function start(). Such structure can show which steps are taken by the program from its attachment to the chart till the moment when it will be taken away from the chart by the user.
The life-cycle of all programs, except scripts, includes: initialization, starting of the program’s main part and deinitialization. These steps are realized by the functions init(), start() and deinit().
It is not difficult to guess that the most part of work is executed in the function start(), because it is executed every time, when chart changes, to which the program is attached. Apart from the mentioned functions, the program has a heading. In this heading comments-explanations about program’s developers and their rights for the program are mentioned, as well as execution of a program, such as: will be indicator executed in the same window as price’s chart or it will be drawn in the separate window and other parameters. All details will be considered in the next articles.
Moreover, it is recommended to look carefully and compare the original codes of indicators and advisors, supplied along with MetaTrader 4 as examples. It will be very useful to read how to use MetaEditor 4 and learn how to compile programs.
2013-05-24 14:30:08 GMT+00 8 hours, 51 min. ago
2013-05-24 13:42:23 GMT+00 9 hours, 39 min. ago