⚡ Stock Alert : Intel stocks are down 14.07% in the last 24 hours – Buy Stock Here

What are Arrays in MQL4

What are Arrays in MQL4

Highlights:

  • Arrays in MQL4 are fundamental data structures used for efficiently storing and managing collections of values.
  • They play a crucial role in trading algorithms by handling data series like price history and indicators, enabling complex and powerful trading strategies.

Arrays in MQL4 are essential data structures that allow you to store and manage collections of values efficiently. They are particularly useful in trading algorithms for handling series of data, such as price history, indicators, and custom variables. By using arrays, one can perform operations on multiple data points simultaneously. Thus, they allow more complex and powerful trading strategies. It is essential to understand how to declare, initialise, and manipulate arrays for developing effective MQL4 programs.

Important Concepts Linked To Arrays

An array is a collection of values of the same type that share a common name. They can be one-dimensional or multidimensional, with a maximum of four dimensions. They can contain any data type.

Array Element

An array element is a part of an array. It is an indexed variable that has the same name as the array but holds a specific value.

Array Index

The array element index consists of one or more integer values placed in square brackets, separated by commas. This index identifies the position of the element within the array. The index follows the array name and is crucial for locating each element. In MQL4, indexing starts at zero.

Declaring Arrays

To declare an array in MQL4, one needs to specify the type of values it will hold, followed by the array name and the size of the array in square brackets. Here are the basic steps:

  • Type Specification: Determine the data type of the array elements, such as int, double, string, etc.
  • Array Name: Choose a name for the array.
  • Size Declaration: Indicate the number of elements the array will hold by placing an integer inside square brackets.

Example of One-Dimensional Array

Code: int myArray[10];

This example creates an array named myArray that can hold 10 integers.

Example of Multi-Dimensional Array

Code: double prices[5][4];

This example creates a two-dimensional array named prices with 5 rows and 4 columns, capable of storing double values.

Array Initialisation

Initialising an array in MQL4 involves assigning values to its elements at the time of declaration or after. Here are some ways to initialise arrays:

Initialising at Declaration

Users can initialise an array with specific values when they declare it.

One-Dimensional Array:

Code: int numbers[5] = {1, 2, 3, 4, 5};

This creates an array named `numbers` with 5 elements, each initialised with a specific value.

Multi-Dimensional Array:

Code: double matrix[2][3] = {

    {1.1, 2.2, 3.3},

    {4.4, 5.5, 6.6}

};

This creates a two-dimensional array named `matrix` with 2 rows and 3 columns, each element initialised with a specific value.

Initialising After Declaration

Users can also initialise or modify an array’s elements after its declaration.

One-Dimensional Array:

Code: int numbers[5];

numbers[0] = 1;

numbers[1] = 2;

numbers[2] = 3;

numbers[3] = 4;

numbers[4] = 5;

This first declares an array named `numbers` with 5 elements, then initializes each element individually.

Multi-Dimensional Array:

Code: double matrix[2][3];

matrix[0][0] = 1.1;

matrix[0][1] = 2.2;

matrix[0][2] = 3.3;

matrix[1][0] = 4.4;

matrix[1][1] = 5.5;

matrix[1][2] = 6.6;

This first declares a two-dimensional array named `matrix` with 2 rows and 3 columns, then initialises each element individually.

Using Loops for Initialization

Loops can be handy for initialising large arrays or for setting elements to default values.

One-Dimensional Array

Code: int numbers[5];

for (int i = 0; i < 5; i++) {

    numbers[i] = i + 1;

}

This uses a `for` loop to initialise each element of the `numbers` array.

Multi-Dimensional Array:

Code: double matrix[2][3];

for(int i = 0; i < 2; i++) {

    for(int j = 0; j < 3; j++) {

        matrix[i][j] = (i + 1) * (j + 1);

    }

}

This uses nested `for` loops to initialise each element of the `matrix` array.

Arrays-Timeseries

An array-timeseries is a predefined array in MQL4, such as Open, Close, High, Low, Volume, or Time. These arrays store values for historic bar characteristics. Each array-timeseries is one-dimensional and holds data for one specific characteristic of market bars. Indexes in arrays-timeseries start from zero and increase for older bars. This data is crucial and frequently used in MQL4 programming.