Arrays in MQL4: A Comprehensive Guide
Watch on YouTube: MQL4: Mastering Arrays for Efficient Data Management
Arrays are essential data structures in MQL4, allowing you to store and manage multiple values of the same type under one variable name. You can define arrays for any data type, such as int, double, string, and more.
Types of Arrays:
1. Single-Dimensional Arrays: These are the most common type of arrays and are used to store a list of values.
int prices[10]; // array to hold 10 integers
2. Multi-Dimensional Arrays: You can also create arrays with multiple dimensions, like matrices.
double matrix[3][3]; // 3x3 matrix
Array Initialization
Arrays in MQL4 can be initialized during declaration or later in the code. Here’s an example:
void OnStart() {
int values[]; // empty array declared
ArrayResize(values, 5); // resizing the array to hold 5 elements
values[0] = 10; // assigning values
Print(values[0]);
}
Important Functions
ArrayResize(): Used to change the size of an array.ArraySetAsSeries(): Makes the array behave like a time series.ArrayCopy(): Copies data from one array to another.
Accessing Array Elements
Each element in an array can be accessed by its index. Remember, the index starts from 0 in MQL4:
int value = prices[0]; // accessing the first element of the prices array
Practical Example:
void OnStart() {
// Define array elements
int values[5];
values[0] = 10;
values[1] = 20;
values[2] = 30;
values[3] = 40;
values[4] = 50;
// Run for loop on array indexes
for (int i = 0; i < 5; i++) {
Print("Value at index ", i, ": ", values[i]);
}
}
To avoid confusion between arrays and time series, it’s important to note that time series in MQL4 are arrays with reverse indexing.
In a time series, the first element is placed at the far right, while the last element appears at the far left. To convert a regular array to a time series, you can reverse the order of the array elements using specific functions like ArraySetAsSeries(). This ensures the data is arranged in the correct order for time series processing.
Pass by Reference
In MQL4, arrays are passed by reference when used as function arguments, meaning any changes made to the array within the function directly affect the original array outside of the function. This reference passing makes it possible to manipulate large datasets efficiently without duplicating memory. To pass an array, declare it as a parameter in the function, as shown below:
void UpdateArray(int &arr[]) {
for (int i = 0; i < ArraySize(arr); i++) {
arr[i] += 10; // Modify each element by adding 10
}
}
void OnStart() {
int myArray[] = {1, 2, 3, 4, 5};
UpdateArray(myArray); // Pass the array by reference
// myArray now holds {11, 12, 13, 14, 15}
Print("First element changed to: ", myArray[0]);
}
In this example, UpdateArray modifies each element of myArray, and the changes persist after the function call.
Conclusion
Arrays are powerful in MQL4 for managing and manipulating collections of data. Whether you are handling prices, indicators, or other numerical data, arrays allow for efficient processing and storage. Use array functions like ArrayResize and ArrayCopy to optimize data handling in your MQL4 scripts, indicators, and Expert Advisors.
