Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Class for creating a ring buffer - Library for MetaTrader 5 | Forex Indicators Download - MT4/MT5 Resources

author EAcpu | 2 reads | 0 comments |

Ring buffer is one of the organizational forms for storing data. Typically, it represents a finite-length array of itself, with the oldest elements replaced by the newest data. Therefore, a certain amount of the last data is always accessible. Mainly used for asynchronous reading/writing of stream data. For more details see here .

When writing Expert Advisors and indicators, it is usually not necessary to store the calculated values ​​of all bars. It is enough to keep the last data, for example 100 bars. Ring buffers are suitable for this. Obvious advantages:

 ClassCArrayRing

title

 #include

Files of the CArrayRing.mqh class need to be placed in the IncOnRingBuffer folder, which needs to be created in MQL5\Include\. An example of the use of this class can be found in the link below.

class method

 //--- Buffer initialization method:
Boolean initialization ( // returns false if error, returns true if successful integer size, // Ring buffer size double volume = empty_VALUE // meaning of empty position buffer );
 //--- Method to add new elements to the buffer:
blank add (
constant double element // add element value );
 //--- This method overwrites the element value with the given index:
boolean update ( // false if error, true if successful constant double element, // new value of element constant integer index = 0 // element index );
 //--- This method returns the element value with the given index:
double in ( // return element value constant integer index // element index ) constant ;
 //--- This method returns the last value written to the buffer element:
double final() constant ;
 //--- This method overwrites the last element value in the buffer:
Blank last (
constant double element // new value of element );
 //--- This method returns the ring buffer size:
integer size();
 //--- This method changes the ring buffer size:
boolean resize(
const integer size // new size );

notes:

As of the publication of this article, there are three examples of using ring buffers:


Attachment download

📎 carrayring.mqh (6.57 KB)

Source: MQL5 #1340

Verification code Refresh