Tuesday, February 24, 2009

Programming in MQL II. How do you find an error?

This is for several months, the magazine Forex Magazine publishes material on programming in the language MQL II, built in the trading terminal MetaTrader, and many of you have certainly mastered sufficient knowledge to write their code.

But copy the text of the articles of the magazine - is one thing, but able to write correctly running programs - very different.

Often, especially in the early stages of programming, it happens so that all thoughts and ideas are already embodied in the form of code, the program has successfully passed the stage of compilation (in the Metadata Editor program, this process is done by pressing the button verify), but the program does not work or does not work quite right . The programmer begins to rethink what is written - to step into his head trying to imitate actions of the computer that something may be, rules, compiles again and again gets a bad result - the program does not work as desired. Failures may discourage deal with an interesting case for many newcomers, and here it probably should be noted that it was not entirely correct to expect that if your program has been the compilation stage, she performs all right, that is, as intended .

It is not that we started programming lacks certain expertise in the subject area, in fact further assumed that all the necessary knowledge and skills already available from previous releases Forex Magazine or from any other sources. The main source of problems of this kind is inattention, haste, a false representation of a particular function or other causes which together can be called "human factor".

So, proceed to practice - take the example given in the published in the 8th edition Forex Magazine article "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]);
);

The above code should draw a graph, shown in blue in Figure 1:

But, suppose that during the recruitment program, we randomly for one reason or another made a mistake, and instead of the cycle in lines 12-14, used a series of incorrect attribution of values sorted array:

for ix = 0 to 4 (
values [ix] = O [shift];
)

Here we are wrong or just forgot that, instead of O [shift] you want to use O [shift + ix], and the result obtained is not a timetable. Figure 2 in blue drawn the correct schedule, and the red line - the schedule incorrectly written indicator:

Just think - it could cross stitch work and hours of reflection! And I assure you that to find such a mistake would be not so easy by conventional razglyadyvaniya code.

What you can find the error? The easiest way would be to display information about the full array for analysis. Fortunately the developers MetaTrader'a added this option in the MQL II, and the program itself.

After filling the array in a loop, we can put it in the window Journal of MetaTrader. Just add the code in the instruction print () with the correct options.

Add the following line after the above before the cycle of filling an array:

print (values [0] + "" + values [1] + "" + values [2] + "" + values [3] + "" + values [4]);

The command print () MetaTrader'u said that in the log window you want to withdraw its parameters - ie, that placed in parentheses following it. Figure 3 shows that each execution cycle of the main program is output to the log line with the values of the array values.

In particular, the second row from above tells us that, 2004.04.24 expert median2 sent in the log window the following message: "1.2545 1.2545 1.2545 1.2545 1.2545. This is how we understand the values, respectively, the first, second, third and so on the array values. The fact that the values of all elements of the array values are identical, although, according to the schedule of quotations, they should have been different, we should be alerted.

After analyzing this, we can understand that the code contains errors, and she admitted to the above lines, which is a command print (). Now, we need to look at how to be a localized area code, which potentially has the bug and fix it. The correct code is printed in the magazine text shown in Figure 4 - this time in an array of the correct values are entered.

This method of debugging a lot of opportunities, as well as the command print () can print both the number and the line, and even the date and time. So every time you are not completely sure of the correctness of their actions can bring some value to the log and then manually verify their accuracy.

But always remember - every little bit extra command slows down the program.

For example, if you are debugging a large program, and you need a lot of similar findings in the journal, the abundance of instructions print () can significantly slow down the operation of the indicator and the system as a whole. It is clear that the debug and recheck the program many times, you can safely remove all unwanted instruction print (), but there are situations where, for example, the indicator is still under construction, but you are already using them in daily market analysis.

In such cases, may help conditional if (). If you make a variable

var: debug (false);

and then each call debug print () to put into the body of a conditional operator if, then, simply changing the value of debug, you can enable or disable the execution of the instruction of the code.

For example, in the case of debugging information from the fact that the variable debug is initialized when you create the value false (false) will not be displayed:

for ix = 0 to 4 (
values [ix] = O [shift + ix];
)
if (debug) then (
print (values [0] + "" + values [1] + "" + values [2] + "
"+ values [3] +" "+ values [4]);
)

Conversely, it is enough to debug when you create a variable set to true, that is to write var: debug (true); then debugging information will appear in the magazine.

At the same time, we finish our story about debugging your programs by using the print () and the terminal window Journal. But worth noting that this is not all ways of debugging, and in the next issue we will look at other opportunities to debug programs in the language MQL II executed in MetaTrader'e.




Alexander Ivanov
to Forex Magazine

No comments: