Module 2: Understanding Variables and Data Types

Introduction to Variables

Now that you've written your first MQL4 script, it's time to dive into variables and data types. Variables are essential building blocks in any programming language. They allow you to store and manipulate data, making your scripts more dynamic and flexible.

What is a Variable?

A variable is a named storage location in your computer's memory that can hold a value. This value can change (vary) as your program runs. Think of variables as containers that hold data, which you can use and modify throughout your script.

Types of Variables

In MQL4, variables must be declared with a specific data type. Here are some common data types you'll use:

  1. int (integar) : Used to store integer (whole) numbers.

    • Example: int count = 10;

    • Explanation: int is for numbers without decimals.

  2. double: Used to store (decimal) numbers.

    • Example: double price = 1.2345;

    • Explanation: double is for numbers with decimals.

  3. string: Used to store sequences of characters (text).

    • Example: string message = "Hello, World!";

  4. bool: Used to store Boolean values (true or false).

    • Example: bool isActive = true;

    • Explanation: bool can only be true or false, and in numerical terms, true is 1 and false is 0.

Understanding Market Terms: Ask and Bid

  • Bid: The bid price is the price at which you can sell a currency pair.

  • Ask: The ask price is the price at which you can buy a currency pair.

These terms are fundamental for trading and will be used in your scripts to execute trades at the correct prices.

Working with Date and Time

MQL4 provides functions to work with date and time, which are crucial for time-based trading strategies:

  1. TimeCurrent(): Returns the current time in the server time zone.

    • Example: datetime currentTime = TimeCurrent();

  2. TimeLocal(): Returns the local time on the computer where MT4 is running.

    • Example: datetime localTime = TimeLocal();

  3. Hour(): Returns the hour of the given time.

    • Example: int currentHour = Hour(currentTime());

Using Color Variables

MQL4 allows you to use color variables for visual elements in your charts and indicators.

  • color: A data type to store color values.

    • Example: color lineColor = clrRed;

    • Explanation: clrRed is a predefined constant for the color red.

Digits and _Symbol

  • Digits: Returns the number of decimal places for the current symbol.

    • Example: int digits = Digits;

  • _Symbol: Stores the current symbol.

    • Example: string symbol = _Symbol;

Declaring and Using Variables

Let's see how to declare and use variables in MQL4:

// Declare an integer variable

int count = 10;

// Declare a double variable

double price = 1.2345;

// Declare a string variable

string message = "Hello, World!";

// Declare a Boolean variable

bool isActive = true;

// Declare a color variable

color lineColor = clrRed;

// Show alerts for the variables

Alert("Count: ", count);

Alert("Price: ", price);

Alert("Message: ", message);

Alert("Is Active: ", isActive);

Alert("Line Color: ", lineColor);