Summary

In MQL4, variables are fundamental building blocks that allow data to be stored, accessed, and modified within a program. There are different types of variables, such as int (integer), double (decimal), bool (true/false), and string (text). Each variable must be declared before it can be used. The scope of a variable determines whether it is accessible locally or globally. Proper use of variables improves the readability and efficiency of the code.

Key Takeaways

  1. Variables store and process data: They enable flexible use and manipulation of information in a program.
  2. Different data types: int, double, bool, and string are the primary variable types in MQL4.
  3. Important rules for variable names: Names should be descriptive and valid, without spaces or reserved keywords.
Text written with chalk on a blackboard Variables in MQL4

What are Variables?

A variable is a named storage space in the computer that contains specific information (data). This data can be changed throughout the program. You can think of a variable as a box that you give a name to, and in which you can place something, like a number or a text.

Example:

Imagine you want to store the number of winning trades. You could create a variable called WinningTrades and assign it the number of winning trades.

Why are Variables Needed?

Types of Variables in MQL4

In MQL4, there are different types (or data types) of variables. Here are the main ones:

int (Integer)

An int variable stores whole numbers, i.e., numbers without decimal points (e.g., 1, 10, -5). It is used for things that do not have fractions, like counts or indices.

MQL Code

int WinningTrades = 5; // A variable named WinningTrades that holds the value 5

double (Floating-point number)

A double variable stores decimal numbers, i.e., numbers with decimal points (e.g., 1.25, -0.75, 100.5). This is useful when working with prices, profits, or other values that need to be precise.

MQL Code

double currentPrice = 1.2501; // A variable named currentPrice with the value 1.2501

bool (Boolean/Logical Value)

A bool variable can only have one of two values: true or false. It is used to make decisions. For example, you could use a boolean variable to store whether a condition is met or not.

MQL Code

bool isTradeOpen = true; // A variable named isTradeOpen, which stores whether a trade is open (true = yes, a trade is open)

string (Character string/Text)

A string variable stores text. This can be anything: names, messages, or strings of characters that you want to display in your program.

MQL Code

string name = "MyTradingSystem"; // A variable named name that contains the text "MyTradingSystem"

How to Use Variables?

To use a variable, you need to declare it, which means you must tell the program what type of data the variable will store and give it a name.

Declaring a Variable:

MQL Code

int myNumber; // Declares a variable named myNumber that stores an integer

Assigning a Value to a Variable:

After declaring a variable, you can assign a value to it:

MQL Code

myNumber = 10; // Assigns the value 10 to the variable myNumber

You can declare and assign a value in one line:

MQL Code

int myNumber = 10; // Declares the variable and immediately assigns it the value 10

Using Variables:

Once a variable has a value, you can use that value in calculations or other parts of your program:

MQL Code

int numberA = 5; int numberB = 3; int result = numberA + numberB; // Adds numberA and numberB and stores the result in the variable result Print(result); // Prints the value of result (8) in the terminal

Variable Names

Valid Names:

MQL Code

int winningTrades; // Valid name double averageProfit; // Valid name

Invalid Names:

MQL Code

int 2trades; // Invalid, as it starts with a number double double; // Invalid, as "double" is a keyword

Variable Scope

The scope of a variable determines where in the program you can use it. There are two main types:

Example of a local variable:

MQL Code

void OnTick() { int tickCounter = 0; // tickCounter is only available within this function }

Example of a global variable:

MQL Code

int totalCounter = 0; // This variable is available throughout the entire program void OnTick() { totalCounter++; // The value of totalCounter can be changed here }

Important Tips for Working with Variables:

Frequently Asked Questions

What is a variable in MQL4?

A variable is a named storage space in the program that can hold data such as numbers or text. This data can be changed and used throughout the program.

What types of variables exist in MQL4?

The main types are int for integers, double for decimal numbers, bool for logical values (true/false), and string for text.

How do I declare a variable in MQL4?

A variable is declared by specifying the data type and the variable name, e.g., int myVariable;.

What is the difference between local and global variables?

Local variables are only visible within the function they are declared in, while global variables are accessible throughout the program.