[EA・Program] The most terrifying error: an active EA disconnecting from the chart
Hello. This is 2pay.
This time it's another little story related to EA.
Have you ever had an EA that was running suddenly come off?
After an EA has established a position, there are rare cases where the EA detaches from the chart by itself and the position becomes uncontrollable.
When an EA gets into an abnormal state, an error is displayed in the operation history or expert log.
The scariest error listed as the theme this time is "array out of range".
This is an error that occurs when accessing an index outside the array, and when it occurs the program is forced to stop.
For indicators, it remains on the chart, but the calculation is not performed and nothing is displayed.
And for EAs, the program is removed from the chart entirely.
Since there is no notification sent upon an error, you may not notice it while sleeping or when you are out, and a position that no one controls can produce enormous profits or losses.
EA users who notice something abnormal cannot understand the situation and hesitate about whether they should close the position.
When trying to set up the EA again, they are repeatedly rejected by the same error.
When does this occur?
To understand that, you need to know about arrays. (The following uses a slightly simplified explanation.)
An array is like a series of consecutive variables.
In programming, a variable is a box you can name as you like and store data (numbers or characters) in.
If you assign the number "1" to a box named "a",
you can store the number 1 inside box a by writing a=1.
Conversely, if you want to know what is inside the box "a",
you write something like Print(a). (Print() is a command (function) that outputs the result to the log.)
Then the log will display "1".
When preparing a large number of boxes like "a", you could create "b", "c", etc., but if you need hundreds, coming up with variable names becomes difficult.
Therefore, to be able to treat "a" as a series, we convert it into an array.
By writing a[100],
we have prepared 100 variables a from a[0] to a[99]. (In the MQL language, arrays start from 0.)
Each of the 100 variables can be assigned a different number.
a[0]=1
a[1]=25
a[99]=-10
and so on.
Now,
a[250]=45
what would happen?
The array a was only created up to 100 elements (up to index 99) previously.
Is it possible to assign a number to a non-existent index 250?
The answer is No.
Because you are specifying a index outside the array range (0~99).
This is an array out of range.
Among the sources that use arrays, the representative ones are the four price values (OHLC).
The close is defined as "close[]" and the array range matches the number of bars displayed on the chart.
An array that is ordered in time sequence like the four-price values is called a "time-series array".
Normally, [0] (= index 0) is the current bar, and the oldest bar is the largest value [bar_count-1].
For example, if the chart has 3000 bars, accessing close[4500], which is older than 3000, will cause an out-of-range error.
If an EA or indicator must calculate by looking back at least 3000 bars, the developer should set the starting bar to [2999] and count down to [0].
What matters here is that the user side should display at least more than 3000 bars to accommodate that.
If the developer provides a directive, follow it.
Of course, the developers should also take countermeasures.
Because users may change the number of bars under certain circumstances, it is better to add an alert to indicate to the user before an array error occurs whether the array range is insufficient or an incorrect specification has been made, and to encourage increasing the bar count.
In actual coding, it is uncommon to assign a number directly to an element of the array; typically you specify it with a variable.
close[a]
Doing so increases coding convenience, but you won't know where you got stuck unless you output what is inside a to the outside.
The following condition allows output only if it is within the close array range. If a is not smaller than the array size, close[a] will not be executed.
if( ArraySize(close)-1 > a ){
Print( close[a] ); }
When running inside a large loop, assign ArraySize(close) to a variable outside the loop and avoid calling the function many times inside the loop.
When I first started studying, I had no idea why this error occurred and struggled quite a bit to understand how it worked.
Nevertheless, being involved in development is essential, and basically you must implement error avoidance so that array out of range does not occur.
That brings us to the end of today's discussion.
Thank you for reading.