MQL5 Lesson 2 - Variable Types

Greetings, friends! Variables are the foundation of all programming languages, because no matter what algorithm you come up with, how you implement it, or what language you write it in, in the end it all comes down to processing variables. In this lesson we will sort out variable types in MQL5.
Previous lessons
What are variables?
If you look at it from a practical angle, a variable is an area, or block, or memory cell to which we give a convenient name, put some data there, and by using the variable name we gave it, we can retrieve this data, change it, or pass it somewhere else.
Now let us create a variable:
char value_char;
The semicolon tells us that the statement has ended. A statement can contain one or more operations. In our case, this is one operation of creating or, as is commonly said, declaring a variable of type char named value_char.
Variable names may consist of letters of the Latin alphabet, and uppercase and lowercase letters are different symbols, of digits, and of underscores; they cannot contain special symbols such as =, #, \\ and so on. In addition, a variable name cannot start with a digit:
NAME1 namel Total_5 Paper
Mql5 inherited strict variable typing from C++. When we declare a variable, a certain amount of memory is immediately allocated for it, depending on the type of value that will be stored in this variable. And once a variable type is set, you can no longer stuff data of another type into it. Well, in principle you can, but the result may be unpredictable.
Now I will tell you what variable types there are in mql5 at all. So, there are numeric types, and there are the most of them. There are also string types, that is, character types, simply text. There are special types created for some non-standard operations and not fitting the description of the previous two. And finally, there are composite or user-defined types.
Numeric types are divided into integer and floating-point types.
Integer numeric types
There are 4 kinds of integer numeric types:
Why so many types? A different amount of memory is allocated for each variable type, and the range of values stored in each variable type will differ.
There are also unsigned values (literals):
The same amount of memory is allocated, but the values are strictly positive and 0. The English u before the type designation means unsigned. They are convenient to use for variables that cannot be negative. For example, for counting the number of orders and positions in a trading account. If you put into a variable a value that goes beyond the range, the result is unpredictable; more precisely, it will be within the specified range, but it can be any value at all.
In theory, all this was done to save memory. Memory for variables is allocated in RAM, of which modern computers usually have gigabytes. And the difference between 2 or 4 bytes is rather dubious savings. Nevertheless, in theory I can imagine a situation where such thrift could be justified. For example, when we have a great many variables, complex mathematical calculations, a bunch of loops, and all this runs on a weak VPS.
But in reality, most variable types are still used rarely and in rather specific situations. The most commonly used types are int, uint, and ulong.
Type bool
The next special type is bool. It occupies 1 byte in memory. Variables of type bool are also called logical: they contain one of two values, 1 or 0, true or false.
bool value_ bool = true;
Let us create several variables:
bool value_ bool_1 = true;
bool value_ bool_2 = false;
bool value_ bool_3 = 1;
bool value_ bool_4 = 0;
bool value_ bool_5 = -1;
bool value_ bool_6 = 777;
I assigned values to the first two variables using the keywords true and false. Keywords are sequences of letters and digits reserved in the programming language that can be used only for their direct purpose and in no other way. There are a great many such keywords in MQL5, we will come across them from time to time, and then I will explain them.
In this case, true means truth or 1, false means falsehood or 0. But the last two variables demonstrate that not everything is so simple, and a variable of type bool can be assigned not only 0 and 1. At the same time, only 0 is considered false; everything else is true.
Why is such a type needed at all? It is often used as flags and switches for working with various options, branching, and also for checking the correctness of certain actions or expressions.
Real Numeric Types
There are only two real numeric (numbers with a floating comma or point) types: float and double:
The difference between double and float is in the precision after the decimal point. For float the maximum precision is 24 digits after the decimal point, for double it is 53 digits.
The way computer memory works is more suitable for storing whole numbers. Fractional ones are stored in memory in the form of a power of an integer. For example, 0.25 is stored as 2 to the power of 2, but 0.3 is not stored so well; when it is taken out of memory, the original 0.3 turns into something like 0.300000001.
The String Type string
Now let us consider string variables of type string. They contain text strings. This is quite a complex data type. For working with strings in MQL5 there are more than a dozen different functions, which we will get acquainted with in other lessons.
string value_string = "ytsuken";
I used the assignment operator (=). It tells us that we assign some value to the variable. Now, when we refer to the variable value_string, it will return the value "ytsuken" to us until we overwrite this value.
In the previous lesson, you already saw comments. Now I want to point out that there are actually two types of comments: line and block. A line comment is in the form of two consecutive slashes //.
A block comment looks different. It is needed in order to comment an entire block of code at once. We put a slash and an asterisk /*, and at the end */.
The enum Type
The special enum type represents an enumeration or a named enumeration. It is declared a little more complicatedly than other variables.
Enum value_enum {num_1, num_2, num_3};
This notation says that the enumeration value_enum contains 3 numbered members and they can be referred to both by name and by ordinal numbers. In MQL5 there is a whole bunch of preset enumerations for all occasions. But we will examine this type in later lessons.
The datetime Type
The next special type is datetime:
datetime value_datetime;
This type contains a date and time, which are stored as the number of seconds that have passed since 00:00 on January 1, 1970. To work with this type, as with the string type, quite a few functions are provided, and a separate lesson will also be devoted to them. With the help of a variable of type datetime, you can, for example, determine the moment a new bar opens or set a trading time or a break in trading for the robot.
The color Type
The next special variable type is color:
color value_color;
This type contains a description of a color. You can assign it a value in three different ways. For named web colors, this can be done with predefined keywords:
color value_color = clrBlue;
The web colors themselves can be viewed in the MQL5 documentation.
You can also assign a value to a variable as three decimal numbers or as a single hexadecimal number. But this is not always convenient.
The color and datetime types make sense only for the convenience of presenting and entering parameters set from the outside, from the properties table of an expert advisor or a custom indicator (the "Inputs" tab). Data of the color and datetime types are represented as integers.
User-Defined Variable Types
These are complex data types such as classes and structures. Calling them variables or even data types is not entirely correct. User-defined data types are closely connected with the concept of OOP, or object-oriented programming, about which we will have a separate lesson or even several. There is no point in going deeper into this now.
Declaring Variables
That is all for variable types. Now we will move on to declaring variables. We have already declared many variables, but the point is that variables of the same type can be declared like this:
int a=3, b, c;
Print("Variable a=", a);
Print("Variable b=", b);
Print("Variable c=", c);
Result: a=3, b=3, c=3.
int a, b=3, c;
Print("Variable a=", a);
Print("Variable b=", b);
Print("Variable c=", c);
Result: a=1, b=3, c=3.
int a, b, c=3;
Print("Variable a=", a);
Print("Variable b=", b);
Print("Variable c=", c);
Result: a=1, b=1, c=3.
When declaring variables on one line, those that we initialized explicitly have the values assigned to them. Those that were not initialized have the previous value.
Code in a program is executed from left to right. A new line is sort of on the right. In theory, the entire program can be written on one line.
Type Casting
Converting one data type to another is called type casting. It is done implicitly and explicitly. Let us assign a value of another type to a variable of one type:
int a = c, b = 12, c=3;
char d = 120;
Print("Variable a=", a);
In the Print example, implicit conversion occurs. We pass an int, and the compiler itself converts it to text.
a = d;
The compiler does not complain in this case. The value range of a is larger than that of d.
If d = a, we get a warning, because the range of a is larger.
To avoid an error, you can explicitly cast the type: d = (char)a; the error will not disappear, but there will be no warning.
Variable Scope
A variable declared inside any function is local. The scope of a local variable is limited to the boundaries of the function inside which it is declared. Local variables are located in the temporary memory area of the corresponding function.
Global variables are placed outside any function. Global variables are defined at the same level as functions; they are not local in any block. The scope of global variables is the entire program. Global variables are available from all functions defined in the program.
The input modifier is specified before the data type. The value of a variable with the input modifier cannot be changed inside an MQL5 program; such variables are read-only. Only the user can change them from the program properties window.
That is all, bye everyone!
Respectfully, Dmitry aka Silentspec
Tlap.io
No algorithm can do without variables. Today we will talk about MQL5 variable types, the features of each variable type, variable scopes in the MQL5 language, and converting one type to another. Video lesson
Comments