# Trader’s Tech – Writing Your Own EA Part 73 – 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 72 we discussed series arrays, special arrays for storing price bar data, and MQL4 functions for manipulating them. In this part we’ll continue the discussion of series array functions.

After we attempt to load the array with the rates and check for any error message, we’re going to set up the loop. The purpose of the loop is to test for any errors and reload the array if an error has occurred. You’ll notice we use the while() construct. That will cause all the lines between the braces to loop as long as the logical statement within the parentheses is true. We use the counter i to be sure we don’t get stuck in an endless loop. Now that I look at this code I don’t really like the way I used the counter and I think I would change it slightly:

`int i=0;`
```while(RateCount == -1 && LastError == 4066)
{
//all the other code
if(i > 2)
break;
i++;
}  //while(RateCount == -1 && LastError == 4066)```

I know you’re sick of hearing me say that programming is art, but this is an example of two different ways to do the same thing. The reason I like this better is the logical statement tested by the while() statement is the real test we’re trying to execute here. The test of the counter i doesn’t seem (at least to me) like it belongs in the while() statement.  I’m really not OCD – except when I’m programming, apparently. 😉

As I was saying before I rudely interrupted myself, the program will test whether the logical statement in the parentheses of the while() statement is true (in other words, if both statements on either side of the AND (&&) are true.) If so, then the code in the braces after the while() statement will execute. If RateCount == -1, then we know that there was some sort of error in the execution of ArrayCopyRates(). We then look at the error to see if it was the ERR_HISTORY_WILL_BE_UPDATED (4066) error. If that is also true, we proceed to execute the code in the braces. After executing that code, the program will loop back to the while() test again. It it’s true again, the code will execute and continue to loop until the while() test fails or something else acts to stop the loop.

If the first execution of the ArrayCopyRates() function works properly, the code within the while() loop is never executed. If there is an error, the first thing we do in the while() loop is Sleep(100) which causes the program execution to pause for a small period (100 milliseconds.) That will give MT4 time to download the rates, maybe. You may have to experiment with the time in cases like this. You don’t want it to pause too long because that would slow the progress of the EA. But too short a pause could cause the whole construct to fail.

MQL4’s built-in function RefreshRates() updates the current rate data. This is always a good idea after you have used the Sleep() function.  You want to be sure you always have the latest rate data. After that we can execute the ArrayCopyRates() function once again and then test for the LastError.

That’s all we really need to do before we execute the while() test again. But before we do that, we need a pressure valve. Anyone who’s been caught in a unending code loop knows that you need a safety valve. That’s where our i counter comes in. When we first called our code we initialized i with 0. Now we’ll test if i > 2. Obviously on the first execution it won’t be – it’s equal to 0. So we move on to the next line, i++ which increments i to 1 and our while() loop executes again. If the ArrayCopyRates() is successful this time, it won’t execute the loop code again. If it is not successful, the loop code will be executed again and i will be incremented to 2. Loop iteration will continue, incrementing i each time until i > 2 at which time the loop will break and the code after the loop will execute.

After the loop breaks, we test RateCount to see if the last execution of ArrayCopyRates() was successful. If it was not successful, we use the Alert() function to notify the operator of a problem and put a notation of the error in the Experts log.

Next time we’ll talk more about series arrays and their functions. Thanks for your attention and please follow me on Twitter and LinkedIn.

Tim

The following two tabs change content below.

#### Latest posts by Tim Black (see all)

Winner’s Edge Trading, as seen on: