Any program operates with data. Data can be of different types depending on their purposes. For example, integer data are used to access to array components. Price data belong to those of double precision with floating point. This is related to the fact that no special data type is foreseen for price data in MQL 4.
Data of different types are processed with different rates. Integer data are processed at the fastest. To process the double precision data, a special co-processor is used. However, because of complicity of internal presentation of data with floating point, they are processed slower than the integer ones. String data are processed at the longest because of dynamic computer memory allocation/reallocation.
The main data types are:
- Integer (int)
- Boolean (bool)
- Literals (char)
- Strings (string)
- Floating-point number (double)
- Color (color)
- Date and time (datetime)
The color and datetime types make sense only to facilitate visualization and enter those parameters that had been set from expert advisor property tab or custom indicator "Inputs" tab. The data of color and datetime types are represented as integer values. int and double are called arithmetic (numeric) types.
Only implicit type casting is used in MQL 4 expressions. The type priority grows at casting:
int (bool,color,datetime); double; string;
Before operations (except for the assignment ones) are performed, the data have been conversed into the maximum priority type. Before assignment operations are performed, the data have been cast into the target type.
int i = 1 / 2; // no types casting, the result is 0 int i = 1 / 2.0; // the expression is cast to the double type, then is to the target type of int, the result is 0 double d = 1.0 / 2.0; // no types casting, the result is 0.5 double d = 1 / 2.0; // the expression is cast to the double type that is the same as the target type, the result is 0.5 double d = 1 / 2; // the expression of the int type is cast to the target type of double, the result is 0.0 string s = 1.0 / 8; // the expression is cast to the double type, then is to the target type of string, the result is "0.12500000" (the string containing 10 characters) string s = NULL; // the constant of type int is cast to the target type of string, the result is "0" (the string containing 1 character) string s = "Ticket #"+12345; // the expression is cast to the string type that is the same as the target type, the result is "Ticket #12345"
Type casting is applied to not only constants, but also variables of corresponding types.
Decimals: numbers from 0 to 9; zero must not be the first number.
12, 111, -956 1007
Hexadecimals: numbers from 0 to 9, letters from a to f or A to F to represent the values 10 to 15; they start with 0x or 0X.
0x0A, 0x12, 0X12, 0x2f, 0xA3, 0Xa3, 0X7C7
Its internal representation is a long 4-byte integer number. Integer constants can assume values from -2147483648 to 2147483647. If the constant exceeds this range, the result is undefined.
Any single character enclosed in single quotes or a hexadecimal ASCII-code of a character looking like ‘\x10’ is a character constant of integer type. Some characters like a single quote (‘), double quote (") a question mark (?), a reverse slash (\), and control characters can be represented as a combination of characters starting with a reverse slash (\) according to the table below:
line feed NL (LF) \n horizontal tab HT \t carriage return CR \r reverse slash \ \\ single quote ' \' double quote " \" hexadecimal ASCII-code hh \xhh
If a character different from those listed above follows the reverse slash, the result will not be defined:
int a = 'A'; int b = '$'; int c = '©'; // code 0xA9 int d = '\xAE'; // symbol code ®
Its internal representation is a long 4-byte integer number. Literal constants can assume values from 0 to 255. If the constant exceeds this given range, the result is undefined.
Boolean constants may have the value of true or false, numeric representation of them is 1 or 0, respectively. True or TRUE, False or FALSE can be used, as well.
bool a = true; bool b = false; bool c = 1;
Its internal representation is a long 4-byte integer number. Boolean constants can assume the values of 0 or 1.
Floating-point constants consist of an integer part, a point (.), and a fractional part. The integer and the fractional parts represent sequences of decimal numbers.
double a = 12.111; double b = -956.1007; double c = 0.0001; double d = 16;
Its internal representation is a double-precision number of 8 bytes. Floating-point constants can assume values from -1.7 * e-308 to 1.7 * e308. If a constant exceeds this range, the result is undefined.
String constant is a sequence of ASCII-code characters enclosed in double quotes: "Character constant".
A string constant is an array of characters enclosed in quotes. It is of the string type. If there is a need to insert a double quote (") into the string, a reverse slash (\) must be put before it. Any special character constants can be inserted into the string if they have a reverse slash (\) before them. The length of a string constant lies between 0 and 255 characters. If the string constant is longer, the superfluous characters on the right will be rejected, and compiler will alert correspondingly.
"This is a character string" "Copyright symbol \t\xA9" "this line contains a line feed symbol \n" "C:\\Program Files\\MetaTrader 4" "A" "1234567890" "0" "$"
Its internal representation is a structure of 8 bytes. The first element of the structure is a long integer that contains the size of the buffer distributed for the line. The second element of the structure is 32-order address of the buffer that contains the line.
Color constants can be represented in three ways: literally, by integers, or by name (for named Web colors only).
Literal representation consists of three parts representing numerical rate values of three main color components: red, green, blue. The constant starts with C and is enclosed in single quotes. Numerical rate values of a color component lie in the range from 0 to 255.
Integer-valued representation is written in a form of hexadecimal or a decimal number. A hexadecimal number looks like 0x00BBGGRR where RR is the rate of the red color component, GG – of the green one, and BB – of the blue one. Decimal constants are not directly reflected in RGB. They represent a decimal value of the hexadecimal integer representation.
Specific colors reflect the so-called Web colors set.
// 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
Its internal representation is a long integer number of 4 bytes. The first byte will not be considered. The other three bytes contain RGB components.
Datetime constants can be represented as a literal line consisting of 6 parts for values of year, month, date, hours, minutes, and seconds. The constant is enclosed in single quotes and starts with D. Either date (year, month, date) or time (hours, minutes, seconds), or both can be skipped. Datetime constant can vary from Jan 1, 1970 to Dec 31, 2037.
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' D'12:30:27' //equal to D'[compilation date] 12:30:27' D'' //equal to D'[compilation date] 00:00:00'
Its internal representation is a long integer number of 4 bytes. The value represents the amount of seconds elapse from 00:00 Jan 1, 1970http://www.myeatrade.com/417/