When you work with a programming language, you normally have a defined set of data types you can use. This set can change from one language to another. Data types are assigned to variables, constants, and functions, and they are necessary because the computer must know how to handle information and what operations are allowed to be performed with that information.
In this guide, you will see an introduction of the data types available in MQL4.
One example to understand this concept is the following. Imagine a variable that has been declared as numeric — you cannot store alphabetical characters in it, you cannot perform text manipulations with it, but you will be able to perform mathematical operations with it. For example, increment its value. Conversely, if you have a variable of type
string, you won't be able to perform mathematical operations, but you will be able to manipulate the symbols stored in it.
To understand the concept of data types, it is useful to know the concept of variables in MQL4.
As already explained in other guides, when you declare a variable, a constant, or a function, you define what data type it is going to contain. In MQL4, you can find the following types of data:
In this guide, we will discuss the most common used data types.
MQL4 is a case-sensitive language, so in all the operations performed with variables and functions, you need to be careful with the case. For example,
a is different from
A, and a generic function
OpenTheTrade is different from
Integer is a numerical type, there are different types of integers to store numerical values of different length. They are:
The types have been designed to optimize memory allocation for variables. For example, if you want a variable with a value from 1 to 10 you can declare it as
char, which can store values from -128 to 127. If you need to store a value of 1,999, then you will need to declare a variable of type
short or longer. The most commonly used integer type is
int. It can usually cover all your requirements as it can store values from -2,147,483,648 to 2,147,483,647.
Types that are not starting with
u can store positive or negative values. Types with
u as the first letter (for example,
ushort) can only store positive values. We can see examples of declaration and use of some data types here:
int i = -123456789; // Declare and initialize variable of integer int type. uint k = 234567890; // Declare and initialize variable of integer uint type. char h = 100; // Declare and initialize variable of integer char type. short j = 1999; // Declare and initialize variable of integer short type.
Be aware that if you try to assign to a variable a value that is out of the range of its data type, the value will be truncated and will be different from what you expect. For example, see below:
Boolean is a logical type that can assume value of either true or false. This type is very useful to set flags, that is, when you want to check if a condition is verified before executing some operations.
For example, if you want to disable trading in an expert advisor in MQL4, you could define a variable called
Trading_Enabled of boolean type and set it to true or false. Then you could change the value of the variable and check it before placing orders.
To declare a variable of type boolean, you need to use the
Keep in mind that true and false are represented numerically as 1 and 0, respectively.
bool Trading_Enabled = true; // Declare and initialize variable of boolean type.
You will learn about the boolean type when studying the
String is a very important data type, which is widely used as well in MQL4 coding. String allows to store and manipulate any alphanumerical sequence of characters. It could be a generic text, a label, a description, a name, some numbers, special characters, and so forth. String manipulation can be very powerful — you can indeed find characters or words in a string, replace characters, transform to upper case or lower case, cut it, add to it, and many other operations.
There are so many functions to handle strings that we cannot even start to mention them in this guide. What we are interested to see here is how to declare them, which is done simply by using the
string keyword in front of the variable.
Remember that strings can contain numbers, however, since the data type is string, you cannot perform mathematical operations with them.
string s = "Hello World!"; // Declared a new variable s of type string and value "Hello World!"
Datetime is a type of data used to store a date and time as the number of seconds elapsed since January 01, 1970. This is a very useful type to work with dates and time as it easily allows performing comparison and sorting and it also optimizes the use of the memory.
To define a variable as Datetime, start the declaration with the
datetime keyword. To assign a value, you can use a string in the format
D'YYYY.MM.DD hh:mm:ss' or
D'DD.MM.YYYY hh:mm:ss' where
YYYY is the year,
MM — the month,
DD — the day,
hh — the hour,
mm — the minute,
ss — the second.
If any of the date element is missing, MetaTrader will use the current date. If any of the time element is missing MetaTrader, will put a 00.
datetime t1 = D'2016.08.01 14:10:00'; // t1 declared as datetime and assigned 2016.08.01 14:10:00. datetime t2 = D'2016.08.01 14'; // t2 declared as datetime and assigned 2016.08.01 14:00:00. datetime t3 = D'14:10:00'; // t3 declared as datetime and assigned [compilation date] 14:10:00. datetime t4 = D'10.09.2016 14:10:00'; // t4 declared as datetime and assigned 10.09.2016 14:10:00.
MetaTrader has several functions to work with datetime variables, for example to convert datetime to string and vice versa.
Building a program that will handle numbers used in trading requires a data type capable of managing floating-point numbers. Many indicators and exchange rates are usually expressed with floating-point numbers, so MetaTrader offers the following data types to deal with these data: float and double.
Float and double are both real number types. The difference between them is the bytes allocated in memory, 4 for float and 8 for double, resulting in the following minimal and maximum values:
To declare these types of data, use
double d = 10.0 / 3.0; // Declared d as double assigning the result of division 10 / 3. float f = 10.0 / 3.0; // Declared f as float assigning the result of 10 / 3.
As you can see in the example above, the numbers used in the division have
.0 appended to them. This is done to make the MetaEditor treat them as real numbers and not integers. Otherwise, an integer division would be used and both
f would be assigned the value of 3.
Also, if you compile the example above, you will notice a value truncation warning for the float variable. This happens because a float data type cannot store precisely the result of that division.
We will not introduce for now the other types because they are for more advanced and complex coding. It would be beneficial to start experimenting by using the above data types for the moment and getting familiar with them. You will use them frequently in your future programs.
If you want to get news of the most recent updates to our guides or anything else related to Forex trading, you can subscribe to our monthly newsletter.