It is a common knowledge that only equitype values can be added or subtracted.
For example, apples can be added to apples, but apples cannot be added to square meters or to temperature. Similar limitations can be found in most of modern algorithmic languages.
Like normal objects of life have certain types characterizing their color
(red, blue, yellow, green), their taste (bitter, sour, sweet), amount (one and a half, two, seven), MQL4 uses data of different types. Speaking about data type, we will mean the type of the value of a constant, of a variable and the value returned by a function (the notion of function is considered in the section of Functions).
In MQL4, the following types are distinguished (for the values of constants, variables, and the values returned by functions):
- int - integers;
- double - real numbers;
- bool - Boolean (logical) values;
- string - values of string type;
- color - values of color type;
- datetime - values of date and time.
The values of int type are integers. This type includes values that are integer by their nature. The following values are integers, for example: amount of bars in the symbol window (16000
bars), amount of opened and pending orders (3 orders), distance in points between the current symbol price and the order Open Price (15 points). Amounts representing such objects as events can also be integers only. For example, the amount of attempts to open an order cannot be equal to one and a half, but only to one, two, three, etc.
There are 2 types of integer values:
- Decimal values can consist of digits from 0 to 9 and be either positive or negative: 10, 11, 12, 1, 5, -379, 25, -12345, -1, 2.
- Hexadecimal values can consist of Latin letters from A to F or from a to f, digits from 0 to 9. They must begin with 0x or 0X and take positive or negative values: 0x1a7b, 0xff340, 0xAC3 0X2DF23, 0X13AAB, 0X1.
Values of int type must be within the range from -2 147 483
648 to 2 147 483 647. If the value of a constant or a variable is beyond the above range, the result of the program operation will be void. The values of constants and variables of int type take 4 bytes of the memory of a computer.
An example of using a variable of int type in a program:
int Art = 10;
int B_27 = -1;
int Num = 21;
int Max = 2147483647;
int Min = -2147483648;
The value of double type are real numbers that contain a fractional part.
Example values of this type can be any values that have a fractional part: inclination of the supporting line, symbol price, mean amount of orders opened within a day.
Sometimes you can face problems designating variables when writing your code, i.e., it is not always clear to a programmer what type (int or double) the variable belongs to. Let us consider a small example:
A program has opened 12 orders within a week. What is the type of variable A that considers the mean amount of orders daily opened by this program? The answer is obvious: A = 12 orders / 5 days.
It means that variable A = 2.4 should be considered in the program as double,
since this value has a fractional part. What type should be the same variable A if the total amount of orders opened within a week is 10? You can think that if 2 (10 orders / 5 days = 2) has no fractional part, variable A can be considered as int. However, this reasoning is wrong. The current value of a variable can have a fraction part consisting of only zeros. It is important that that value of this variable is real by its nature. In this case, variable A has also to be of double type. The separating point must also be shown in the constant record in the program: А =
The values of real constants and variables consist of an integer part, a decimal point, and a fractional part. The values can be positive or negative. The integer part and the fractional part are made of digits from 0 to 9. The amount of significant figures after decimal point can reach the value of 15.
27.12 -1.0 2.5001 -765456.0 198732.07 0.123456789012345
The values of double type may range from -1.7 * e-308
to 1.7 * e308. In the computer memory, the values of constants and variables of double type take 8 bytes.
An example of using a variable of double type in a program:
double Art = 10.123;
double B_27 = -1.0;
double Num = 0.5;
double MMM = -12.07;
double Price_1 = 1.2756;
The values of bool type are values of Boolean (logical) type that contain falsehood or truth.
In order to learn the notion of Boolean type, let's consider a small example from our everyday life. Say, a teacher needs to account the presence of textbooks of the pupils.
In this case, the teacher will list all pupils on a sheet of paper and then will tick in a right line whether a pupil has a textbook or not.
For example, the teacher may use tick marks and dashes in the table:
|List of Pupils
||Textbook on Physics
||Textbook on Biology
||Textbook on Chemistry
The values in right columns can be of only 2 types: true or false. These values cannot be attributed to either of the types considered above since they are not numbers at all. They are not the values of color, taste, amount, etc., either. However, they bear an important sense. In MQL4, such values are named Boolean, or logical, values. Constants and variables of bool type are characterized through that they can only take 2 possible values: true (True, TRUE, 1) or false (False, FALSE,
0). The values of constants and variables of bool
type take 4 bytes in the computer memory.
An example of using a variable of bool type in a program:
bool aa = True;
bool B17 = TRUE; bool Hamma = 1;
bool Asd = False;
bool Nol = FALSE;
bool Prim = 0;
The value of string type is a value represented as a set of ASCII characters.
In our everyday life, a similar content belongs to, for example, store names, car makes, etc. A string-type value is recorded as a set of characters placed in double quotes (not to be mixed with doubled single quotes!). Quotes are used only to mark the beginning and the end of a string constant. The value itself is the totality of characters framed by the quotes.
If there is a necessity to introduce a double quote ("), you should put a reverse slash (\) before it. Any special character constants following the reverse slash (\) can be introduced in a string. The length of a string constant ranges from 0 to 255
characters. If the length of a string constant exceeds its maximum,
the excessive characters on the right-hand side will be truncated and compiler will give the corresponding warning. A combination of two characters, the first of which is the reverse slash (\), is commonly accepted and perceived by most programs as an instruction to execute a certain text formatting. This combination is not displayed in the text. For example, the combination of \n indicates the necessity of a line feed; \t demands tabulation, etc.
The value of string type is recorded as a set of characters framed by double quotes: "MetaTrader 4", " Stop Loss",
"Ssssstop_Loss", "stoploss", "10 pips". The string value as such is the set of characters. The quotes are used only to mark the value borders. The internal representation is a structure of 8 bytes.
An example of using a variable of string type in a program:
string Prefix = "MetaTrader 4";
string Postfix = "_of_my_progr. OK";
string Name_Mass = "History";
string text ="Upper Line\nLower Line";
The value of color type is a chromatic value.
The meaning of 'color' (blue, red, white, yellow, green, etc.) is a common knowledge. It is easy to imagine what a variable or a constant of color type may mean. It is a constant or a variable, the value of which is a color. It may seem to be a bit unusual, but it is very simple, generally speaking. Like the value of an integer constant is a number, the value of a color constant is a color.
The values of color constants and variables can be represented as one of three kinds:
The value of color type represented as a literal consists of three parts representing the numeric values of intensity of three basic colors: red, green and blue (RGB). The value of this kind starts with 'C' and is quoted by single quotes.
The numeric values of RGB intensity range from 0 to 255 and can be recorded both decimally and hexadecimally.
Examples: C'128,128,128' (gray), C'0x00,0x00,0xFF' (blue), C'0xFF,0x33,0x00' (red).
- Integer Representation
Integer representation is recorded as a hexadecimal or a decimal number. A hexadecimal number is displayed as 0xRRGGBB where RR is the value of red intensity, GG - green, and BB - blue.
Decimal constants are not directly reflected in RGB. They represent the decimal value of a hexadecimal integer representation.
Representation of the values of color type as integers and as hexadecimal literals is very user-friendly.
The majority of modern text and graphics editors provide information about the intensity of red, green and blue components in the selected value of color. You have just to select a color in your editor and copy the values found in its description to the corresponding color value representation in your code.
Examples: 0xFFFFFF (white), 0x008000 (green), 16777215 (white), 32768 (green).
Fig. 11. Color parameters for literal and integer representation of the constant color value can be taken in modern editors.
- Color Names
The easiest way to set a color is to specify its name according to the table of web colors. In this case, the value of a color is represented as a word corresponding with the color, for example, Red - the red color.
Constants and variables of color type take 4 bytes in the computer memory. An example of using such a variable in a program:
color Paint_1 = C'128,128,128';
color Colo = C'0x00,0x00,0xFF';
color BMP_4 = C'0xFF,0x33,0x00'
color K_12 = 0xFF3300;
color N_3 = 0x008000; color Color = 16777215;
color Alfa = 32768;
color A = Red;
color B = Yellow;
color Colorit = Black;
color B_21 = White;
The value of datetime type is the values of date and time.
The values of this type can be used in programs to analyze
the moment of beginning or ending of some events, including the releases of important news, workday start/finish, etc. The constants of date and time can be represented as a literal line consisting of 6 parts that represent the numeric value of year, month, day (or day, month, year), hour, minute, and second.
The constant is framed in single quotes and starts with 'D'. It is allowed to use truncated values: either without date or without time, or just an empty value. The range of values: from January 1, 1970, to December 31, 2037. The values of constants and variables of datetime type take 4 bytes in the computer memory. A value represents the amount of seconds elapsed from 00:00 of the 1st of January 1970.
An example of using a variable of datetime type in a program:
datetime Alfa = D'2004.01.01 00:00';
datetime Tim = D'01.01.2004';
datetime Tims = D'2005.05.12 16:30:45';
datetime N_3 = D'12.05.2005 16:30:45';
datetime Compile = D'';
Variable Declaration and Initialization
In order to avoid possible 'questions' by the program about what type of data this or that variable belongs to, it is accepted in MQL4 to specify the types of variables at the very start of a program explicitly. Before a variable starts to participate in any calculations, it should be declared.
Variable Declaration is the first mentioning of a variable in a program. At declaration of a variable, its type should be specified.
Variable Initialization means assignment to it a value corresponding with its type at its declaration. Every variable can be initialized. If no initial value is set explicitly,
a numeric variable will be initialized by zero (0) and a string variable will be initialized by an empty line.
||In MQL4, it is accepted to specify the types of variables explicitly at their declaration. The type of a variable is declared at the first mentioning of the name of this variable. When it is mentioned for the second and all subsequent times, its type is not specified anymore. In the course of execution of the program, the value of the variable can change, but its type and name remain unchanged. The type of a variable can be declared in single lines or operators.
A variable can be declared in a single line:
This record means that there will be variable Var_1 (variable declaration as such) and the type of this variable will be int in the given program.
In one line, several variables of the same type can be declared:
int Var_1, Box, Comm;
This record means that there will be variables Var_1, Box
and Comm, all of int type, used in the program. It means that the variables listed above will be considered by the program as variables of integer type.
Variables can also be initialized within operators:
double Var_5 = 3.7;
This record means that there will be variable Var_5 of double type used in program, the initial value of the variable being 3.7.
The type of variables is never specified anywhere in the subsequent lines of the program.
However, every time the program calls a variable it "remembers" that this variable is of the type that has been specified at its declaration. As calculations progress in the program, the values of variables can change, but their type remains unchanged.
The name of a variable has no relation to its type, i.e., you cannot judge about the type of a variable by its name. A name given to a variable can also be used for variables of any types in different programs. However, the type of any variable can be declared only once within one program. The type of declared variables will not be changed during execution of the program.
Examples of Variable Declaration and Initialization
Variables can be declared in several lines or in a single line.
It is allowed to declare several variables of one type simultaneously. In this case, variables are listed separated by commas, a semicolon being put at the end of line.
Fig. 12. Example of variable declaration in a single line.
The type of variables is declared only once, at the first mentioning of the variable. The type will not be specified anymore for the second and all subsequent times when the variable is mentioned.
Fig. 13. Example of variable declaration in a single line.
It is allowed to declare and initialize variables in operators.
Fig. 14. Example of variable initialization.
Fig. 15. Variable initialization in the header of a compound operator.