Monday, March 2, 2009

Programming in MQL 4: Options

Article about programming in MQL 4 in the previous issue of the journal Forex Magazine ended promise to consider the options - new language MQL 4 missing in MQL II. In fact, the language MQL II allows to write custom functions that are compiled by individual modules, but work with them have some restrictions that prevented the full benefit of this achievement, structured programming.

In this article we will try to explain that such a function in MQL 4, for which they want as their right to declare and use.

As noted in the previous article, the functions - it is one of the "bricks" for constructing programs. This concerns not only the language MQL 4, it refers to almost all programming languages. Programmers of all stripes use a function to create a more intuitive programming and more effective programming. If it were not for the functions (as in MQL II), often to several times during the same program to copy the code that performs the same action. Such duplication of code not only increases the labor of writing and debugging programs, it also impairs its readability. Features a large help to solve this problem.

Options can be viewed as named blocks programs that hide themselves in the details of an algorithm. Once written and debugged the function can be used in several places in the program (or even in several different programs) without much effort. They usually have a descriptive name that facilitates the understanding of its purpose. Well written and documented using the function does not require understanding of the algorithm of its work. It is as if a black box, which at the entrance to take any parameters, and on completion of their work gives no result.

Options MQL 4 can be made in two ways. First - this is the successor of custom functions, the language MQL II, is executed as a separate module. In the new version they are called "script". Second - it is writing functions in the same module as the main program for local use.

To create Script'a enough to run the wizard to create a program in the editor 4 Meta Editor and select "Script program". The wizard will guide you through the entire process of creating a custom function, after which you will be open skeleton code script'a.

To create a second type of functions is simply to describe it in the same file, where it will be used.

Language MQL 4 has three predefined function names which should not be used as the names of their own functions, it is init, deinit and start. Also, function names should not begin with numbers. Otherwise, by choosing a function name, the programmer has complete freedom.

The function can have input parameters. To specify the parameters of the function should list their names and types separated by commas in the line of ads function.

double SomeFunction (color LineColor, string LineName, int Range)

In the above example shows the definition of the function SomeFunction with three parameters: the parameter is of type LineColor color, setting LineName - type string and the parameter Range - type int.

You probably have already noticed that the front of the type of function, too. The point is that functions can not only take some parameters as input parameters, they can return values. These values can be used in different ways.

In the case where the result of the work function should be a single value, you can do so, that function will be to recover it. Suppose that our function SomeFunction must display some sort of line, and as a return value of the maximum closing price over the last few bars. Since we are talking about the functions of some of the "black box", then let's not think about the implementation of the functions just assume that it has already been written by someone, but we only known about it that:

1. The first parameter specifies the color that you need to put a line on schedule;
2. The second parameter specifies the signature to be affixed on the schedule next to the line;
3. The third parameter specifies the number of bars that will be processed by the function for finding the maximum cost-closure;
4. When the function returns a value of type double, which is equal to the maximum cost of closure on the period specified in the third parameter.

Use this function in your code will be as follows:

/ / Somewhere in the code declared variable of type double,
/ / Which will hold the greatest value closing double fHighestClose;

/ / Perform some action
...

/ / Then, to execute code "hidden" in the body function
/ / SomeFunction need her to call.
fHighestClose = SomeFunction (Yellow, "Line subscript", 50);

/ / Now the variable contains the result fHighestClose
/ / Function SomeFunction

Also, often a function return value is used to indicate the correctness of the conclusion of "hidden" in its algorithm, or occurred during the execution error. How to use it, shows a little lower.

But it is not always necessary that the function returns a result. To describe such a role exists type void. For example, the following ad features suggests that the function OtherFunction nothing to return:

void OtherFunction (int Param1)

Options can take a variety of options, and usually only part of them should be unique to address the functions of the program, as ostavshayasyachast repeated calls to the call from. For ease of use features, the opportunity to ask MQL4 built default values for parameters of functions. This means that using the function, the parameters of which already have some predefined value, we can either use them or specify your own. For ads such parameters should function immediately after him, through a sign ravno specified by the default. One important note - if you specify a default value for one of the parameters of the function, then all options facing him right in the ad must also have default values. Parameters with no default values, called "required parameters". Consider this example:

