Independent modification of programs for MetaTrader4 (part 2)

Continuing the series of articles on programming in MQL 4, started by in the 74th issue of ForTrader.org magazineWe have come right down to the most important thing of all - source code. So how do you understand this MQL 4 language? How do you learn to modify it? Most people have the most difficulties and fears with these very questions. However, if we're talking about the modification of the program, it's not so scary. The program in the language MQL4 is like a text in English, from which most of the words have been thrown out, leaving only the bare essentials.

forex-mql4

Other articles on programming in MQL4:

The Language of MQL4: Recalling English

In essence, reading the program boils down to simple translation of commands from English into Russian.

For example, consider this single line in MQL4:

if(counted_bars>0) counted_bars-;

Let's try to translate its text into Russian "If the counted bars are greater than 0, the counted bars are minus minus". The meaning of the first part of the phrase, I hope, is clear to everyone. It remains only to find out what "counted bars minus minus" means. A quick search on the Internet informs us that the code "variable minus minus" is nothing else but reducing the variable by one. Thus, the whole meaning of the received line becomes clear - if the number of the calculated bars is more than 0, then we reduce their number by one.

I want to draw your attention: we understand WHAT a line of code does, but we do not understand WHY and WHY it does it. Reading the source code often tells us only the sequence of commandsthat need to be accomplished. We can understand that this line does one thing and this line does another, but putting the whole puzzle together is often not as easy as it seems at first glance. And that's why, in the article "Before You Begin," we paid close enough attention to learning of the program algorithm.

In MQL4 language there are not many fundamental points that are difficult to understand without knowing about them beforehand. Let's look at the main ones.

Declaring variables

Variables are the workhorse of any program. In essence, variables are named RAM cells, in which the program stores its data. There are six different types of data in MQL4:

  • int - is an integer data type. Used to store integers.
  • double - is a floating point data type. Used to store fractional numbers.
  • string - String data type. Used to store strings.
  • color - color data type. Variables of this type store the color code.
  • datetime - date and time data type. Variables of this type store the date and time.
  • bool - is a logical data type. Used to store values of type "true" or "false".

Before you can use a variable you have to declare it, i.e. you have to tell the computer to allocate a memory location of a certain size and give it a name. The following command is used for this purpose: "type_variable name_variable;" - The type of variable is specified first, followed by a space and its name, then a semicolon.

At all, semicolon in MQL4 language - this is analogous to the dot in Russian. It is required after each "sentence" in the programming language.

Sometimes in the process of declaring a variable, an initial value is immediately assigned (i.e., they do not just tell the computer to allocate a RAM cell, but also tell it what value to put there). To do this, the operator = is placed after the variable name, but before the semicolon, and the initial value is given after it. Let's look at some examples:

int a; double b = 5.0; string hello = "Hello";

Note: Strings in MQL4 must be enclosed in double quotes.

In the future, to use a variable, you just need to specify its name, after which the computer itself will look in the RAM cell with that name and substitute the desired value. The only question that remains is how can variables be used?

Types of operators in MQL4

Variables in the MQL4 language usually is used in one of two possible ways: they are either involved in the operators, or are parameters of functions.

First, consider operators. In MQL4 a truly huge number of different operators are available - short commandswhich operate on variables in a certain way.

The most important operator, in a way, is the assignment operator. It is denoted by an equal sign ("="), with a variable on the left and the value to be written to that variable on the right. This value can be almost anything: another variable, a predefined expression, the result of a function, etc.

Consider a simple example:

a = 5;

This line should be read as follows "Put value 5 in the memory cell named a". Under no circumstances should it be read as "a equals 5"! This is completely wrong. In MQL4 there is a separate equality operator (more about it later). Here we just put some value into the memory cell.

In addition to the assignment operator, in MQL4 there are other operators. For example, arithmetic: + (plus), - (minus), * (multiply), / (divide). Example:

a = a + 5;

This line should read "Take the value of the memory cell named a, add 5 to it, and write the result to the memory cell named a", or just "increase the value of a by 5".

Another group of operators that we will consider is relation operators. The basic ones are: == (equal), != (not equal), > (more), = (more or equal), <= (less or equal). Relation operators check the truth of an expression and return true if it is true and false if it is false.

For example:

if(a == b) Alert("1" );

