One of the most confusing things that I've noticed for my students is the subject of arrays in MQL4. It's easy to see why a lot of them get intimidated by them but actually they're pretty straightforward.
What tends to happen is that they 'psych' themselves out and decide that they are difficult... and therefore they ARE difficult. I find the ones that get the concept the fastest are those that don't allow this to happen to them.
So if you're coming to this article with the idea that arrays in MQL4 are hard... get rid of that idea! You'll thank me in the end... trust me.
What Are Arrays in MQL4?
So what is an array? Well an array is nothing more complicated than a collection of 'stuff'. What that stuff actually is doesn't matter for right now. The first thing you need to understand is what an array is in it's most simplest form.
You should already be aware of what a variable is in MQL4 but if not be sure to . The second lesson will give you more information on what a variable is or you can search on the web.
I'll write another article on variables at some point but for now, on with arrays in MQL4.
Where as a variable is a container for a single value, an array is a container for a collection or group of values. Sounds complicated but it's really not.
Here's an example of a variable...
As you can see I've just declared a variable as type string and given it the name 'sName'. I've then assigned the value 'Steve Fleming'. Nothing too complicated and you should be able to follow along easily enough.
Don't start crowding your mind with questions like 'what is a string?' etc. If you spend too much time thinking about the little things you'll never progress as a programmer. Just accept some things at face value and move on.
Everything will become clearer as you move along but it's important not to get tied down at the beginning. Just jump in and do like Nike say... "Just Do It!"
OK, so we've seen what a simple variable looks like. Here's how you would declare an array to hold a group of associated values. In this case it's a bunch of names but it could be anything. Whatever you decide you need to group together in your particular situation.
Now we have one variable 'asNames' but it holds three values, in this case.
This is the true power of arrays in MQL4. We can now refer to any of the names in our collection by accessing the correct index in our array. We only have to have one variable name, 'asNames', and then grab an index.
Again don't panic... it's simple. An array has any number of items within it. Each one has it's own 'index' which is simply a number from 0 to the size of the array, minus one.
So in our case where we have three items/elements the indexes would be from 0 to 2. The first item would be index number 0, the next would be 1 and so on.
Elements, Items and Indexes
Write it down and say it a few times... 'arrays are zero based!'
This article was just a basic introduction to the concept of arrays in MQL4 and to illustrate that they're really not that complicated. Too many fledgling programmers are intimidated by the idea of them and either don't use them at all or use them incorrectly.
Arrays are a powerful tool and if used correctly they can save you a lot of time. They also make your code more efficient and manageable. Imagine if you had an expert advisor that had to hold 10, 50, 1000 variables and they were all related.
The poorly equipped coder would have to use a thousand single variables. Now you're much smarter than those guys and you know to use arrays in MQL4 when you're coding your own expert advisors.
Good luck with your coding and let me know how it's going.
Please leave a comment below and let me know if you found this article helpful.