Sunday, February 22, 2009

Programming in MQL II. Strong program

Starting to write this article I reflect on the desirability of continuing the conversations about MQL II. Many believe that with the imminent advent of MQL4 - an entirely different programming language for MetaTrader'a (see Forex Magazine May 2004, ? 15, "The Future of the trader's toolbox") should not pay more attention to such "old" as the MQL II. I would like to prevent you from such thoughts, especially among those who are only starting or planning to learn programming in MetaTrader'e.

I'll try to defend their views:

First out of the Quartet are planning only in the summer and it may happen that, in anticipation of MQL4 prosidet will have at least a month, that is a whole month without a cause;
Secondly, to regard newcomers, even if you will now take up the study of MQL II, and wait for the "quartet" Take at once for MQL4, then I can assure you that the study of the programming itself will still begin with the same phases;
The third wonderful MQL II language in order to begin programming, it is not a gift to the schools to study science simple languages like Pascal, but not fashionable object languages, although the latter in many ways more effective;

I hope I have convinced you that continue to talk about MQL II stands, and we turn to the questions that sooner or later stand up to all programmers - how to make the program more resilient to unexpected situations.

For some, this issue may seem a bit frivolous, what could be easier: write a program, debug, run, running. So argue those who believe that the programs are written only by those of their Use.

In fact, with the growing popularity of such activities as work on the stock markets, and growing demand for professionals possessing the skills of writing programs for analytical systems for traders.

Now let us compare two situations: first, when people write software itself as it uses votraya when the trader does not wish for one reason or another to enter into the subtleties of programming programmer described the essence of their ideas, pay money and expect that it will provide a working program.

What happens when a program of force majeure? Certainly in the first case, people write a program to understand what's wrong and immediately corrected code gets corrected, to work with this unforeseen circumstance, the program. In the second case, the trader is likely to be concerned about the incident, it is possible that unforeseen circumstance cost him a certain amount of loss. He explained the programmer what's wrong and will wait until he corrects the mistake. It takes time, and with it, as they say, and money. Does not that unpleasant situation for both the programmer and for a trader?

How may take unforeseen circumstances? Let us turn our attention to the parameters of custom functions, indicators and experts. Each time the program is the user's input some parameters and a possibility of unforeseen circumstances. The work program in one way or another designed for some of the assumptions imposed on the parameters. Consider the examples.

Suppose that in our program you must enter the initial price to make some calculations. We do not even allow the thought that someone of the users try to enter in the price of the currency value of 999999999 or 0, because we have not seen yet such quotations on any of the currency pairs. But it turned out that when you enter the wrong user or not to read those instructions, which we wrote to him and eventually the program what Wade got the results. We seemed to do with it, but the user has been disappointed.

Then look at another similar example. Let us turn to more than once mentioned in the magazine smoothing algorithm for constructing a curve using the median smoothing (Forex Magazine April 2004, ? 14, "How do you find a mistake," March 2004, ? 8, "Programming in MQL II: median smoothing)

var: shift (0);
array: values [5] (0);
var: ix (0), iy (0);
var: is_sorted (true);
SetLoopCount (0);
for shift = 0 to bars-5 (
/ / Puts the value from the array of Open
/ / Into a temporary array, which we will
/ / More work
for ix = 0 to 4 (
values [ix] = O [shift ix];
)
/ / Sort the array using the bubble
for ix = 0 to 4 (
is_sorted = true;
for iy = 0 to 3 (
var: tmp (0);
if (values [iy]> values [iy 1]) then (
tmp = values [iy];
values [iy] = values [iy 1];
values [iy 1] = tmp;
is_sorted = false;
);
);
if (is_sorted) then (
break;
);
);
SetIndexValue (shift, values [2]);
);

From the second row shows that the values we have used an array containing five elements. From this it follows that the smoothing occurs median selected from the five elements. Having experience with the smoothing curves moving average (LED Moving Average), users are likely familiar with, that can arbitrarily change the amount of smoothing. What if you need to make the indicator, which is able to build a median smoothing at different intervals of 5. Unfortunately MQL II has not been able to create arrays dynamically change their sizes, so as one of the exits from this situation, you can use a large amount of which will be used for placing therein sized elements. In the next example, we have distributed an array of 20-y cells.

var: shift (0);
array: values [20] (0);
var: ix (0), iy (0);
var: is_sorted (true);
SetLoopCount (0);
for shift = 0 to bars-5 (
/ / Puts the value from the array of Open
/ / Into a temporary array, which we will
/ / More work
for ix = 0 to range - 1 (
values [ix] = O [shift ix];
)
/ / Sort the array using the bubble
for ix = 0 to range - 1 (
is_sorted = true;
for iy = 0 to range - 2 (
var: tmp (0);
if (values [iy]> values [iy 1]) then (
tmp = values [iy];
values [iy] = values [iy 1];
values [iy 1] = tmp;
is_sorted = false;
);
);
if (is_sorted) then (
break;
);
);
if (mod (range, 2)! = 0) then (
SetIndexValue (shift, values [(range - 1) / 2]);
) Else (
SetIndexValue (shift, (values [(range / 2) - 1] values [(range / 2)]) / 2);
)
);

We see that except for cosmetic changes to the program with an array sortable, added a single parameter - range, which enables the user to choose the amount of the median smoothing. Figure 1 shows the result of the modified indicator with the range taking values 5, 6 and 7.

But an array nevertheless limited and there is a danger that the user enters values undesirable for our program. It is not known how the program behaves when the user wants to set the range = 50. One of the troubles that can happen is when a program such as one's own will work out a contingency, and you will be entered into a confusion that the meaning and range = 30, and on the value range = 40 to obtain the same schedule.

Fig.1

More logical behavior of the program in both cases could be achieved if we were in the top block checking options. In this case, if the parameters do not meet certain requirements of the program could be prevented user error input by using the Alert () and then indicate the permissible value. The following is working code that will respond to the wrong user input properly, and informing him about it, finds the nearest acceptable value of the variable range.
Around the same input parameters can be done in the first situation. Now, the user will be warned of the error entry and will not be disappointed by unexpected results of the program.

In this issue we have covered only a portion of the material on the writing of sustainable programs in the future, we will try to address other aspects of the topic. Understand the material and poeksperementirovav examples you may find ways to further protect the users of your program from unforeseen situations.

The results of this paper should be understood that the program - it is not only to set out an algorithm. Yet it must be able to correctly process a variety of unexpected situations, and mastery of the programmer, only highlights the ability to anticipate and prevent such situations.

input: range (5);

var: shift (0);
array: values [50] (0);
var: ix (0), iy (0);
var: is_sorted (true);
var: real_range (0);
SetLoopCount (0);
for shift = 0 to bars-1 (
SetIndexValue (shift, 0);
);
real_range = range;

for shift = 0 to bars-5 (
/ / Puts the value from the array of Open
/ / Into a temporary array, which we will
/ / More work
for ix = 0 to real_range - 1 (
values [ix] = O [shift ix];
)
/ / Sort the array using the bubble
for ix = 0 to real_range - 1 (
is_sorted = true;
for iy = 0 to real_range - 2 (
var: tmp (0);
if (values [iy]> values [iy 1]) then (
tmp = values [iy];
values [iy] = values [iy 1];
values [iy 1] = tmp;
is_sorted = false;
);
);
if (is_sorted) then (
break;
);
);
if (mod (real_range, 2)! = 0) then (
SetIndexValue (shift, values [(real_range - 1) / 2]);
) Else (
SetIndexValue (shift, (values [(real_range / 2) - 1] values [(real_range / 2)]) / 2);
)
);

Alexander Ivanov

No comments: