$ £ ¥
¥ £ $

What Are Arrays in MQL4

Arrays are used extensively when working with charts and indicators, so even if you already know what they are, it is worth spending a few minutes reviewing them. Arrays are one of the basic components of MQL4 language, and it is necessary for you to understand what arrays are and how to use them in MQL4 to create your own programs.

To understand the concept of an array, you must already have a good grasp of variables and data types, operators, and you should also know how to create a demo script to test everything.

Arrays are specially arranged variables. Or more precisely, arrays are indexed sequences of data of the same type.

As mentioned above, arrays are sequences, and the data in the sequence must be of the same type, so, for example, a program can have:

  • Array of integer values - 1, 2, 3, 4, 5, 6, 7, 8, 9.
  • Array of strings - "a", "b", "c", "d", "e", "f", or "John", "Mark", "Matt", "Luke", and so on.

Since arrays are variables, they need to be declared. To declare an array, you use the same way as you do with any other variable, appending [N] after the variable name, where N is the size of the array, that is, how many elements it can hold. For example:

  • int i[5]; is an array that can contain up to 5 integer numbers.
  • string s[10]; is an array that can hold up to 10 strings.

In the definition of an array, you can read that it is an indexed sequence. This means that the elements are identifiable in the array using an index. This may be confusing at first, but you need to get used to it as the majority of programming languages use the same convention: arrays start to count their elements from 0. The first element of an array is located at index 0 (zero). As a consequence, in an array of size 10 (with ten elements), the last element will be located at the position 9 (nine).

MQL4 Array Example with 10 Elements

There are several ways to populate an array with values. The most popular two are manual and using loops (for or while for example). You can manually insert values into an array by writing the sequence of values between { } (curly brackets), separating each value with a , (comma) like in this example: int i[10] = {4, 6, 8, 10, 13, 17, 23, 42, 88, 64};. You can also insert a single value into a specific position by using an index and a value — like this: i[5] = 88; — it writes the value 88 into the array i at index 5 (remember that index 5 is actually the 6th position in the array).

When working with arrays, you will see that loops are one of your best friends. By combining the use of indexes and loops, you can manipulate arrays, scan for information, write values, make calculation, and so on. You can see a simple example here where the program scans an array of strings and prints its values.

//|                                                       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

string s[10] = {"Mark", "John", "Matt", "Daniel", "Luke", "Jack", "Vince", "Andrew", "Bill", "George"};

//| Script program start function                                    |
void OnStart()
   // A "for" loop is defined where variable i, which goes from 0 to 9, it prints the value of array s at index i.
   // i is incremented by 1 at each execution.
   for (int i = 0; i <= 9; i++)

The result in the Experts tab of the Terminal panel when executing this script is as follows:

Printing Text Array Using Simple MQL4 Script

This was just an introduction to arrays in MQL4, but it is necessary to progress in your learning path. If you don't have a clear understanding of the concept of arrays, you will hardly be able to produce programs and perform operations, so get to practice and master the arrays!

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.