Variables are an important concept in programming. Variables allow you to store data to be reused at a later stage. Variables are names that can hold different types of value. You can store a piece of text, numbers, and a multitude of other data structures. In this guide, we will not talk about constants that are very similar to variables. The only difference between the variables and constants is that the latter are static, and their value cannot be modified during the program execution. On the other hand, variables can be changed at any time during the execution, and values can be recalculated or re-attributed.
In MQL4 and MQL5, there are three particularities you need to consider when declaring variables:
1. Variables must be declared before they are used
Before you can assign any value to a variable, you need to declare them. Let us take our Hello World! example. Instead of writing “Hello World!” many times, you can use a variable to store the text “Hello World!” (fig.31). To declare a variable, you need to specify the type of value you want to store and provide a unique name for your variable. Remember to not use any reserved words to name your variable. In fig.31 we provide two approaches to declaring a variable, with a variable declaration with a default value assignment on the left side. On the right side (fig.31), we show you how to declare without providing any default value – you can attribute that later in the program.
Fig.31 Declare the variable by providing the type of value followed by a unique name.
In fig.32, the use of the variable HelloWorldText whose value type is string contains the value “Hello World!” which is used in two different functions. Fig.33 shows you that variable value can be changed during the program execution.
Fig.32 HelloWorldText variable is reused within function OnInit and OnDeinit
Fig.33 HelloWorldText variable is modified after being used in the function OnInit.
2. Variable types
Variable types define the type of value to be stored, such as text, characters, numbers, etc.
Fig.34 Examples of different variable types declarations
To declare a variable, specify the type of data to store followed by the unique variable name (fig.34). There are various types of data fig.35 lists some of the most used.
|char||The char data type holds a single symbol or character, for instance, a letter, a digit, or a punctuation mark.|
|short||A short can contain a range of numerical values equal to 2 to the power of 16. 2^16 = 65,536. A short can contain integers of positive or negative values, within the range between -32,768 and 32,767.|
|int||An integer is very similar to the previous class. It holds values ranging from -2,147,483,648 to 2,147,483,647.|
|long||The long data type is a longer version of the integer data type which can hold values ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.|
|color||The color type is the data type-specific to MQL4 and MQL5. It stores information related to color: RGB values and color constants. The purpose of this variable is to interact graphically with chart elements and indicators.|
|datetime||The datetime data type holds date and time as the number of seconds elapsed since January 01, 1970. It is a peculiarity of MQL4 and MQL5. Date and time can be represented as a text or literal string which comprises 6 numerical parts, year, month, day, hours, minutes, and seconds.|
|bool||A boolean is a data type that is intended to contain a binary value, true or false, or numerically, 1 or 0, where 1 = true, and 0 = false.|
|float||A float data type is a double-precision floating-point number with up to 17 digits.|
|double||The double data type is a real type that is twice the precision of a float.|
|string||The string data type is used to contain text or a sequence of characters. Note that the string data type must be enclosed between double-quotes.|
Fig.35 List of most common data type
3. Scope and extent of variables
Scope and extent of variables in programming is the range or the region of the program where a declared variable exists and can be accessed.
|In MQL4 and MQL5, there are two places where variables can be declared:
Global variables are declared outside any functions, and their intent is to be accessible from the entire program. You can access the value of a global variable from any part of your Expert Advisor. Note that global variables are declared only once at the start of the program before the first event-handling function OnInit. Conversely, local variables are declared within functions, and therefore they can only be accessed and utilized within the scope of the function. In fig.36, we show you a practical example of the difference between global and local variables.
Fig.36 The difference between global and local variables.
4. Operations and expressions with variables
Now that you know how to declare and access variables values, we will now explain to you how to manipulate them and perform some basic operations. Some characters and character sequences are particularly important. These are what we name operation symbols. They are used for arithmetic operations, logical expressions, and assignments (fig.37).
Fig.37 Arithmetic, logical, and assignment operators.
Arithmetic operations are basic math operations such as addition, multiplication, and division (Fig.38).
Fig.38 Arithmetic operations
An assignment operation is a way to simplify the variable change of value by assigning the right variable to the left operand. For example, you want to assign variable y to be equal to variable x (fig.39), simply use the equal sign.
Fig.39 Variable value assignment
Fig.40 shows you some more advanced assignments coupled with arithmetic operations (Fig.40).
Fig.40 Arithmetic operations and value assignment
Logical operations are operations that manipulate Boolean values. Booleans are logical values and, therefore, binary, and can be either 1 or 0, or true or false (Fig.41).
Fig.41 Logical expressions
Note the use of exclamation mark (!) in front of a variable, which signifies a negation as demonstrated in fig.41, a != b; which expresses the statement a is not equal to b. In a more practical way, fig.42 demonstrates how logical expressions work. These are applicable to a simple moving average strategy:
The first IF statement compares the variable LastPrice with the variable MovingAverage. If the LastPrice value is bigger than (>) MovingAverage, then the variable IsAboveMovingAverage value is set to true.
The second IF statement is the exact opposite of the first IF statement. IsAboveMovingAverage value is set to false if the LastPrice is smaller than (<) the MovingAverage.
The third IF statement calls the function OpenSellOrder if AboveMovingAverage is false. Note the exclamation point (!) in front of the variable AboveMovingAverage, which indicates a negation.
The last IF statement calls the function OpenBuyOrder if AboveMovingAverage is true.
Fig.42 Practical example of Logical expressions
5. Array variables
An array is a data structure that can contain a collection of elements such as values and/or variables. Each element in an array can be identified by an index or key.
- An array contains a set of values of the same data type.
- Arrays can be one-dimension or multi-dimensional.
- An array can have a maximum of four dimensions.
An Array is declared almost in the same way as any other variables. It requires you to define a data type, followed by a unique name, and furthermore, the number of elements it can contain. Fig.43 is a visual representation of arrays, which (a) is a one-dimension array, (b) a two-dimensional array, and (c) a three-dimensional array.
Fig.43 One-dimension and multi-dimensional arrays.
In the same principle as variables, an array is required to be declared prior to its use in the program. The array can be either global or local. Fig.44 is an illustration of a declaration of a two-dimensional array.
Fig.44 Declaration of a two-dimensional array
To retrieve data from an array or to assign an element value to it, use the array coordinate-axis as in fig.45. Fig.46 shows you how to initialize a full set of data into an array. Note that initializing an array must be done only with constants of a corresponding type. One-dimensional and multi-dimensional arrays are required to be a single-dimensional sequence of comma-separated values.
Fig.45 Retrieve and assign an element value from an array.
Fig.46 Example of an Integer and double two-dimensional array and a Boolean one-dimensional array