Trader’s Tech – Writing Your Own EA Part 69 – 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 68 we looked at some of the details of initializing and assigning values to arrays. In this article, we’ll look at some of MQL4’s built-in functions for manipulating arrays.

MQL4 provides an interesting group of functions for manipulating arrays. You can see the whole group here. We’ll be looking at a few of the more basic functions. The more advanced functions are a discussion for another day.

int ArrayCopy(object&dest[], object source[], int start_dest=0, int start_source=0, int count=WHOLE_ARRAY) – This function copies an array to another array. This would be useful if you wanted to keep an original copy of your data and have copy to manipulate. If returns the number of elements copied. When you enter the parameters for these functions, you will enter the arrays using only the array name with no brackets or indices. For example, the array we used as an example in the last article, Pair[n], would be entered in the parameter list as Pair:

string PairCopy[12];
int TotalCopied;

TotalCopied = ArrayCopy(PairCopy, Pair);


The string array PairCopy[] would now contain the same pairs in the same positions as the string array Pair[]. The integer TotalCopied would contain the value 12.

The additional parameters:

start_dest – controls the start index in the destination array (PairCopy[].) The default is the beginning (0.)
start_source – controls the start index in the source array (Pair[].) As with start_dest, the default is the beginning (0.)
count – controls the number of indices that are copied. The default is the WHOLE_ARRAY.

int ArrayMaximum(double array[], int count=WHOLE_ARRAY, int start=0) – searches an array of doubles to find the maximum number. This is handy if you’re looking for the highest in an array of prices. It returns the index of the maximum value in the array. As with the prior function, you can specify the starting index and count of elements you want to search. If you just use the array name, it will search the entire array.

double Prices[10];
int MaxPrice;
MaxPrice = ArrayMaximum(Prices);


Prices(MaxPrice) will contain the highest price in the array.

int ArrayMinimum(double array[], int count=WHOLE_ARRAY, int start=0) – searches an array of doubles to find the minimum number. As with ArrayMaximum(), this is handy when looking for the lowest in an array of prices. It returns the index of the minimum value in the array. And again, you can specify the array start and count for the search:

double Prices[10];
int MinPrice;
MinPrice = ArrayMinimum(Prices);


Prices(MinPrice) will contain the lowest price in the array.

int ArrayRange(object array[], int range_index) – returns the number of array elements in the specified dimension. We’ve only been discussing single dimension arrays, so for our purposes, the range_index should be 0. Remember that since all the arrays are 0-based (the array indices start counting at 0), the range will be one higher than the last element in the array. If the function returns a result of -1, it means the selected dimension is not defined.

double Prices[10];
int ArrayCount;

ArrayCount = ArrayRange(Prices,0);


In this example, ArrayCount will contain the value of 10. This function does not take into account how how many array elements contain values. It only returns the declared dimension of the array (in this case 10.)

Next time we’ll look at a few more array functions. 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