Read this code like this: "If the value of variable a equals the value of variable b, print a message with the text 1". As you may have guessed by now, Alert("1" ) is nothing else but message display command with the text 1. It will be shown if the value of a equals b, and will not be shown otherwise.

Using conditional statements

It's time to take a closer look. if. This is nothing less than conditional statement. It allows you to perform certain code sections, only if the condition in parentheses is true. For example, as discussed in the previous example, 1 will be displayed only if the value of variable a equals the value of variable b.

Sometimes parentheses are followed by a curly open bracket. For example:

if( a == b ) { a = a + 1; Alert( a ); }

Default, only the first command refers to the conditional operator, located after it (before the first semicolon). The rest of the code does not belong to the conditional operator. The presence of curly braces allows attribute the whole code to the conditional operatorwhich is located between them.

Sometimes, after the if statement you can see else operator. The else operator allows you to specify the commands that will be executed if the expression in parentheses of the if statement is false. Example:

if( a == b) Alert("A equals B" );

else Alert("A is not equal to B" );

Read this code as follows: "If the value of variable a equals the value of variable b - display the line "A equals B", otherwise - display the line "A does not equal B".

Cyclic operators

Another important type of operator is cycles. They allow you to repeat a specific set of commands some number of times. In the MQL4 language there is two loop types - for and while.

Consider the cycle while. Its syntax looks very simple:

while ( condition ) { commands;}

As long as the value of the "condition" expression is true, the commands will be executed. As soon as it becomes false, the cycle will stop. Let's look at an example:

int i = 0;

while( i < 10 ) {i++;}

Read this code as follows: "Create an integer variable named i and give it an initial value of 0. As long as i is less than 10, increase the value of i by 1." As soon as the value of i equals 10, the loop will stop.

A slightly more complicated example of a cycle is for. You can read about it in MQL4 documentation.

Function - program in miniature

Another important tool of the MQL4 language is the. Essentially, a function is a named block of code responsible for to solve a particular problem. It is a program in miniature: a function has its own input parameters, and it can also report the result of its work. Functions are very, very a powerful tool of any programming languagewhich adds internal structure to the code, as well as allowing you to divide tasks into subtasks, implementing the principle of modularity.

Functions in MQL4 can be custom и library. User functions - are the functions that the programmer independently introduces into their programby creating a description of them in the source code. We will not discuss this topic within the scope of this article.

Library functions - are functions, already certain for you inside the MQL4 language. To use such a function (or, to be more correct, to call it), you should specify its name, and then specify all parameters it needs in parentheses, separated by commas. For example,

Alert( "1" );

In this line we call the familiar Alert function, which displays a message on the screen. In parentheses we specify its parameters (in this case, the string "1").

What about the other functions? How do I know what function does and what parameters it has? The answer to this question can be found in the MQL4 documentation.

Warning! Function parameters must be passed in the same order and quantity as specified in the documentation. If a function accepts two arguments, it cannot pass three. If the function works with integers, it will not work with strings. Pay attention to this when you read the documentation!

How to apply it all?

In this article I have tried to give you the shortest, but at the same time succinct overview of What is the program in the MQL4 languageand what you need to do to be able to modify it yourself. Of course, most likely, you will not succeed at the first attempt. Here, as in any serious business (and programming is very serious), you need practice, practice and practice again.

It is best to start with simple indicators or scripts. You can figure out their algorithm, add a message output to the code (first a simple one, then you can add one before each meaningful block) in order to Overcome the first fear of modification. And then you can start editing or correcting things.

In my opinion, the important things in this question are 2 things:

1)  Don't forget documentation and Internet searches. Every time you come across a command you don't understand, open the documentation and look it up. Try to understand exactly what it does and how it can be "read" in Russian.

2)  Don't be afraid to ask on the forums. This can even be done on the forum of this magazine, ForexSystems.ruwhere there is a section on MQL4. Any incomprehensible piece of code can be posted there and ask more experienced participants to explain what it does and how it does it.

Armed with these two rules, you can understand almost any program. Of course, at first it can (and will) be quite time-consuming and demanding. However, as you gain experience, this process will become easier and easier. The main thing is continuous work. If you try to get to the bottom of it, you'll succeed!

If you want to learn MQL4 under the guidance of an experienced programmer and learn to create complete programs completely by yourself, then visit my website and sign up for a full-fledged MQL4 course, where we will study in detail "from" and "up".

Other articles on programming in MQL4:

Leave a Reply

Back to top button