Data Types

What does the Data type mean?

Any programming language gives different names of the memory representation of the data.
For example if the memory holds numbers between -2147483648 to 2147483647, the most of the programming languages names this data as “Integer” data type.
Variables?

Variables are the names that refer to sections of memory that stores data.
To help you understanding the concept, imagine the memory as a cabinet (Figure 1) with a different size of boxes. The box size is memory storage area required in bytes.




.
Figure 1 - cabinet

In order to use a box to store data you have to tell the computer that you want to use the box; this process is known as declaration.
In the declaration phase you use a word to tell the computer what’s the kind and the size of the box you want to use, this word known as keyword.
And as any new born baby you have to give him a name (It helps if you give the box a meaningful name), this name is the variable constant.
Data is placed into a box by assigning the data to the box.
You can set the value of the variable (box) you created in the same line of declaration; this process is known as initialization.

When we create a variable we are telling the computer that we want him to assign a specified memory length (in bytes) to our variable, since storing a simple number, a letter or a large number doesn't occupy the same space in memory, so the computer will ask us what’s the kind of data and how much the length of the data? This is what the Data type for.
For example if we talk to the computer saying this line of code:

1
int  MyVaraible=0;

That’s mean we are asking the computer to set a block of 4 bytes length to our integer variable named “MyVaraiable”.
In the previous example we have used:

int<-Keyword
int <-Integer data type.
int<- Declaration
MyVaraible <- Variable’s constant.
=0 <- Initialization

We will know more about variables in a coming lesson.
In MQL4, these are the kinds of Data types:

  • Integer (int)
  • Boolean (bool)
  • Character (char)
  • String (string)
  • Floating-point number (double)
  • Color (color)
  • Datetime (datetime)  

1-  Integer

An integer, is a number that can start with a + or a - sign (can be positive or negative) and is made of digits. And its range value is between -2147483648 to 2147483647.
MQL4 presents the integer in decimal or hexadecimal format.
For example the next numbers are Integers:

1
2
12321340, -230;
0x0A0x120X120x2f0xA30Xa30X7C7;

We use the keyword int to create an integer variable.

For example:

1
2
3
int intInteger 0;
int intAnotherIntger = -100;
int intHexIntger=0x12;

Decimal and Hexadecimal:

Decimal notation is a method writing of numbers in the base of 10, and uses digits (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9) to represent numbers.
These digits are frequently used with a decimal point which indicates the start of a fractional part, and with one of the sign symbols + (plus) or − (minus) to indicate sign.
Hexadecimal is a numeral system with a base of 16 usually written using the symbols 0–9 and A–F or a–f.
For example, the decimal numeral 79 can be written as 4F in hexadecimal.

2- Bolean

Boolean variable is a data type that can hold only two values, true or false (or their numeric representation, 0 or 1). And it occupies 1 bit of the memory.
In MQL4, false,FALSE,False,true,TRUE and True are equals.
Boolean named like this in the honor of the great mathematician Boole George.
We use the keyword bool to create a boolean variable.

 For example: 

1
2
3
bool MyBoolVar_1 true;
bool MyBoolVar_2 1;
bool MyBoolVar_3 FALSE;

3- Character

MQL4 names this Data type “Literal”.
A character is one of 256 defined alphabetic, numeric, and special key elements defined in the ASCII (American Standard Code for Information Interchange) set.
Characters have integer values corresponding to location in the ASCII set.
You write the character constant by using single quotes (') surrounding the character.  

For example: 

1
'a' '$' 'Z';

We use the keyword int to create a character variable. 

For example: 

1
2
int chrA 'A';
int chrB '$';

Some characters called Special Characters can’t present directly inside the single quotes because they have a reserved meanings in MQL4 language.
Here we use something called Escape Sequence to present those special characters, And that by prefixing the character with the backslash character (\).

 For example: 

1
2
int chrA = ;        //slash character
int chrB '\n';        //new line

This is the list of Escape Sequence characters used in MQL4.

1
2
3
4
5
6
7
carriage_return                            r
new_line                                   n
horizontal_tab                             t
reverse_slash                              
single_quote                               
double_quote                               "
hexadecimal                                xhh

And here's the ASCII table:

4- String

The string data type is an array of characters enclosed in double quotes (").
The array of characters is an array that holds one character after another, starting at index 0. After the last character of data a NULL character is placed at the next array location. It does not matter if there are unused array locations after that.
A NULL character is a special character (represented by the ASCII code 0) used to mark the end of this type of strings.
See figure 2 for a simple representation of the string constant "hello" in the characters array.


Figure 2 - Characters array

MQL4 limits the size of the string variable to 255 characters and any string exceeds the 255 characters will generate the error: (too long string (255 characters maximum)).
You can use any of the special characters mentioned above in you string constant by prefixing it with the backslash (\).
We use the keyword string to create the string variable .

For example :

1
2
3
string str1 "Hello world! With  you coders guru";
string str2 "Copyright 2010 \"xpworx.com\".";    //Note the use of (") character
string str3 "1234567890";

5- Floating-point number (double)

Floating point number is the Real Number (a number that contains fractional part beside the integer part separated with (.) dot). Example: 3.0, -115.5, 15 and 0.0001.
The range is this data type is between 2.2e-308 to 1.8e308.
We use the keyword double to create the floating-point variables.

For example:

1
2
3
double dblNumber1 =  1000000000000000;
double dblNumber2 1/4;
double dblNumber3 5.74;

6- Color

Color data type is a special MQL4 data type that hold a color. We use colors for example for the indicators lines of histograms that appears on the MetaTrader chart and the user can change from the property tab of your indicator.
You can set the color variable constant with one of three methods:

1- By the color name:
For the well-known colors (called Web Colors Set) you can assign the name if the color to the color variable (see the list below of the Web Colors Set).

2- By Character representation:
In this method you use the keyword (C) followed by two signal quotations (').
Between these two quotations you set the value if the Red, Green and Blue (known as RGB value of the color). These values have to be between 0-255. You can write these values in decimal or hexadecimal format.

3- By the integer value:
Every color in the Web Colors Set has its integer representation that you can write in decimal or hexadecimal format. You can assign the integer value of the color to the color variable. The hexadecimal color format looks like this: 0xBBGGRR where BB us the Blue value, GG is the Green value and RR is the Red value.

For example:

1
2
3
4
5
6
7
8
//by Character representation
C'128,128,128'   //gray
C'0x00,0x00,0xFF'   //blue
//By the color name
0xFFFFFF   //white
16777215   //white
0x008000   //green
32768   //green

We use the keyword color to create the color varaibles.

For example:

1
2
3
color clr1 Red;
color clr2 C'128,128,128';
color clr3 32768;


7- Datetime

Datetime is a special MQL4 data type that holds date and time data. You set the datetime variables by using the keyword (D) followed by two signal quotations ('). Between these quotations you write a string consists of 6 parts for values of year, month, date, hour, minutes and seconds. Datetime constant starts from Jan 1, 1970 to Dec 31, 2037.

For example:

1
2
3
4
5
D'2004.01.01 00.00'   //New Year
D'1980.07.19 12:30.27'
D'19.07.1980 12:30.27'
D'19.07.1980 12'   //Equal to  D'1980.07.19 12:00:00'
D'01.01.2004'   //Equal to  D'01.01.2004 00:00:00

We use the keyword datetime to create the datetime varaibles.

For example:

1
2
datetime dtMyBirthDay D'1980.07.07';
datetime dt1 D'2010.10.09 00:23:00';

I hope you enjoyed this lesson.

See you
Coders’ Guru

Do You Like this Article?