$ £ ¥
¥ £ $

How to Use Variables in MQL4

Variables are one of the most important foundation of programming; without them, it is nearly impossible to build a useful program! Make sure you understand what variables are and how to use them in MQL4 because you will work with them in all your programs. From this guide you can learn about the variables in MQL4 with detailed examples.

Variables are containers of information. You can give them a value, change it, combine it, and re-use it.

In MQL4, variables must be declared with some data type. A declared variable can only contain values allowed by its data type. For example, if a variable is of an integer type (numerical), it cannot contain alphabetical characters.

Variables have a life cycle — they are created, assigned a value, changed the value (or not), depending on their purpose, and then they are destroyed.

If you haven't created your first demo script in MQL4, it is better to do so before proceeding to the code examples below.

MQL4 is a case-sensitive language, so in all the operations performed with variables and functions, you need to be careful with the case. For example, a is different from A, and a generic function OpenTheTrade is different from openthetrade.


Declaration

To use a variable you must declare it first. This action is to tell the program to create the variable assigning a data type to it. To declare a variable, write the data type followed by a space and the name of the variable; close the line with a semicolon (;). Here are some examples of variable declaration:

int i; // i is a variable of type integer.
string s; // s is a variable of type string.
bool enabled; // enabled is a variable of type boolean.

Initialization

Once the variable is declared, you can assign a value to it. Initialization is the first assignment of a value to a variable.

If you try to assign a value to a variable that hasn't been yet declared, your code will throw an error on compilation.

To initialize a variable, type the name of a variable followed by an equal sign (=) and the value you want to assign. Close the line with a semicolon (;). Here are some examples:

i = 1; // i is assigned a value of 1.
s = "Hello"; // s is assigned a the string "Hello".
enabled = true; // enabled is assigned true.

Declaration with Initialization

Depending on the program and your style of coding, you can choose to initialize a variable when you declare it. It is totally acceptable in MQL4, so feel free to use this method. To declare and initialize a variable at the same time, just write the data type followed by a space, the variable name, an equal sign (=), the value, and a semicolon (;).

int i = 1; // i is declared and assigned a value of 1.
string s = "Hello"; // s is declared and assigned a the string "Hello".
bool enabled = true; // enabled is declared and assigned true.

Use

To use a variable, you just have to call the variable name in a function or any other operation. We can see it in an example below:

int i = 1; // i is declared and assigned a value of 1.
string s = "Hello"; // s is declared and assigned a the string "Hello".
bool enabled = true; // enabled is declared and assigned true.
Declaring and Printing a String Variable in MQL4

Update the Value of a Variable

The value of a variable will likely change during the execution of a program. It is called variable indeed because its value can vary (change). If we wanted a variable with a fixed value we would have created a constant instead. To update the value of a variable, the code is the same as the initialization: use the name of the variable, an equal sign (=), and the new value, like in the following example:

int i = 1; // i is declared as integer and initialized with 1.
i = 2; // The value of i is changed to 2.
i = 10; // The value of i is change to 10.

You can also use variables to assign values to other variables. We can see some examples 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()
  {
//---

   int i = 10; // i is declared as integer and initialized with 10.
   int j = 20; // j is declared as integer and initialized with 20.
   int k = i + j; // k is declared as integer and assigned a value of i + j.

   string greeting = "Hello"; // greeting is declared as string and assigned a value "Hello".
   string name = "John"; // name is declared as string and assigned a value "John".
   string welcome = greeting + " " + name; // welcome is declared as string greeting + name .
   
   Print("k = ", k); // The value of k will be printed as output in the Terminal - Experts tab.
   Print(welcome); // The value of welcome will be printed as output in the Terminal - Experts tab.
   
  }
//+------------------------------------------------------------------+
Printing Out Updated Variables in MQL4

Deinitialization and Destruction

In MQL4, you don't really need to deinitialize and destroy variables. This would be the process to unload the variables and their values from the computer's memory, but MetaTrader does it automatically during unloading and exiting the program.

Well done! You should now have a better understanding of what are the variables in MQL4. This will allow you to proceed in your learning path.

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.