Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 2

Declaring and Initializing Variables

Recommended resources

Welcome to your second lesson! In our first session, we mapped out the MQL5 landscape, distinguishing between Scripts, Indicators, and the Expert Advisors (EAs) that are central to your goal of building trading bots. Now, we're ready to get our hands dirty and write some code.

To build any set of instructions, your program needs a way to remember things—like the current price, the number of trades you have open, or whether it's time to buy. In this lesson, we'll cover the most fundamental building block for this: variables. You will learn how to declare and initialize variables using the four essential data types that form the basis of almost everything you'll do in MQL5. This is the first step in giving your future bot a memory.

What is a Variable?

Think of a variable as a labeled storage box. You can put information inside it, and the label tells you what's inside so you can find it later. In programming, these "boxes" are stored in your computer's memory.

Every variable has three key parts:

  • Type: What kind of information the box can hold (e.g., numbers, text).
  • Name: The label you give the box to identify it.
  • Value: The actual information stored inside the box.

This short video explains this concept perfectly, comparing variables to storage containers and introducing the main types we'll be using.

MQL5 Programming Tutorial 1.04 - Core Building Blocks Part 1 - Variables and Comments

This clip provides an excellent analogy for understanding what variables are and why they are fundamental.

Watch the segment from storage containers. Pay attention to the four main data types mentioned: double, int, string, and bool.


Declaring and Initializing Variables

Creating a variable in MQL5 involves two steps that are often done together: declaration (creating the box and giving it a type and a name) and initialization (putting an initial value inside it).

The syntax looks like this:

type name = value;

Let's break that down:

  • type: A keyword telling the computer what kind of data will be stored (e.g., int).
  • name: The unique name you choose for the variable. A good name is descriptive, like accountBalance instead of x.
  • = : The assignment operator. It tells the computer to put the value on the right into the variable on the left.
  • value: The data you are storing. The format of the value must match the variable's type.
  • ;: The semicolon. This is crucial—it marks the end of a statement in MQL5, like a period at the end of a sentence.

Here's an image showing several variables being declared and initialized. Notice how each line follows the type name = value; pattern.

This image shows the syntax for declaring and initializing variables for different data types, which we will now cover in detail.

Before we dive into the types, it's important to know the rules for naming variables. This article section provides a clear list.

Master MQL5 from beginner to pro (Part II): Basic data types and use ...

This resource details the rules for creating valid variable names, known as identifiers.

Read the short section titled Identifier conventions. The key rules are that names must start with a letter or underscore and cannot contain spaces or special characters.


The Four Fundamental Data Types

While MQL5 has many data types, you can build a huge amount of logic with just these four. Let's explore them with practical, trading-related examples.

For a quick and simple explanation of each type, this article is very helpful.

A Beginner's Guide into Algorithmic Trading - MQL5 Articles

This guide is written for absolute beginners and provides clear, concise definitions for the basic data types.

Read the section Basic Data Types. This will give you a brief text-based overview of each type before we see them in action.

Now, let's see how to actually use them. The following video demonstrates how to declare and use each of these types within a basic Expert Advisor.

LEARN MQL5 TUTORIAL BASICS - 4 WHAT ARE DATA TYPES

This video walks you through creating a simple MQL5 file and demonstrates the declaration of each fundamental data type.

As we discuss each data type below, watch the corresponding segment from the video to see it in a real code editor.

1. int - For Whole Numbers

The int (short for integer) type is used to store whole numbers without any decimal places.

  • When to use it: For counting things, like the number of open trades, the number of bars on a chart, or a trade's ticket number.
  • Example Code: int tradeCount = 10;

To see this in action, watch the part of the video that demonstrates an integer.

  • Watch from this point.

2. double - For Numbers with Decimals

The double type is used for numbers that have a fractional part. This is probably the most common type you'll use in trading.

  • When to use it: For storing prices (e.g., EUR/USD at 1.0750), account balance, indicator values, or Stop Loss levels.
  • Example Code: double accountBalance = 15432.78;

Now, see the demonstration for double. Notice how it's used for the account balance, which includes cents.

  • Watch from this segment.

3. string - For Text

The string type holds a sequence of characters, or text. The text value must be enclosed in double quotes (").

  • When to use it: To store the name of a currency symbol ("EURUSD"), an error message, or the name of your trading strategy.
  • Example Code: string traderName = "AlgoCraft";

Watch the video segment that demonstrates a string.

  • Watch from this section.

4. bool - For True or False

The bool (short for boolean) is the simplest type. It can only hold one of two values: true or false.

  • When to use it: To represent a state or a decision. For example: Is it okay to trade? (isTradingAllowed = true;) Is a moving average crossover detected? (crossoverDetected = false;)
  • Example Code: bool isBotRunning = true;

Finally, see the bool type demonstrated.

  • Watch from this clip.

Conclusion

You've just learned the absolute foundation of programming in MQL5. Without variables, a program has no memory and cannot perform any meaningful tasks. Mastering these four data types is the key to unlocking the power of algorithmic trading.

Key Takeaways:

  • Variables are named containers used to store data in your program.
  • The basic syntax for creating a variable is type name = value;.
  • The four fundamental data types are:
    • int: For whole numbers (e.g., 5, -100).
    • double: For numbers with decimal points (e.g., 1.2345, 500.50).
    • string: For text, enclosed in quotes (e.g., "EURUSD").
    • bool: For logical states (true or false).

In our next lesson, we will build directly on this knowledge. Now that you know how to store numbers in variables, you'll learn how to perform calculations with them using arithmetic and assignment operators. This will allow you to start manipulating data, which is the first step toward creating trading rules.