MT5 Basics: Don't Fear the Code! 4 Essential MQL5 Data Types and Variable Settings

Many traders looking to transition from manual trading to automated algorithmic trading are often intimidated when they first see MetaTrader 5 (MT5) code. However, writing a trading program is just like learning a new language. As long as you master the basic "vocabulary classification" and "sentence structure," you can easily write your first Expert Advisor (EA).
In the world of MQL5, these fundamental vocabulary classifications are called "Data Types," and the sentence structures are "Variable Settings (Declarations)." This article will break down the 4 essential data types you must know, making code no longer a headache for you!
1. What are Variables and Data Types?
Imagine a variable as a "storage box" on your desk. You can put various data (such as prices, lots, or ticket numbers) inside it. To help the computer process it efficiently, we must label the box to specify what kind of data it can hold. This label is the "Data Type."
2. 4 Essential MQL5 Data Types
In MQL5 development, more than 80% of scenarios only require the following four basic types:
- 1. Integer Type (int): Used to store whole numbers without decimals. In trading, it is most commonly used to record "Order Ticket Numbers," "Position Counts," or "K-line Timeframes (e.g., 15 minutes)."
- 2. Double Type (double): Used to store numbers with decimals. This is crucial in financial trading because "Asset Prices (e.g., Gold XAUUSD at 2350.75)," "Indicator Values (e.g., RSI at 70.5)," and "Trade Lots (e.g., 0.01 lots)" all require it.
- 3. Boolean Type (bool): This is a very simple type that has only two values:
trueorfalse. It is commonly used for logical conditions, such as:IsNewBar = true;(Is it a new K-line?). - 4. String Type (string): Used to store pure text messages. For example, the asset name
"XAUUSD"or the notification text you want to display on the chart"Strategy Started". String values must be enclosed in double quotes.
3. How to Correctly Set (Declare) Variables
The syntax for setting variables in MQL5 is very strict, following the formula: Data_Type Variable_Name = Initial_Value;. Never forget to add a semicolon (;) at the end. Here are practical code examples:
int magicNumber = 123456; // Set a unique EA identification number
double lots = 0.1; // Set the trading lot size to 0.1
bool isTrading = true; // Set whether trading is currently allowed
string symbolName = "XAUUSD"; // Set the trading symbol to Gold
Through these settings, the computer prepares four storage boxes and safely stores the corresponding values inside, ready to be called by your trading logic later.
4. Conclusion
Once you understand int, double, bool, and string, along with variable declarations, you have taken your first major step in MQL5 algorithmic trading. Next, you can try importing technical indicator values into variables and begin your journey into automated trading!
References: MQL5 Reference - Data Types (https://www.mql5.com/en/docs/basis/types)
Frequently Asked Questions
- Can I name MQL5 variables anything I want?
- No. Variable names cannot start with numbers, cannot contain spaces, and cannot use MQL5 reserved words (such as int or double). Additionally, MQL5 is case-sensitive, so lots and Lots are treated as two entirely different variables.
- What happens if I store a price into an int data type?
- If you force a price with decimals (e.g., 2350.75) into an integer type (int), the digits after the decimal point (.75) will be completely dropped, leaving only 2350. This can cause severe calculation errors in trading logic, so prices must always use the double type.