MQL5 Arrays

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.

Learning MetaTrader MQL5 Arrays

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:

Dynamic arrays, more common in trading, resize as needed:

Initialize arrays with values or populate them later:

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:

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:

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:

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:

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:

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:

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.

MQL5 Debugging and Troubleshooting

MQL5 If Then and Conditional Operators

Leave a Comment