banner ad

MQL4 Arrays And Why You Should Be Using Them

I’ve been helping people to learn how to code in MQL4 since 2007 and by far the most confusing topic that seems to stump students is ‘arrays’. Why that is I’m not completely sure but I have a few theories.

The idea of arrays sounds confusing at first and when I hear the blind panic in students voices I liken it to the feeling I had when I first learned about algebra. No arrays are not the same as algebra and they’re way easier to understand… but the feeling of dread and fear when presented with them for the first time is very similar.

If you understand what a variable is then you can understand what an array is. If you don’t know what a variable is then the first thing you should do is and learn the basics of MQL4.

An array is really nothing more than a regular variable that can hold multiple values.

Sounds confusing right? It’s really not so stick with me on this one.

Arrays In MQL4 Explained

Think of an array as a collection of ‘things’ or ‘values’. What those ‘things’ are is not really important right now but the point is that all of these ‘things’ are related. They are a collection of a certain type of something. Here’s an example that will make it easier for you to understand.

Let’s say we want to create an array or a collection of ‘Animals’. An obvious name for that array would be ‘Animals’…right?

That array (or collection) could have any number of items/animals in it. It could have a lion, a tiger, an elephant etc .

We could of course just have 3 different variables for the example above. With only 3 it’s not that big of a deal. But what about if we had 50 animals or maybe 500? Then it becomes a much bigger issue and we certainly wouldn’t want to declare 500 separate variables for each individual animal.

That’s exactly why we would chose an array. Since all the items are related, in this case ‘Animals’, we can just declare an array called ‘Animals’ and load in all our various types of animals. Exactly how we would create and load the array is a topic for another article but for now I just want you to understand the concept of arrays and why you would use them.

They allow us to use a bunch of related items in our code and make them relatively easy to access and manipulate. Because we have really only declared one variable – ‘Animals’ – then all we need to do to use one of them is to be able to identify that particular animal.

Accessing Elements In An Array

We do this by accessing an arrays ‘index’. Now again this might sound complicated at first but trust me, it’s really simple.

Back to our ‘Animals’ array. Let’s say we have our three animals in the array; lion, tiger and elephant. We would access each one with code similar to this…mql4 array

Now in our code when we need to access any item in the array we can just reference it by it’s name and index number. Again the specifics of exactly how we would do that in MQL4 is beyond the scope of this article but there are some simple functions available that make it easy.

The other thing to note about arrays in MQL4, as in most other programming languages, is that they are zero based. Again it sounds complicated but it’s not.

All it means is that arrays begin at zero so the first item in an array has an index of 0, the second has an index of 1 and so on and so on.

Where To Go From Here

That’s just a really simple explanation of what arrays are and why we would use them when coding our own Expert Advisors in MQL4. They’re really not too hard once you get the idea but it surprises me how many ‘programmers’, that charge for their services, have no idea how to use them.

I did some coding work just last week for a client who showed me an example to illustrate an idea he had about progressive lot sizes. While I know what I mean when I use the term, I wanted to be crystal clear on exactly what he meant just to make sure we were both on the same page.

The example he showed me was just plain crazy and it was obvious that the guy who coded it had never heard of arrays. If he had he could have written the same functionality in 4-5 lines of code instead of the 30-40 lines that he’d written.

If you look at other peoples code, which I would urge you to do, you’ll soon see who knows about and uses arrays, and who doesn’t.

The fact is they’re an essential part of any coders toolbox and help make the code more efficient and easier to maintain. If you’re not using them because they seemed too hard the first time you looked at them, you should definitely give it another try.

Once you’ve mastered arrays you’ll be one step closer to .

Tags: , ,

Category: MQL Programming

About the Author (Author Profile)

Steve Fleming has been a professional programmer and Internet professional for over 15 years. is a way to empower the non-programmers and offers a FREE 5 day E-course showing how to build an EA (expert advisor). Steve also offers his mql programming services at very competitive rates and has programmed many, many strategies both for himself and his ever growing list of satisfied clients.

Comments (0)

Trackback URL | Comments RSS Feed

There are no comments yet. Why not be the first to speak your mind.

Leave a Reply