Trader’s Tech – Writing Your Own EA Part 70 – 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 69 we discussed some of the functions MQL4 makes available for the manipulation of arrays. In this installment, we’ll continue our discussion of built-in array functions.

int ArraySize(object array[]) – this function returns a count of the total number of elements in the provided array. For this example, I’ll use a 2 dimension array – each dimension’s individual size will be 5. We’ll discuss multi-dimensional arrays in a future installment.

double Prices[5][5];
int ArrayCount;

ArrayCount = ArraySize(Prices);


ArrayCount would contain 25 (5 x 5.) If Prices[] were a single dimension array, ArraySize(Prices) is functionally the same as ArrayRange(Prices,0).

int ArraySort(double&array[], int count=WHOLE_ARRAY, int start=0, int sort_dir=MODE_ASCEND) – this function sorts an array by the first dimension. As I mentioned, we’ll discuss multi-dimensional arrays in a future installment. Just know this function will sort your array. As with prior functions you can indicate how much of the array you want to sort and the sort direction by specifying the start, count and direction (MODE_ASCEND, MODE_DESCEND.) ArraySort() will return the number of array elements sorted.

string Pairs[30];
int NumberSorted;
NumberSorted = ArraySort(Pairs);


NumberSorted will equal 30 and the whole array will be sorted in an ascending manner.

string Pairs[30];
int NumberSorted;
NumberSorted = ArraySort(Pairs,10,5,MODE_DESCEND)


NumberSorted will equal 10 (the count) and the elements 5 through 15 will be sorted in a descending manner.

int ArrayResize(object&array[], int new_size) – this function will resize the first dimension of an array. This is handy if you are unsure how many array elements you will need at compile time (programmerspeak for the time when you compile your program), allowing you to dynamically change the size of the array at runtime (programmerspeak for the time when the program is run) based upon the circumstances.

string Pairs[10];
int NewSize = 25;
int SizeBefore, SizeAfter;
SizeBefore = ArraySize(Pairs);
SizeAfter = ArrayResize(Pairs,NewSize);


SizeBefore will contain 10 (the original size of the array Pairs[]) and SizeAfter will contain 25 (the new size of the array Pairs[].)

Which brings us to a convenient stopping point for this week. New time we’ll discuss multi-dimensional arrays in anticipation of discussing series arrays (arrays of pricing information.)

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