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
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
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.
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.