- Forex Brokers
Best Brokers
- Guide
- Education
- Forex Software
- Tools
- News
- Forex Brokers
Best Brokers
- Guide
- Education
- Forex Software
- Tools
- News
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.
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.
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.
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.
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:
Code: int myArray[10];
This example creates an array named myArray that can hold 10 integers.
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.
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:
Users can initialise an array with specific values when they declare it.
Code: int numbers[5] = {1, 2, 3, 4, 5};
This creates an array named `numbers` with 5 elements, each initialised with a specific value.
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.
Users can also initialise or modify an array’s elements after its declaration.
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.
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.
Loops can be handy for initialising large arrays or for setting elements to default values.
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.
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.
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.