Discussion of article "MQL5 Cookbook - Creating a ring buffer for fast calculation of indicators in a sliding window"

 

New article MQL5 Cookbook - Creating a ring buffer for fast calculation of indicators in a sliding window has been published:

The ring buffer is the simplest and the most efficient way to arrange data when performing calculations in a sliding window. The article describes the algorithm and shows how it simplifies calculations in a sliding window and makes them more efficient.

At the start of the calculation, the indicator simply adds new values to the ring buffer of the moving average. You do not have to control the number of added values. All calculations and removals of obsolete elements occur automatically. If the indicator is called when changing the price of the last bar, the last moving average value should be replaced with a new one. The ChangeValue method is responsible for that.

The graphical display of the indicator is equivalent to the standard moving average:

Fig. 1. The simple moving average calculated in the ring buffer

Author: Vasiliy Sokolov

 

Dear Vasiliy

First of all, thank you for this excellent article and its implementations with different indicators.

I would ask you whether it would be possible to post a sample of code for the section of calculating Highs/Lows with the ring buffer? I am finding difficult to get the right results when the market is running. It seems I loose the pointer when updating the changing prices with OnChangeValue() function.

Thanks in advance.

 
Savio Araujo:

Dear Vasiliy

First of all, thank you for this excellent article and its implementations with different indicators.

I would ask you whether it would be possible to post a sample of code for the section of calculating Highs/Lows with the ring buffer? I am finding difficult to get the right results when the market is running. It seems I loose the pointer when updating the changing prices with OnChangeValue() function.

Thanks in advance.


See this example: https://www.mql5.com/en/articles/3047#c6

 
Vasiliy Sokolov:

See this example: https://www.mql5.com/en/articles/3047#c6


I have seen it. As I have checked the examples you provided. They show the same problem I had when dealing with updating the values while the market is running. Check the Stochastic you provided. Adding a new value is not a problem, but when we try to use Stoch.ChangeLast() or OnChangeValue() in CRiMaxMin class, it does not work. It does not change the value accordingly. If you could check it or send an example of a working code, that would be very nice.

Thank you.

 
Savio Araujo:

I have seen it. As I have checked the examples you provided. They show the same problem I had when dealing with updating the values while the market is running. Check the Stochastic you provided. Adding a new value is not a problem, but when we try to use Stoch.ChangeLast() or OnChangeValue() in CRiMaxMin class, it does not work. It does not change the value accordingly. If you could check it or send an example of a working code, that would be very nice.

Thank you.

Great work here, many thanks to the author. Perhaps the bug you're seeing, Savio, is here:

int RingBuffer::iToRealInd(int iIndex)

{

   if(iIndex >= iNumElements() || iIndex < 0)

      return iBufferSize-1; //previous bug was caused by no -1 here

...

I added the -1 on the last quoted line; it wasn't there before and would cause an improper index to be returned. Note that I changed variable/method names to my style of programming but it's the same idea.

 
Why does not CRiStoch have a method called ChangeValue?
 
brisully:

Great work here, many thanks to the author. Perhaps the bug you're seeing, Savio, is here:

int RingBuffer::iToRealInd(int iIndex)

{

   if(iIndex >= iNumElements() || iIndex < 0)

      return iBufferSize-1; //previous bug was caused by no -1 here

...

I added the -1 on the last quoted line; it wasn't there before and would cause an improper index to be returned. Note that I changed variable/method names to my style of programming but it's the same idea

I tried your correction, but it still does not update right. It seems there is something missing and I can't find the problem when trying to run the ring buffer in the formation of a new bar. When the market is running, the High/Low lines get completly mixed up. The code works very well and very quick while processing old data, but with new data arriving during the formation of a new bar, it simply does not work for me.

Reason: