Friday, February 27, 2009

Programming in MQL 4: Introduction.

In the previous issue of the journal Forex Magazine, we began a series of articles prior to release of a new version of the trading system Meta Trader 4. In this article we will try to describe the changes that have affected the built-in programming language MQL 4.

We will try to build their presentation in such a way that the material was understandable as the readers who have experience programming for the Meta Trader, and the first time introducing subtleties of translating their ideas in the language MQL 4.

Let's begin with a description of the program. Programs in the language MQL 4 consists of three main parts: the initialization, the implementation of the algorithm and deinitsializatsiya.
Initialization - phase, which is executed when you first start the program, it is arranged in the form of the function init. Usually performed during the initialization parameters and checking the internal variables are initialized for the correct operation of the program.

Deinitsializatsiya - the final stage of the program, it is arranged as a function of deinit. At this stage, if desired, you can remove it from the schedule implications of the program, or even perform some activities "zametaniyu traces.

The main stage of the procedure is to start. It is here that must be implemented an algorithm for your indicator, adviser, or a script.

By the way, since we are talking about the fact that the programs are divided into three types, will give a brief explanation of each. Those who already have programming experience, can skip the next paragraph.

Custom Indicators - programs that can display on the basis of historical data from different curves, histograms, or what any other useful information to help the trader to take resheniyaob opening and closing orders. Advisers or experts are called program that can perform trade operations and is often used for writing, mechanical trading systems. Scripts - a method often used an iterative algorithm, once programmed it. Scripts - not quite the program, and because they are not caused by themselves, but from the advisers or indicators, their relevance would be called the sub. The script has no stage of initialization and deinitsializatsii. It consists only of the procedure start.

Although the syntax of a programming language has changed, when not studying, it seems that to learn a new programming language. Go to the MQL 4 we would have called "peaceful revolution". The dramatic changes in the way of working with the parameters transmitted in the program, and already referred to the division of program into three phases. In our view, only those innovations will require a rethinking of the style of programming. More changes in the software interface, in particular the substantial increase in functionality, can be interpreted only as a huge step forward in order to provide programmers a convenient tool for implementing the unique zadumok!
To illustrate the changes in the syntax of the language, we have here is written on MQL 4 - it is already familiar to readers of previous editions Forex Magazine indicator "median smoothing." Note that the transfer program with MQL II on MQL 4 does not require much effort, even more, thanks to the emergence of a set of functions for working with arrays, the program easier. Instead of self-realization in sorting the array using the bubble, it is possible to use the built-sorting functions.

This will display on MQL 4 is commented in detail, and the reader is invited to independently deal with the fact that it was written in the program.

# define copyright "Copyright c 2004, Horn."
# define link "alexander@indus.ru"

/ / Global variables Advertisement
double g_ValuesArray [5];

/ / Custom indicator initialization function
int init (int Range = 5)
(
int err = 0;

/ / If the size of the array is different from what we
/ / Specified when creating a variable g_ValuesArray
/ / It is possible to change it in the execution of the program.
if (Range! = 5) (
err = ArrayResize (g_ValuesArray, Range);
if (err! = Range) (
Alert ( "Unable to allocate memory for an array");
err = -1;
)
)

return (0);
)

/ / Function deinitsializatsii custom indicator
int deinit ()
(
return (0);
)

/ / The main function of a custom indicator
int start ()
(
int shift = 0;
int ux = 0;
int size = ArraySize (g_ValuesArray);

for (shift = size; shift <(Bars - size); shift + +) (
/ / Puts the value from the array of Open
/ / Into a temporary array, which we will
/ / More work
ArrayCopy (g_ValuesArray, Open, shift, size);

/ / Sort the array but not by the bubble
/ / A built-in MQL4 aetrwbtq to sort massivav.
ArraySort (g_ValuesArray);

if (MathMod (size, 2)! = 0) (
SetIndexValue (0, shift, g_ValuesArray [(size - 1) / 2]);
) Else (
SetIndexValue (0, shift, (g_ValuesArray [(size / 2) -1] + g_ValuesArray [(size / 2)]) / 2);
)
)

return (0);
)

At the same time, we suspend our story on MQL 4 to the next issue of Forex Magazine. Let's hope that this article will help many get a first idea of a new programming language of custom indicators, functions, and advisers to the new information and trading system Meta Trader 4.




Alexander Ivanov
to Forex Magazine

No comments: