Lesson 3 - MQL4 Data types

What’s the Data type mean?

 

Any programming language has a set of 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 will name this data as “Integer” data type.

 

Variables?

Variables are the names that refer to sections of memory into which data can be stored.

To help you think of this as a picture, imagine that memory is a series of different size boxes. The box size is memory storage area required in bytes.

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 is not going to 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? That is the Data type for.

 

For example if we said this line of code to the computer:

 

int  MyVaraible=0;

 

That’s mean we are asking the computer to set a block of 4 bytes length to our 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:

 

1-    Integer

An integer, is a number that can start with a + or a - sign 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:

 

12, 3, 2134, 0, -230

0x0A, 0x12, 0X12, 0x2f, 0xA3, 0Xa3, 0X7C7

 

We use the keyword int to create an integer variable.

 

For example:

 

int intInteger = 0;

int intAnotherIntger = -100;

int intHexIntger=0x12;

 

 

Decimal and Hexadecimal:

 

Decimal notation is the 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-    Boolean

 

Boolean variable is a data type which can hold only two values, true and false (or their numeric representation, 0 and 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:

 

bool I = true;

bool bFlag = 1;

bool bBool=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:

 

'a' , '$' , 'Z'

 

 

We use the keyword int to create a character variable.

 

For example:

 

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:

 

int chrA = '\\';      //slash character

int chrB = '\n';      //new line

 

 

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

 

carriage return                          \r

new line                                    \n

horizontal tab                           \t

reverse slash                             \\

single quote                               \'

double quote                              \"

hexadecimal ASCII-code          \xhh

4-    String

The string data type is an array of characters enclosed in double quote (").

The array of characters is an array which holds one character after another, starting at index 0. After the last character of data, a NULL character is placed in 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 string.

See figure 1 for a simple representation of the string constant “hello” in the characters array.

Figure 1 – Characters array

MQL4 limits the size of the string variable to 255 characters and any character above 255 characters will generate this error: (too long string (255 characters maximum)).

You can use any special character -mentioned above- in your string constant by prefixing it with the backslash (\).

We use the keyword string to create a string variable.

For example:

string str1 = "Hello world1, with you coders guru”;

string str2 = "Copyright © 2005, \"Forex-tsd forum\".";  //Notice the use of (") character.

string str3 = "1234567890";

 

5-    Floating-point number (double)

Floating point number is the Real Number (that is, a number that can contain a fractional part beside the integer part separated with (.) dot).Ex: 3.0,-115.5, 15 and 0.0001.

And its range value is between 2.2e-308 to 1.8e308.

We use the keyword double to create a floating-point variable.

For example:

double dblNumber1 = 1000000000000000;

double dblNumber3 = 1/4;

double dblNumber3 = 5.75;

 

 

 

6-    Color

Color data type is a special MQL4 data type, which holds a color appears on the MetaTrader chart when you create your own Expert Advisor or Custom Indictor and the user can change it from the property tab of your Expert Advisor or Custom Indictor.

You can set the Color variable constant in three ways:

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

2- By Character representation (MQL4 named it this name): In this method you use the keyword (C) followed by two signal quotations ('). Between the two signal quotations you set the value of the red, green and blue (know as RGB value of the color). These values have to be between: 0 to 255. And 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 value which you can write it in decimal or hexadecimal format. And you can assign the Integer value of the color to the color variable. The hexadecimal color format looks like this: 0xBBGGRR where BB is the blue value, GG is green value and RR is the red value.

For example:

// symbol constants

C'128,128,128'    // gray

C'0x00,0x00,0xFF' // blue

// named color

Red

Yellow

Black

// integer-valued representation

0xFFFFFF          // white

16777215          // white

0x008000          // green

32768             // green

 

We use the keyword color to create a color variable.

 

For example:

 

color clr1= Red;

color clr1= C'128,128,128' ;

color clr1=32768;

 

 

Web Colors Set

Black

DarkGreen

DarkSlateGray

Olive

Green

Teal

Navy

Purple

Maroon

Indigo

MidnightBlue

DarkBlue

DarkOliveGreen

SaddleBrown

ForestGreen

OliveDrab

SeaGreen

DarkGoldenrod

DarkSlateBlue

Sienna

MediumBlue

Brown

DarkTurquoise

DimGray

LightSeaGreen

DarkViolet

FireBrick

MediumVioletRed

MediumSeaGreen

Chocolate

Crimson

SteelBlue

Goldenrod

MediumSpringGreen

LawnGreen

CadetBlue

DarkOrchid

YellowGreen

LimeGreen

OrangeRed

DarkOrange

Orange

Gold

Yellow

Chartreuse

Lime

SpringGreen

Aqua

DeepSkyBlue

Blue

Magenta

Red

Gray

SlateGray

Peru

BlueViolet

LightSlateGray

DeepPink

MediumTurquoise

DodgerBlue

Turquoise

RoyalBlue

SlateBlue

DarkKhaki

IndianRed

MediumOrchid

GreenYellow

MediumAquamarine

DarkSeaGreen

Tomato

RosyBrown

Orchid

MediumPurple

PaleVioletRed

Coral

CornflowerBlue

DarkGray

SandyBrown

MediumSlateBlue

Tan

DarkSalmon

BurlyWood

HotPink

Salmon

Violet

LightCoral

SkyBlue

LightSalmon

Plum

Khaki

LightGreen

Aquamarine

Silver

LightSkyBlue

LightSteelBlue

LightBlue

PaleGreen

Thistle

PowderBlue

PaleGoldenrod

PaleTurquoise

LightGrey

Wheat

NavajoWhite

Moccasin

LightPink

Gainsboro

PeachPuff

Pink

Bisque

LightGoldenRod

BlanchedAlmond

LemonChiffon

Beige

AntiqueWhite

PapayaWhip

Cornsilk

LightYellow

LightCyan

Linen

Lavender

MistyRose

OldLace

WhiteSmoke

Seashell

Ivory

Honeydew

AliceBlue

LavenderBlush

MintCream

Snow

White

 

 

 

 

 

7-    Datetime

Datetime data type is a special MQL4 data type, which holds a date and time data. You set the Datetime variable by using the keyword (D) followed by two signal quotations ('). Between the two signal quotations you write a character line consisting of 6 parts for value of year, month, date, hour, minutes, and seconds. Datetime constant can vary from Jan 1, 1970 to Dec 31, 2037.

For example:

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 a datetime variable.

 

For example:

datetime dtMyBirthDay= D'1972.10.19 12:00:00';

datetime dt1= D'2005.10.22 04:30:00';