Directory of RSS feeds
Statistics

RSS feeds in the directory: 2798

Added today: 0

Added yesterday: 0

Business / Finance

MQL4: Fixed errors and warnings when compiling in MetaEditor

Portal Forex trader 24.08.2018 at 09:54

Forex trading strategies, expert advisors, indicators, video training trade

Developing expert advisors in MQL4 is not a very easy task. First- algorithmization of any complex trading systems for oneself is a problem because you need to consider many details, beginning with TC and ending with features specific MetaTrader 4 environment. Second, even the presence of a detailed algorithm does not eliminate difficulties that arise during the transfer of the developed algorithm in the programming language MQL4.

the Compiler has some help in writing the correct experts. After the start of the compilation MetaEditor will report all syntax errors in your code. But unfortunately, in addition to the syntax errors in your EA may contain logic errors that the compiler cannot catch. So this is what we have to do for themselves. How to do it — in our current material.

the Most common compilation errors

If there are errors in the code, the program cannot be compiled. For full control of all errors, it is recommended to use strict compilation mode which is set by the Directive:

#property strict

This significantly simplifies the search for errors. We now turn to the most common errors at compile time.

the Identifier matches a reserved word

If name of variable or function coincides with one of the reserved words:

int char[]; // wrong

int char1[]; // correct

int char() // wrong

{

return(0);

}

the compiler displays the error messages:

To correct this error you need to fix the name of the variable or function. I recommend the following system for naming:

All functions must indicate the action. That is, it should be a verb. For example, OpenLongPosition() or ModifyStopLoss(). Because the function always doing something, right?

furthermore, functions will be named so-called CamelCase style. And the variables in cebab_case style. It is common practice.

speaking of variable names. Variables are nouns. For example, my_stop_loss, day_of_week, current_month. Not so bad to name a variable with a long name, it is much worse to call it unclear. What is the dow, the Dow Jones index? No, it turns out, the day of the week. Of course, today you and so it is clear what this variable is. But when you open the EA code a month later, all will not be so obvious. And this time, lost on decoding messages from the past – it is necessary to you?

Special characters in the names of variables and functions

Go ahead. If names of variables or functions contain special characters ($, @, period):

int $var1; // wrong

int, @var2; // wrong

int var.3; // wrong

void f@() // wrong

{

return;

}

the compiler displays the error messages:

To correct this error, again you need to adjust the names of variables or functions, or well just call them human. Ideally, the code you need to write so that even a person who does not know programming, just read it and realized what was going on.

the Error of using the switch statement

the Old version of the compiler allowed to use any values in expressions and constants in switch statement:

void start()

{

double n=3.14;

switch(n)

{

case 3.14:

Print("Pi");

break;

case 2.7:

Print("E");

break;

}

}

In the new compiler expressions and constants switch statement must be integers, so when using such structures errors:

So when you disassemble code the classics, such as WallStreet, Ilan and other netlenki (which is very useful for self-development), it is possible to encounter this error. It is treated very simply, for example, using this line:

switch(MathMod(day_48, 10))

so you can easily solve the problem:

switch((int)MathMod(day_48, 10))

Return values of the functions

All functions, except void, must return a value of declared type. For example:

int function()

{

}

When strict compilation mode (strict) error:

In compilation mode by default, the compiler displays a warning:

If the return value of the function does not correspond to ad

int init()

{

return;

}

Then, in strict compilation mode, an error occurs:

In compilation mode by default, the compiler displays a warning:

To correct such errors in the function code only need to add a return statement return c return value of the appropriate type.

the Arrays in the function arguments

Arrays arguments to functions are passed only by reference. Earlier it was not so in the old advisers can meet this error. Here's an example:

double ArrayAverage(double a[])

{

return(0);

}

This code when strict compilation mode (strict) will cause an error:

In compilation mode by default, the compiler displays a warning:

To correct such errors should explicitly specify transmission of the array, adding the prefix & before the array name:

double ArrayAverage(double &a[])

{

return(0);

}

by the Way, the constant array (Time[], Open[], High[], Low[], Close[], Volume[]) can't be passed by reference. For example, the call:

ArrayAverage(Open);

regardless of the mode of compilation produces an error:

To eliminate such errors need to copy the required data from the constant array:

//--- array for storing the values of open prices

double OpenPrices[];

//--- copy the values of open prices to the array OpenPrices[]

ArrayCopy(OpenPrices,Open,0,0,WHOLE_ARRAY);

//--- call the function

ArrayAverage(OpenPrices);

One of the most common mistakes – the loss EA indicator. In such cases, usually the expert users on the forums angrily write: "the EA does not work!" or "Put the EA on a chart and nothing happens!". The solution to this question is actually very simple. As always, just look on the Journal tab of the terminal and find there an entry like:

2018.07.08 09:15:44.957 2016.01.04 00:51 cannot open file

'C:Users1AppDataRoamingMetaQuotesTerminal

MQL4indicatorsKELTNER_F12.ex4' [2]

this Tells us that the indicator in the folder, forgot to lay, or it is named differently. If the indicator is missing, need to add it to the folder indicators. If it is, check its name in the EA code – most likely there it is called differently.

compiler Warnings

compiler Warnings are informative and are not error messages, but they indicate possible error sources and to correct. Clean code should not contain warnings.

the intersection of the global and local variables

If the global and local levels, there are variables with the same name:

int i; // global variable

void OnStart()

{

int i=0,j=0; // local variables

for (i=0; i