# Trader’s Tech – Writing Your Own EA Part 72 – Arrays

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 71 we discussed multi-dimensional arrays. In this installment we’ll talk about a very special type of multi-dimensional array for storing time series (price bar) data.

MQL4 includes a special type of multi-dimensional array they can the series array. It’s a two-dimensional array of variable type double for the purposes of storing price rate data. The first dimensions represents the bar count (it can either be ascending or descending – and there are functions to tell you which way the data happens to be stored in your particular array.) The second dimension represents the six different time series/rate data elements for each bar:

0 = Bar Time
1 = Bar Open Price
2 = Bar Low Price
3 = Bar High Price
4 = Bar Close Price
5 = Bar Volume

The purpose of an array such as this would be to perform calculations on the data as a group. The same thing could be done directly on the rate data, but since the rate data is constantly changing, you would always have to allow for the addition of new data. That may be beneficial in some circumstances (the data is always being updated), but periodically you may want to look at old data as a group.  Additional, the series array is the easiest and best way to transfer pricing data to and from a DLL. A DLL (Dynamic Link Library) is a group of functions written in a Windows language (usually C) and compiled into a library (with a .dll file extension) that can be used in conjunction with MQL4 to perform duties outside MQL4’s native capabilities. We will be delving into using DLLs with MQL4 in future installments of our series.

The function you will want to use to load your array with price rate data is:

int ArrayCopyRates(double&dest_array[], string symbol=NULL, int timeframe=0) – this function will copy price rate info into the provided array. The array must be declared as two-dimensional. The second element of the array should be dimensioned for six elements. The first element of the array will be dimensioned automatically by the ArrayCopyRates() function. If you only pass the array name to the function, it will assume the current chart pair and the current chart time frame. If you request data from another pair or time frame and the pair/time frame combination has never been accessed in the MT4 platform in which this is run, the data will be requested and you will have to execute the ArrayCopyRates() function a second time to retrieve the data. If that is the case, the function will return a -1 and the GetLastError() function will return a 4066 (ERR_HISTORY_WILL_BE_UPDATED) error. So be sure to use this in a loop that will catch that error:

```double Rates[][6];
string Pair = "EURUSD";       //Pair
int TF = 5;                   //Time Frame
int i = 0, RateCount, LastError;
RateCount = ArrayCopyRates(Rates,Pair,TF);
LastError = GetLastError();
while(RateCount == -1 && LastError == 4066 && i < 3)
{
Sleep(100);
RefreshRates();
RateCount = ArrayCopyRates(Rates,Pair,TF);
LastError = GetLastError();
i++;
}  //while(RateCount == -1)
if(RateCount == -1)
Alert("Error Loading Series Array Rates() - Error=",LastError);```

You’ll notice we’ve added a few things to this loop. Most of this is for error trapping. Error trapping is an important part of programming. The more you handle anticipated errors in the code, the easier it is to debug the program when it is complete. We’ll discuss that further in a future installment.

Remember what I’ve said numerous times: what I show you here is one of hundreds of ways this can be done. It may not even be the best way. I present this here as a way to do it for those of you who are new programmers. As you develop your programming skills, you may find better ways to do things. I enjoy learning new techniques, so don’t hesitate to pass on your solution to these problems. When we stop learning, we die.

The first thing I like to do is declare variables and set up a few constants. Since we will be naming the pair and the time frame a couple times in the function, I like to set up a variable to store that data. That way, if we change the pair or time frame in a future installment, it only has to be changed one time near the beginning of the code. Also, if you ever want to turn this into a separate function, you can just move the declaration of these variables up to the function’s parameters list.

Next we declare the local variables we’ll be using in the code. As per usual, i is a counter variable. RateCount and LastError will be used to store those values.

After declaring all our variables it’s time to try out our ArrayCopyRates() function. We’ll set RateCount to the function’s returned value. Then we’ll get a value for the LastError. I set this here rather than check it directly in the while() statement because we may need the value for the Alert() function later on. Once the GetLastError() function is called, it clears the value of the last error, so the function will not return the same error code again.

We’re running a little long today. We’ll pick up the explanation of this code starting with the while() loop in our next installment. Thanks for your attention and please follow me on Twitter and LinkedIn.

Tim

The following two tabs change content below.

#### admin

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.

#### Latest posts by admin (see all)

Winner’s Edge Trading, as seen on: