MQL Data Types
MQL4 supports seven data types within the program. Each type is associated with different trading tasks that
programmers need to perform. The goal of this article is to provide a brief reference for when to use each data type.
The double data type is probably the most common type found in MQL programs. This is because it is the type
responsible for calculating floating point numbers. Say for example, that an expert advisor needs to determine when
to adjust a trailing stop. The expert advisor looks at the current price and subtracts it from the current stop loss to
maintain the appropriate distance (1.3230-1.3209=0.0021). The distance requires a decimal point. When the expert
advisor saves the distance to memory, it needs to save the information after the decimal point. That forces the
programmer to choose a variable of type double.
Integers, or int, is the simpler version of double. Double values require a decimal place to hold the number's value
accurately. An integer, or whole number, does not have a decimal place. Integers are appropriately used when the
MQL programmer knows for a certain fact that the number will never contain a decimal. An example would be if you
wanted to implement a max trades feature. If the number of open trades in the account exceeds a certain number,
then prevent trades. We know in advance that there is no such thing at 4.76 trades being open. There can only be 4
trades open or 5 trades open. This clearly indicates the need to use an integer.
Datetime values are just what they sound like. They represent both the date and time. More specifically, a datetime
variable represents the number of seconds that have elapsed since January 1, 1970. This is where it gets a little
tricky. The number of seconds that have elapsed is actually an integer. Datetimes store integer values but then
associate them with a date and time.
A value of 0 would indicate that the time is 00:00 on January 1, 1970. A value of 60 stands for one minute later at
00:01 1/1/1970, and so on. One benefit of knowing that the datetime type stores information as integers is that you
can easily determine the amount of time that happens between an event. If the event starts at 15:35 and ends at
18:12, you can simply subtract 18:12 - 15:35 and wind up with the number of seconds between those values. That
information can then be used to determine the number minutes/hours/days between the two events.
The color data type, not surprisingly, holds color information such as black, yellow, red and so on. Much like datetime
types, color also uses integers to store the information. The difference, though, is that extracting the color information
from the integer is not at all obvious. Increasing a color type from 32768 by one will not necessarily make it more or
less green. Colors use the integer information to retrieve the red, green and blue components of the color in
hexadecimal format. Explaining hexadecimals is well beyond the scope of this article. It's unlikely to come up in your
MQL programming. I've been doing this for over five years and only came across one project that required
manipulating a color in way more complicated than alternating between two set colors.
A string is anything that resembles a word or sentence. It always uses quotes to contain the information. My favorite
use of strings is to gather information to display on the chart or in a log file whenever I need to debug an expert
Char is the final data type. It's so closely related to a string that I wasn't even aware this type existed until I looked up
information for this article. If we study the word "trade", then we will find that it is composed of the five characters t, r,
a, d and e.
A final note on data types. There are two ways that types are held in memory. An extern variable is one that shows
up in the inputs screen whenever an expert advisor or indicator loads. Static variables are the opposite. They remain
within the expert advisor programing and are never visible outside of it.
Shaun Overton is the owner of OneStepRemoved.com, a company that specializes in programming expert advisors
and strategies on behalf of traders. Sample strategies are found in the free expert advisor section. The MQL4, MQL5
and NinjaTrader source code is available directly from the site.