Saturday, March 14, 2009

Implementation of programs, types and structure

Programming language MQL 4: Implementation of programs, types and structure

Writing programs in any programming language requires a complete understanding of how and in what environment it will rabotat, and what is its user audience. If the audience with programs written in MQL 4 is almost always clear, since the first issue is to deal with separately. A number of comparisons in the usual program, performed on our computer, and programs written in MQL 4. First of all, attention should be drawn on who performs the instructions of a program.

When the regular program runs on your computer, its instructions are executed directly to the processor and operating system. If such a program needs to read the file from the disk, it makes a request to the kernel operating system, and have the kernel code is responsible for reading the file. If such a program needs to calculate the sum of two numbers, then it turns to the processor with a request to put two numbers. Thus, the normal program is executed, appealing directly to the processor and operating system. That is, the operating system is the environment program, a processor - a means of execution of the program guidelines.

Now consider the case where a program is written in MQL 4. We can not simply run a program on our computer, if it is not installed MetaTrader 4. This means that this program really is not a full-fledged stand alone executable module. The lion's share of what the program wants to make, not the program is executed, and the interpreter MQL-program client terminal Metatrader 4.
When the MQL-program needs to read the file from the disk, ona do not ask for the opportunity to have an operating system, as it would make "full" program, and asked the interpreter to refer to the operating system and read the file. If the MQL-program needs to put two numbers, then it can not lay down the processor skomandovat two numbers, it may ask interpritator that he appealed to the processor and the processor has already resigned two numbers. MQL-starts, and running the interpreter, built into the client terminal. Is it good or bad? For programs orietntirovannyh to work only with the MetaTrader 4, this option is suitable, as no other. It is through such an arrangement the terminal with user applications can easily be made available to MQL-program all the services available to the user in MetaTrader 4. The interpreter intercepts all calls MetaTrader'a functions and performs them with the core client terminal. Agree to call the program interface terminal
set of features and functionality, offers MetaTrader'om MQL-program. And note that the study of programming language - this is basically the study of interfaces available for implementation of their plans.

In addition, with MQL-accessible program interface terminal, the new version of the language was added to the opportunity to perform functions that are compiled in the so-called dynamic library that allows you to perform the functions written in other programming languages. Thus, writing their own dynamic library allows you to create user interfaces, and thereby expand the functionality MetaTrader'a. But it will be discussed later, and only when fully mastered the basics of programming in MQL 4, now it is worth noting is that many of the functions of the operating system available for use in such a way. To see a list of such functions are advised to look to the file WinUser32.mqh, supplied together with MetaTrader 4.

Now it is time to discuss the types of programs that we can write in MQL 4, they are divided into three types:
. Counselor;
. Custom indicator
. Custom script.

It is these three types of programs we proposes the creation wizard programs MetaEditor'e called by pressing Ctrl + N).

Counselor - MQL-program, which is attached to a certain schedule and running at every prevention, ie, after each price change. Counselors are typically used for writing, mechanical trading systems. Indeed, the very title of "advisor", says that this program is to suggest something to do or not do you change the price. The program type advisor is exclusive opportunity - at that time to commit the transaction. Such a possibility is lacking in other types of custom programs. Moreover, an adviser may make the transaction as a real and virtual. In the latter case, he notes in the chart is the place where the transaction could be accomplished without performing
actual operation, he would advise her to make.

Custom Indicator - MQL-program, which allows us to write our own technical indicator. Often, traders want to have a large set of indicators than that provided them with the developers of the terminal. To save the situation called MQL-program type of "user LED". In these programs, we can draw their own graphs and display text and graphics on the label character quotations. Indicators, as well as advisers, to display attached to the schedule of a commercial instrument, and performed at every price changes. The difference is that the custom indicators can not carry out any transactions.
As already noted, advisors and indicators are performed at each change in price. This oznayaaet that when we are attached to a graphic indicator of or an advisor, metatrader puts him in a sort of internal queue and when there is a change in price, all trapped in this particular program are executed alternately.

Custom script - MQL-program that is written to perform any specific action and is done only once. To re-execution of the script, it will need to explicitly call again Taka as he will not be executed, as with advisors and indicators.

As we have seen, all three types of MQL-programming differ, as a way of fulfilling their interaction with environment (ie MetaTrader'om) and destination.

Now proceed to consider the structure of the program.
As a rule, to write the program you want to use the creation of advisors (called by pressing Ctrl + N). Depending on the type of program, after all the steps of the implementation of the master creation adviser, if the adviser had been selected or custom indicator, you'll see the text with three empty functions or one function of an empty, if the script has been selected.

First of all, it is worth noting that the program for the MQL 4 clearly structured, and their implementation is divided into several stages.

Counselors and Displays include in their programs of three phases of implementation, which are represented by the functions init (), start () and deinit (). Scripts are the interpretation of the function start (). Such a structure can talk about what steps the program takes place from its attachment to the schedule to the point where it will be removed from the schedule of the user.

The life cycle of all programs, with the exception of scripts includes: initsialzatsiyu, the main part of the program and deinitsializatsiyu. These stages as time and realized functions init (), start () and deinit ().
Not hard to guess that the main work is performed in the function start (), because it runs every time there is a change in schedule, which is attached programma.Krome these functions, the program contains a header. In this title, except for comments, explanations of the founders of the program and their rights to the program, shows the parameters for program execution, such as: whether the indicator is running in the same window as the schedule of prices, or whether it will draw in a separate window, and other parameters . All details will be considered by us in the following articles.

Today all. Readers are advised to carefully look at and compare the sources of indicators and advisers included with MetaTrader 4 as examples. Very useful to read how to use MetaEditor 4 and learn how to compile the program.



Alexander Ivanov
to Forex Magazine
fxtrade@tomsk.ru

No comments: