When programming in MetaTrader 5, MQL5 arrays are a game-changer for managing data in Expert Advisors (EAs), indicators, and scripts.
Arrays let you store, organize, and manipulate multiple values efficiently, think price histories, trade signals, or indicator outputs.
Expertise in using arrays in MQL5 unlocks flexibility and precision in your trading algorithms.
In this article, we’ll explore MQL5 array basics, syntax, and practical examples to supercharge your projects.
What Are MQL5 Arrays?
The concept of “Array” is not always simple to understand, especially for beginner coders.
In the most simple form, think of an array like a sequence of values of a defined type.
An array is in most cases a variable, it has a data type, and memorizes a sequence of values.
An array in MQL5 is a collection of elements of the same data type (e.g., double, int) stored under one name.
You access elements via an index, starting at 0.
Arrays come in two flavors: static (fixed size) and dynamic (resizable).
They’re perfect for tracking sequences, like candle prices or RSI values.
The is also some official documentation available here if you were interested.
How to Use Arrays in MQL5
The use of arrays is not as straight forward as any other variable, because an array stores a sequence of values.
Being a sequence there are ways to interact with it both when it comes to store data in it or extract data from it.
Declaring and Initializing Arrays
First thing, you will need to let MT5 know that a variable is actually an array, and you do so in the declaration, using []
Static arrays have a fixed size defined at declaration, so you know the size of the sequence:
|
1 |
double priceArray[5]; // Array for 5 prices |
Dynamic arrays, more common in trading, resize as needed:
|
1 |
double dynamicPrices[]; |
Initialize arrays with values or populate them later:
|
1 |
double fixedArray[3] = {1.2000, 1.2010, 1.2020}; |
As you see, an array called fixedArray was created, it is of double type and contains a string of three double numbers.
For dynamic arrays, use ArrayResize() to set the size:
|
1 2 3 4 5 6 |
double tradeLevels[]; void OnInit() { ArrayResize(tradeLevels, 10); // Space for 10 levels tradeLevels[0] = 1.2050; // First element } |
Some Best Practice to Use MQL5 Arrays
Like anything, there are several ways to use MQL5 arrays, however some are more efficient than others and contribute to make better programs.
These are some of the best practices when it comes to MQL5 Arrays
Use Arrays for Price Data
Arrays shine when handling price histories.
An example could be to store the last 10 closing prices like this:
|
1 2 3 4 5 6 7 |
double closePrices[]; void OnTick() { ArrayResize(closePrices, 10); CopyClose(Symbol(), 0, 0, 10, closePrices); // Fills array with last 10 closes Print("Latest close: ", closePrices[0]); // Most recent price } |
CopyClose() populates the array from MT5 price data, with index 0 as the latest candle. This is ideal for trend analysis in your EAs.
Loop Through Arrays
Loops are a very common way to work with arrays.
Because they are sequences of values, looping through them is often necessary.
Process array data with loops.
Calculate the average of those 10 prices:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
double GetAveragePrice(double &prices[]) { double sum = 0; for(int i = 0; i < ArraySize(prices); i++) { sum += prices[i]; } return sum / ArraySize(prices); } void OnTick() { double closePrices[]; ArrayResize(closePrices, 10); CopyClose(Symbol(), 0, 0, 10, closePrices); double avgPrice = GetAveragePrice(closePrices); Print("10-bar average: ", avgPrice); } |
The & in double &prices[] passes the array by reference, avoiding unnecessary copying, which is a performance win.
Multidimensional Arrays for Complex Data
MQL5 supports multidimensional arrays for layered data.
Another way of viewing them would be like a multidimensional matrix.
An example could be storing high and low prices together:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
double priceRange[][2]; // [rows][high, low] void OnInit() { ArrayResize(priceRange, 5); // 5 candles for(int i = 0; i < 5; i++) { priceRange[i][0] = iHigh(Symbol(), 0, i); // High price priceRange[i][1] = iLow(Symbol(), 0, i); // Low price } } void OnTick() { Print("Latest high: ", priceRange[0][0], " | Low: ", priceRange[0][1]); } |
This structure suits indicators tracking multiple metrics.
Check Array Bounds
Array errors during compiling or code execution are very frequent, these happens when you try to work outside of the array boundaries.
You can avoid runtime errors by staying within array limits.
Use ArraySize() and validate indices:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
double values[]; void OnTick() { ArrayResize(values, 3); int index = 5; if(index < ArraySize(values)) { values[index] = Bid; } else { Print("Error: Index ", index, " exceeds array size ", ArraySize(values)); } } |
Out-of-bounds access crashes your code, which is bad, this check prevents that.
Leverage Built-in Array Functions
MQL5 offers handy array utilities like ArraySort(), ArrayMinimum(), and ArrayMaximum(). Find the lowest price in an array:
|
1 2 3 4 5 6 7 8 |
double prices[]; void OnTick() { ArrayResize(prices, 10); CopyLow(Symbol(), 0, 0, 10, prices); int minIndex = ArrayMinimum(prices); Print("Lowest price: ", prices[minIndex]); } |
These functions save time and optimize your code.
Conclusion
MQL5 arrays are a cornerstone of efficient trading code.
Whether tracking prices, calculating averages, or organizing complex data, arrays streamline your work.
Start simple, test in the Strategy Tester, and build up to multidimensional magic.
In many cases you may not need them, but in many others you will, so hopefully you now understand them.
For questions and support please Contact Us.