In this article you can learn the essentials about MQL5 Constants and why they can be useful when building your own MT5 custom indicators and expert advisors.
What Are MQL5 Constants?
Constants play a crucial role in programming by representing fixed values that remain unchanged throughout the execution of a program.
The concept is very very similar to variables, but in the case of contacts, they store set values that can’t change.
In MQL5, constants provide stability and clarity, making your trading algorithms more readable and easier to maintain.
A MQL5 Constant is a named value that cannot be modified after its declaration. Unlike variables, which store data that can change during program execution, constants remain fixed. This ensures that specific values stay consistent and prevents accidental changes in critical parts of your code.
You can find some information about constants also on the official page.
Why Do We Need Constants?
Similar to variables, some programs don’t really need constants, but there are many benefit introducing constants in your code.
Constants are essential for:
- Code Clarity: By using meaningful names, you make your code more readable.
- Maintenance: Changing a value in one place updates all its references.
- Error Prevention: Fixed values reduce the risk of unintentional modifications.
Declaring Constants
Declaring a constant in MQL5 is similar to declaring a variable, with the addition of the const keyword.
Syntax:
|
1 2 3 |
const double PI = 3.14159; const int MAX_TRADES = 100; const string BROKER_NAME = "MyBroker"; |
In this example:
PIis a constant representing the mathematical value of pi.MAX_TRADESsets an upper limit for trades.BROKER_NAMEstores a fixed broker name.
Types of Constants in MQL5
Like variables, constants store values of a defined type.
MQL5 supports constants for all fundamental data types:
- Integer Constants:
const int,const long - Floating-Point Constants:
const double,const float - Boolean Constants:
const bool - Character Constants:
const char - String Constants:
const string - Date and Time Constants:
const datetime - Color Constants:
const color
Using Constants
Once declared, constants can be used just like variables, with the key difference that their values cannot be changed.
Example:
|
1 2 3 |
const double COMMISSION_RATE = 0.01; double commission = 1000 * COMMISSION_RATE; Print("Commission: ", commission); |
In this example, COMMISSION_RATE remains fixed at 1% and cannot be modified.
Attempting to Modify MQL5 Constants
MQL5 enforces constant behavior by preventing value changes after declaration.
Any attempt to modify a constant results in a compilation error.
Example:
|
1 2 |
const int MAX_ORDERS = 10; MAX_ORDERS = 20; // Compilation error |
This helps safeguard critical values and avoid logic errors.
Scope of Constants in MQL5
Constants follow the same scope rules as variables:
- Local Constants: Declared inside functions, accessible only there.
- Global Constants: Declared outside functions, accessible throughout the program.
Example:
|
1 2 3 4 5 6 7 |
const int GLOBAL_LIMIT = 50; // Global void CheckLimit() { const int LOCAL_LIMIT = 10; // Local Print("Local Limit: ", LOCAL_LIMIT); Print("Global Limit: ", GLOBAL_LIMIT); } |
Conclusion
MQL5 constants are vital for creating stable, clear, and maintainable code.
By using fixed values with meaningful names, you enhance readability, reduce errors, and simplify updates.
Mastering the use of constants ensures more robust and efficient trading algorithms.
For questions and support please Contact Us.