Every MQL programmer knows that a lot of concepts in MQL programming surrounds the usage of buffers. The "buffers" in MQL context are actually nothing more than arrays that are managed internally by MT4 itself. However, not a lot has been published on the use of Arrays and I find that rather... (I can't find a suitable word). I truly believe if one knows how to manipulate arrays at will, one can do wonders with MQL. That includes overcoming certain limitations of the MQL language. This spurs me to write a little something about our unsung hero, our beacon of hope, our all time (sometimes agonising) saviour... The ARRAY
This post is dedicated to discussions over the use of Arrays and nothing else. You are free to post questions and I (or other programmers) can help where we can.
Before reading on, I urge all beginners to at least read up on what an Array is. A good explanation article can be found here http://book.mql4.com/variables/arrays
--------------------------------------------------------
Defining an array's size based on user input
If ever you wanted an array that doesn't have a fixed size at compilation time but rather changes based on user's needs then this trick is for you.
For those of you who tried the code below but failed, this post aims to offer a workaround.
The snipplet above will result in a couple of compilation error.
-> 'Size' - integer number expected
-> ']' - comma or semicolon expected
That's because "Size" actually has no value at this point in time yet. It only gets the value of 5 at the time you load this (script, indicator or EA) onto chart.
Which is why I'm going to introduce you to an array user's best friend
int ArrayResize( object&array[], int new_size)
Sets a new size for the first dimension. If executed successfully, it returns count of all elements contained in the array after resizing, otherwise, returns -1, and array is not resized.
Note: Array declared at a local level in a function and resized will remain unchanged after the function has completed its operation. After the function has been recalled, such array will have a size differing from the declared one.
The following code snipplet will use ArrayResize to workaround the compilation error.
What this does is simple. myArray has no size at compile time. but we know that upon loading, the init() function always run first, and by that time, all extern variables are initialised, which means the value of Size has been put in and we use ArrayResize to change the size of myArray to whatever it is the user had placed in the input tab.
I've been using this for quite a while and I'm sure this method can some good in the right hands.
My next post will be about how to use Arrays to mimic buffers in order to overcome indicator's limitation of only allowing 8 buffers.
(I've once written a single indicator that requires 17 buffers for its computations)
This post is dedicated to discussions over the use of Arrays and nothing else. You are free to post questions and I (or other programmers) can help where we can.
Before reading on, I urge all beginners to at least read up on what an Array is. A good explanation article can be found here http://book.mql4.com/variables/arrays
--------------------------------------------------------
Defining an array's size based on user input
If ever you wanted an array that doesn't have a fixed size at compilation time but rather changes based on user's needs then this trick is for you.
For those of you who tried the code below but failed, this post aims to offer a workaround.
Inserted Code
extern int Size = 5; int myArray[Size];
The snipplet above will result in a couple of compilation error.
-> 'Size' - integer number expected
-> ']' - comma or semicolon expected
That's because "Size" actually has no value at this point in time yet. It only gets the value of 5 at the time you load this (script, indicator or EA) onto chart.
Which is why I'm going to introduce you to an array user's best friend
int ArrayResize( object&array[], int new_size)
Sets a new size for the first dimension. If executed successfully, it returns count of all elements contained in the array after resizing, otherwise, returns -1, and array is not resized.
Note: Array declared at a local level in a function and resized will remain unchanged after the function has completed its operation. After the function has been recalled, such array will have a size differing from the declared one.
The following code snipplet will use ArrayResize to workaround the compilation error.
Inserted Code
extern int Size = 5; int myArray[]; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { ArrayResize( myArray, Size ); }
What this does is simple. myArray has no size at compile time. but we know that upon loading, the init() function always run first, and by that time, all extern variables are initialised, which means the value of Size has been put in and we use ArrayResize to change the size of myArray to whatever it is the user had placed in the input tab.
I've been using this for quite a while and I'm sure this method can some good in the right hands.
My next post will be about how to use Arrays to mimic buffers in order to overcome indicator's limitation of only allowing 8 buffers.
(I've once written a single indicator that requires 17 buffers for its computations)
Programming for a better future.