Wednesday, March 4, 2009

Moving programs to MQL MQL II 4

In forums on programming for MetaTrader'a sometimes appear unhappy with remarks on the incompatibility between the new language MQL 4 language MQL II, used in earlier versions of the popular trading platform. Many users have a set of favorite MetaTrader'om custom indicators written in MQL II and, of course, be saddened by the fact that these indicators will not work in the new version of the trade information system MetaTrader 4. But it turns out, not so dramatic as it might seem. If there is a handy indicator of the source code to MQL II, a shift indicator on the new platform will not take too much time, and can be done is not even a man skilled in programming.



In this article we try to give advice, which can facilitate the process of converting "old" indicators on the new platform.



The first thing I would like to note - this will change the set of functions of language MQL 4.



Naturally, the change that has the language MQL, aimed at creating a more user-friendly tools and environment for writing programs. In this case, it would be logical to expect from the developers only enhance functionality and other features of language, rather than vice versa. This means that any feature of language MQL II duplicated version of the language MQL 4.



The new version of the language MQL supports a large set of functions and that are not entangled in the abundance, function names, with rare exceptions, it helps to understand which group belongs to a particular function. For example, the names of all the functions for working with arrays begin with the prefix Array, and the names of mathematical functions - with the prefix Math. In this regard, not all features in the new version of the language are exactly the same name as the old programs, but almost certainly can say that their "twin" exists in the new version.



The second point to be addressed - is that the program advisors and indicators are now divided into three mandatory functions: init (), start () and deinit (). We already wrote about this in one of the rooms Forex Magazine (Forex Magazine, June 7, 2004, ? 20 "Programming in MQL 4: Introduction.").



The majority of indicators includes a cycle in which each bar is calculated value of the indicator. Indeed, this cycle is precisely the place which is considered an indicator, but that was before this cycle, and sometimes beyond - is the code, "serving" its correct working. If you transfer the program to MQL 4, the entire cycle, most likely, no additional changes needed to be put into the body of the function start ().



Initialize variables, which takes place before completing the cycle, can be placed in the function init ().



The input parameters of the indicator, which were advertised in the MQL II with the keyword inputs, should be converted to options function init ().



Function deinit (), is likely to remain empty, as well as programs for MQL II often did not contain any action to address the effects of their work.



Third, what is required to make when transferring the program to a new version of the language MQL, - this is the syntax to bring it into conformity with the requirements of MQL 4. This process will try to describe the steps:



Step 1:



All operator brackets "begin" and "end" replaced by "(" and ")" respectively. All those who are already used when writing programs in the MQL II braces as operator brackets have already saved themselves from the extra effort at this stage of the transfer program to a new language. Next you need to remove all occurrences in the future program on MQL 4 language official word MQL II - the word "then".



Step 2:



We change all the logical operators "and", "or" and "not" to "&&", "| |" and "!" respectively. If someone has used in programs for MQL II operators "&" and "|", then do not forget, and they also translate into "& &" and "||", respectively.



Step 3:



The peculiarity of the operator "=" in the MLQ II was that in places where the result of its expected logical, comparison of values was carried out, rather than assigning a variable facing the left side of the value of the right-hand side. That is, in such situations, the operator "=" play the role of the operator "==". This is common in an "If" and "While". In MQL 4, in order to exclude the ambiguity, the developers clearly separated these two operators, and now, if you want to compare two values, use the operator "==". This observation is very important and should not be ignored in any case. Otherwise, a new version of the indicator will deliver you to the unexpected "surprises" every time instead greeted by comparing two variables of their assignment.



Step 4:



Previously, when writing programs in the MQL II could not think about how to write the name of a variable or an operator - using capital letters or lowercase? Compiler did not care whether the program is written, "While" or "while". Programmers to decide in accordance with their preferences registration code, how to write not only the names of variables, but also the official language of the word.



The new version of the language MQL, the situation has changed, and now, if you write "If", then the compiler will try to interpret it as a variable name, rather than as the beginning of the operator of choice, and could not find the announcement of such a variable, will return an error. Therefore, one should pay attention to it, and when transferring programs to MQL 4 to ensure that operators in the writing of "if", "while", "for" and others do not use capital letters. The same attention should be drawn to the fact that the register of letters plays a role in the ads of variables and functions. That is, if you declare a variable as "Var1", then it will not turn on behalf of "var1" or "VAR1". The compiler will accept each of the three names as the names of the different variables.



Given all this, the following, correct from the standpoint of MQL II, code:



Variable: Var1 (0);

Variables: Var2 (0.), Var3 (0.)



/ / ...

/ / Perform some action

/ / ...



If ((Var1
((VAR2 = 0) Or (VAR2 = 0)))

then Begin

/ / Perform some action

End;



would look like this:



int Var1 = 0;

double Var2 = 0;

double Var3 = 0;



/ / ...

/ / Perform some action

/ / ...



if ((Var1
((Var2 == 0) | | (Var2 == 0)))

(

/ / Perform some action

);



Step 5:



Fifth step could be asked to "carry" variables for the clusters where they are used, and thus divide their ads on the global and local, regarding the scope of functions. But this is one step that does not give the program new functionality, but will improve its readability and, perhaps, increase the speed of its work. At the first stage, you can do without this step, simply by placing obyavlniya all variables in global scope, that is, declaring them immediately before the announcement of the first function in your source program.



If all the above steps have been carefully executed, the lamp should compile and work.



Of course, do not predict, and while moving indicators MQL 4 there will be some moments that will require an individual approach, but, in general, the procedure for transfer of indicators with MQL II on MQL 4 should be consistent with the procedure described in the article.



For example, transport the reader is invited to compare the version indicator "median smoothing", written in MQL II and MQL 4, and published in the 15 th and 20 th issues of the journal Forex Magazine.









Alexander Ivanov

to Forex Magazine

No comments: