Trader’s Tech – Writing Your Own EA Part 68 – Arrays

Forex Robot

If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.

In Part 67 we started talking about arrays, what they are and what advantages they provide. This time we’ll talk about how to use them.

Way back in the day, when I was learning to program (let’s say it was less than 100 years ago and more than two years ago, OK?), for some reason arrays were the thing I had the hardest time wrapping my brain around. Maybe it’s because there were far fewer educational resources and no publicly available Internet for learning at that time (OK, so maybe it was more than 5 years ago.) Actually, all I had at my fingertips was CompuServe, a 300 baud dial-up modem (compared to 56,000 bps dial-up modems from the ’90s) and a few samples of code from which to glean. I experimented quite a bit before I got a good handle on it all.  I just want you to understand that there’s no rocket surgery here. Arrays elements are just like any other variable, they’re just more easily manipulated as a group.

To ease confusion, we’re going to start by talking about single-dimension arrays. Once we talk about them, multi-dimensional arrays will be more easily grasped.

Declaring an array is as simple as declaring any other variable:

string Pair[10];
double Price[10];


This code declares a string array and a double array of 10 elements each. With these arrays you can store the prices of ten different pairs. If you would like to initialize your array with values, there are a few ways to do that.  When you create your array, string arrays are initialized with NULL values and numeric arrays are initialized with 0. The first and most straightforward way to initialize your array is to assign each individual element as you would any other variable:

Pair[0] = "EURUSD";
Pair[1] = "USDJPY";
Pair[2] = "GBPUSD";


This can be a bit tedious, but sometimes it’s the only way. If you would like to reinitialize your array with some value (all the elements the same), you can use MQL4’s ArrayInitialize() function:



I’m not sure why you would want an array of pairs initialized as “EURUSD” . I could, however, see wanting to initialize an array of status numbers to a certain value. It’s as simple as that.

And lastly, the simplest way to initialize an array makes a little more sense with a Pair array:



You can initialize each element of the array without having to specify each element. Just enclose the group in curly braces and separate each element with a comma.  I’ve divided this into two lines for the purpose of this column, but it’s not necessary to do that in the actual code.

That brings us to a convenient stopping point. Next time we’ll talk about some of the functions built into MQL4 for manipulating arrays and assigning a time series (prices) to an array.

Thanks for your attention and please follow me on Twitter and LinkedIn.



The following two tabs change content below.
Winners Edge Trading was founded in 2009 and is working to create the most current and useful Forex information and training available on the internet.

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news