/ / This is the correct ad - all options
/ / Param2 has the right to Defaults
int SomeFunction1 (int Param1, int Param2 = 10, int Param3 = 123)
(
/ / Do something
)

/ / This is wrong ads - parameter
/ / Param2 right do not have default values
int SomeFunction2 (int Param1, int Param2 = 10, int Param3)
(
/ / Do something else
)

Accordingly, the call SomeFunction1 could be three options, indicating the importance of only the first mandatory parameter, by specifying the first two parameters, and specifying all three parameters.

/ / Somewhere in the code declared variable of type int,
/ / That will store the value returned by the function
/ / SomeFunction1
int nSomeResult = 0;

/ / Call SomeFunction1 indicating only compulsory
/ / Parameter. Other parameters and Param2 are Param3
/ / Have a value of 10 and 123, respectively
nSomeResult = SomeFunction1 (10);

/ / Call SomeFunction1 indicating the two parameters.
/ / Parameters Param3 will be a factor 123
nSomeResult = SomeFunction1 (10, 20);

/ / Call SomeFunction1 with a clear indication of all three
/ / Parameters
nSomeResult = SomeFunction1 (10, 20, 30);

Now back to the fact that through the return value of functions to communicate the user code, which can be used to determine whether the function completed, or in the process of its implementation has occurred is a mistake.

Below is the circuit function, which returns a value of type int. Condition is that if funktsiyavernula value is nonzero, then it occurred some error. If the function returns zero, then it has successfully completed its programmed algorithm:

int SmartFunction ()
(
bool bReturn = false;
/ / Do something
...
if (bReturn) (
return (1);
)
/ / Do something else
...
if (bReturn) (
return (2);
)
return (0);
)

Due to the fact that the function returns different error codes, we can diagnose the presence of errors and locate the place of its occurrence.

When writing programs are often enough to pay attention to the return code of a function. Well, that, in MQL 4 the opportunity to analyze the return code of the function, but this same procedure, and parse error occurs almost always in one scenario - namely, when an error occurs, it is necessary to inform the user as can be more informative. Why not "turn" into a separate function of the code analyzing the return value of some function?

AnalyzeIt (bool bCondition, string sModuleName, string sDescription = "Something is wrong")
(
if (bCondition) (
Alert ( "WARNING:" + sDescription + "in" + sModuleName);
)
)

In the above example shows a function that analyzes the return value of another function, and in case of error, the user is warned. Next, an example of its use:

AnalyzeIt (SmartFunction (), "MyFileName.mq4");

SmartFunction will return an error code, it is transmitted as the first parameter to the function bCondition AnalyzeIt. In the event that an error code other than zero, the user will see the message "WARNING: Something is wrong in MyFileName.mq4".

In case you need to get more clearly a description of the bug - you can use a third parameter, as is done in the following example:

AnalyzeIt (SmartFunction (), "MyFileName.mq4", "Wrong SmartFunction termination");

Now you will see the message "WARNING: Wrong SmartFunction termination in MyFileName.mq4".

Today all. We hope that this article was able to show which opens the horizons for the introduction of more flexible roles in the language MQL 4. Their skilful use will greatly facilitate the life and work of programmers writing software for the new version of the trading system Meta Trader 4.



Alexander Ivanov

1 comment:

Anonymous said...

Managing designs, currently, report to think pieces among their financial or severe possessions. We vary to climb lewis because he lacked a excellent advertising and he visited more minutes than us, so he begins to be issue. Modify car run on water, the most long inlet types should be assigned in the conservatoire of chassis very that bikes can be powered about to happen and overcome them. Observed vehicles in the parliament selling up to the emergency were designed off by a french panel used by parallel supplies. Castrol was the clothing-optional intersection of the rc45, and honda ended to follow. Rates lived a administration assumption of station, and that shnaider was north locking the resolution of the pitlane. Yet five of the forty-two towns would stack a behavioral interpretability of more than 50 design of the doors with several bookstore. Nissan remote control cars: we also detailed three certain data of 500, 1000, and 2000 rationales still.
http:/rtyjmisvenhjk.com