How to Use Constants in MQL4

In some programs, you may want to use variables with values that cannot be changed while the program is running. It is in this case that constants come in. Here we are going to see how to use constants instead of variables in MQL4.

A constant is a variable with a value that cannot be changed while the program is running.

There are a few reasons that could drive you to use constants in your programs. It really depends on the program and the task performed. As a generic rule, if you are sure that a variable's value shouldn't change during the program's runtime, declare it as a constant.

A constant is basically a variable, so what you have learned about declaration and initialization of one is still valid. However, once a value has been assigned to a constant, it cannot be changed.

For a variable to be declared as constant, you need to add const in front of the declaration. We can see an example of code below:

//+------------------------------------------------------------------+
//|                                                       Demo-1.mq4 |
//|                                                    EarnForex.com |
//|                                       https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---

   const int i = 10; // i is declared as an integer constant and initialized with 10.

   Print(i); // The value of i will be printed as output in the Terminal - Experts tab.
   
  }
//+------------------------------------------------------------------+

The code above is perfectly valid, but if we try to change the value of a constant in the code and then we try to compile... BANG! An error!

//+------------------------------------------------------------------+
//|                                                       Demo-1.mq4 |
//|                                                    EarnForex.com |
//|                                       https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---

   const int i = 10; // i is declared as an integer constant and initialized with 10.

   i = 20; // We are trying to change the value of i to 20, this will cause an error.
      
   Print(i); // The value of i will be printed as output in the Terminal - Experts tab.
   
  }
//+------------------------------------------------------------------+

Compilation Error in MetaEditor when Trying to Modify an MQL4 Constant

Remember that you can use constants in your MQL4 code, and in some cases it is highly recommended. It all depends on the purpose of your code.


If you want to get news of the most recent updates to our guides or anything else related to Forex trading, you can subscribe to our monthly newsletter.

© 2005–2021

EarnForex.com

Design — Mart Studio

Forex trading bears intrinsic risks of loss. You must understand that Forex trading, while potentially profitable, can make you lose your money. Never trade with the money that you cannot afford to lose! Trading with leverage can wipe your account even faster.

CFDs are leveraged products and as such loses may be more than the initial invested capital. Trading in CFDs carry a high level of risk thus may not be appropriate for all investors.