Assistance with "bizarre" array out of range error

 

Hi guys,

I am attempting to learn about how to control chart buffers and change which buffers are displayed on the chart versus which ones are not.  I decided to use the "Parabolic SAR" as an example, and for my little exercise I decided to try plotting the "step" size rather than the SAR value itself in a separate window.  Accordingly, I have modified the standard parabolic SAR that is supplied with MT4/MQL4.

The file is attached - I know it is quite long but I have only modified a handful of rows.  They key rows that I have modified or added are:

In the intro section I have added the following lines:

#property indicator_separate_window
#property indicator_buffers 1

and I declare a new buffer that will store the "step" size.

double       ExtACTBuffer[];

In the "on init" section I attempt to modify the code to say that the ACT buffer rather than the SAR buffer should be plotted:

   IndicatorBuffers(2);
   SetIndexBuffer(0,ExtACTBuffer, INDICATOR_DATA);
   SetIndexBuffer(1,ExtSARBuffer, INDICATOR_CALCULATIONS);

I then copy similar lines of code from the existing parabolicSAR code, such as:

   ArraySetAsSeries(ExtACTBuffer,false);
   ArrayInitialize(ExtACTBuffer,0.0);

Then, every time a piece of data is written into the "ExtSARBuffer", I copy paste the line and write the "step" into the ACTBuffer:

         ExtSARBuffer[i++]=last_low;     // this bit is existing
         ExtACTBuffer[i]=step;          // this is the bit that I have added.

The code compiles without errors.  When I attach it to a chart I indeed get a new indicator window and it appears to plot the activity factor properly, but when it gets to the end of the calculation (65000th bar), there is an out of range error which states:

2020.06.14 15:21:35.172 ActivityFactorDemo EURUSD-sb,M5: array out of range in 'ActivityFactorDemo.mq4' (243,19)

My understanding is that this error is caused by trying to put an "Nth" item in an array that isn't N items long.  What I don't understand is that I have only entered values into the ACTBuffer when a value is entered in the SARBuffer, and I have not added any lines of code which increment "i" by one, so this behaviour seems very strange.

I note that there is a discussion about indicator buffers and out of range errors here:

https://www.mql5.com/en/articles/1391#2_1

As I understand it, that article is concerning the situation where the markets are open and the indicator is running, and the bar count changes partway through calculation (?).  I wrote and tested my code when the market was closed, so surely it is not possible for this to be the cause of my problems?

I apologise if this is somewhat of a rookie query, but any assistance would be greatly appreciated.

Kind regards,

Paul


Common Errors in MQL4 Programs and How to Avoid Them
Common Errors in MQL4 Programs and How to Avoid Them
  • www.mql5.com
Some older programs can return errors in the new version of the MQL4 of compiler. To avoid critical completion of programs, the previous version compiler handled many errors in the runtime environment. For example, division by zero or array out of range are critical errors and usually lead to application crash. Such errors occur only in some...
Files:
 

Attach your code and tell us which line is giving the error.


In future please post in the correct section

I will move your topic to the MQL4 and Metatrader 4 section.

 
Keith Watford:

Attach your code and tell us which line is giving the error.


In future please post in the correct section

I will move your topic to the MQL4 and Metatrader 4 section.

Apologies Keith - I was partway through writing my post and accidentally clicked "done" rather than "insert" when using the code tags and it posted my partially written thread!  Hopefully it is complete now and makes sense!

 
Paul Garlick:


      Print("Z", " ", ArraySize(ExtSARBuffer), " ", ArraySize(ExtACTBuffer), " ", i);
      ExtSARBuffer[i++]=sar;
      ExtACTBuffer[i]=step;

You increment i in line 242, could that be the problem? I am never sure about i++ in strange places.

 
Keith Watford:

You increment i in line 242, could that be the problem? I am never sure about i++ in strange places.

The i++ is in the original code.

The problem is he added his ExtACTBuffer[] filling AFTER, it should always be BEFORE. Not only at line 242 but in all other occurrences.

And you are right in this case, the original is not good using this i++ at 4 different places which are mutually exclusive. Bad coding practice.

 

Thank you guys - thank you Alain, swapping the "ExtACTBuffer" and the "ExtSARBuffer" lines around has indeed fixed the problem.

Just for my understanding though, does this not effectively change how the code is behaving and makes the "SAR" and the "step" values out of sync in their arrays?

What I mean is, I thought that doing this for example:

            ExtACTBuffer[i]=step;
            ExtSARBuffer[i++]=last_low;

Is the equivalent of saying:

"Assign the value of <step> to the ExtACTBuffer array at position (i)" and then

"Assign the value of <last_low> to the ExtSARBuffer at position (i+1)"

So that "pair" of values of step/last_low is now split between positions i and i+1 depending on which array is being looked at.

I thought that in the way I had them in my code, it was the equivalent of saying:

"Assign the value of <last_low> to the ExtSARBuffer at position (i+1)"

"Assign the value of <step> to the ExtACTBuffer array at position (i) which has the same value as in the line above", meaning that the step/last_low values are at equivalent positions in the two buffers?

 
   SetIndexBuffer(0,ExtACTBuffer, INDICATOR_DATA);
   SetIndexBuffer(1,ExtSARBuffer, INDICATOR_CALCULATIONS);
This is MQL5 code not MQL4
 
William Roeder:
This is MQL5 code not MQL4
It's mql4 compatible, but it does nothing in mql4.
 
Paul Garlick:

Thank you guys - thank you Alain, swapping the "ExtACTBuffer" and the "ExtSARBuffer" lines around has indeed fixed the problem.

Just for my understanding though, does this not effectively change how the code is behaving and makes the "SAR" and the "step" values out of sync in their arrays?

Not it's not. The ++ is applied after i is used as index.It's post-increment, read the documentation please.
 
Alain Verleyen:
Not it's not. The ++ is applied after i is used as index.It's post-increment, read the documentation please.

Ok - thank you Alain for clarifying on both points - I will read up on this further!

Reason: