Summary
In MQL4, there are various data types that store different kinds of information. These include integer data types (such as char, int, long), logical data types (bool), string data types (string), floating-point numbers (float, double), color data (color), time and date data (datetime), and enumerations (enum). These data types determine how much memory is required for variables and what type of data can be processed, such as integers, strings, logical values, or colors.
Top 3 Key Takeaways:
- Integer and logical data types: MQL4 offers various integer data types like char, int, long, which can be chosen based on memory requirements and value range. bool stores simple logical values (true/false).
- Floating-point numbers and strings: Floating-point numbers are defined by float and double, with double being more precise. The string data type is used for storing sequences of characters.
- Color and time data types: The color data type allows representing colors using RGB values, integers, or predefined names. datetime stores time and date values, representing the number of seconds since 1970.
Table of contents
Click for open/close
Integer Data Types:
char
The char type requires 1 byte of memory (8 bits) and allows the representation of 2^8 = 256 values in binary form. The char type can contain both positive and negative values, with a range from -128 to 127.
short
The size of the short type is 2 bytes (16 bits), allowing it to represent 2^16 = 65,536 values. The short type also contains both positive and negative values, with a range between -32,768 and 32,767.
int
The size of the int type is 4 bytes (32 bits). The minimum value is -2,147,483,648, and the maximum value is 2,147,483,647.
long
The size of the long type is 8 bytes (64 bits). The minimum value is -9,223,372,036,854,775,808, and the maximum value is 9,223,372,036,854,775,807.
uchar
The uchar integer type occupies 1 byte of memory, like the char type. However, unlike char, uchar is only intended for positive values. The minimum value is zero, and the maximum value is 255. The "u" in uchar stands for unsigned.
ushort
The ushort integer type occupies 2 bytes of memory, like the short type. However, ushort is only intended for positive values. The minimum value is zero, and the maximum value is 65,535. The "u" in ushort stands for unsigned.
uint
The uint integer type occupies 4 bytes of memory, like the int type. However, uint is only intended for positive values. The minimum value is zero, and the maximum value is 4,294,967,295.
ulong
The ulong integer type occupies 8 bytes of memory, like the long type. However, ulong is only intended for positive values. The minimum value is zero, and the maximum value is 18,446,744,073,709,551,615.
Example:
MQL Code
char number1=-13;
short number2=-12457;
int number3=533;
long number4=-184541515;
uchar number5=28;
ushort number6=9355;
uint number7=99241;
ulong number8=9157848151;
Logical Data Type
bool
The bool data type is used to store logical values, which can either be "true" or "false". Alternatively, the numeric values 0 for "false" and 1 for "true" can also be used.
Example:
MQL Code
bool value1= true;
bool value2= false;
bool value3=0;
bool value4=1;
String Data Type
string
The string data type is used for storing text. A string is a sequence of characters in Unicode format and is enclosed in double quotation marks: "This is a string".
Example
MQL Code
string text1="This is a text string";
string text2="This is a text string with a newline at the end \n";
string text3="This is a multiline text string\n"
"This is line 2 of a multiline text string\n"
"This is line 3 of a multiline text string\n";
Floating-Point Data Types
float, double
MQL4 offers two types of floating-point numbers. The float data type uses 4 bytes, while the double data type uses 8 bytes. The double type is generally preferred due to its higher precision.
Example
MQL Code
double number1=3.14;
double number2=98.7654321;
double number3=-0.123456;
Color Data Type
color
The color data type is used for storing color information and occupies 4 bytes of memory. The first byte is ignored, and the remaining 3 bytes hold the RGB components.
Color values can be represented in three ways: RGB values, integers, or predefined color names.
RGB representation consists of three parts that specify the values of the red, green, and blue components. The variable begins with the letter C and is enclosed in single quotation marks. The numeric value for each color component ranges from 0 to 255.
The integer representation can be in hexadecimal or decimal form. A hexadecimal number is written as 0xBBGGRR, where RR represents the red color component, GG the green, and BB the blue. The decimal form is the equivalent numeric value of the hexadecimal representation.
Example
MQL Code
RGB Values
color color1=C'255,255,255'; // corresponds to the color White
color color2=C'0,0,0'; // corresponds to the color Black
Integer representation
color color3=0x0000FF; // corresponds to the color Blue
color color4=0x00FF00; // corresponds to the color Green
Date and Time Data Type
datetime
The datetime data type is used to store date and time. It stores the number of seconds elapsed since January 1, 1970, rather than the actual date. This type occupies 8 bytes of memory.
Date and time can be represented as a string consisting of 6 parts that display the numeric values of the year, month, day, hour, minute, and second. The representation is enclosed in single quotes and begins with the letter D. The values range from January 1, 1970, to December 31, 3000.
Example
MQL Code
datetime date=D'2018.12.31 23:59:59';
List Data Type
enum
The enum data type is used to define a set of related constants. It is often referred to as an enumeration data type.
Example
MQL Code
enum months
{
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};
With assigned values:
enum TradingStrategy
{
Strategy1=1,
Strategy2=2,
Strategy3=3,
Strategy4=4
};
Questions and Answers
The string data type is used in MQL4 to store strings, i.e., text. Texts are enclosed in double quotes.
The int data type can store both positive and negative integers, while uint can only store positive values, as it is an "unsigned" data type.
Colors can be represented in MQL4 using the color data type. They can be specified either by RGB values, integers (hexadecimal or decimal), or by predefined color names.