MetaTrader 5 MT5 Libraries The simplified class of the CArrayRing256 ring buffer – library MetaTrader 5

The simplified class of the CArrayRing256 ring buffer – library MetaTrader 5

Description

The CArrayRing256 class is the simplified version of the CArrayRing ring buffer, but it is faster and allows to organize the mini time series, indicator minibuffers, short sized buffers to store intermediate stream data inside the Expert Advisor or indicator. 

Simplification based on the property of uchar-variable, which is used as the array index. For example, in the code

double array[UCHAR_MAX+1];  // the ring buffer data
uchar  index=0;             // the buffer element index

while(!IsStopped())
  {
   array[++index];          // go to the next element of the buffer
   ...
  }

the array index always takes the value from 0 to 255 and never go beyond the range of the array dimension. It allows to remove all checking from the methods associated with the possible exit of the index beyond the array exit by simplifying the methods themselves and increasing rate of performance. But the buffer size will be always constant and has the size of 256 elements. For many tasks this is an acceptable alternative to the universal class of CArrayRing in exchange for an increase the rate of the work.

Declaration
сlass CArrayRing256

Title

#include <IncOnRingBuffer\CArrayRing256.mqh>

The CArrayRing256.mqh class file should be placed in the IncOnRingBuffer folder that need to be established in MQL5\Include\.

Class methods 

//--- buffer initialization method:
void Init(
   double volue=EMPTY_VALUE // value for empty location buffers
   );
//--- addition method of new element into the buffer:
void Add(
   const double element     // added element value
   ); 
//--- the method rewrites the elements value with the set index:
void Update(                // if error it returns false, if successful - true
   const double element,    // the new value of the element
   const uchar  index=0     // the element index
   );  
//--- the method returns the value of the element with the set index:
double At(                  // returns the value of the element
   const uchar index        // the element index
   ) const;
//--- method returns value of the last written in the buffer element:
double Last() const;
//--- method overwrites last element value in the buffer:
void Last(
   const double element     // new value of the element
   );  
//--- method returns ring buffer size:
int Size();

Examples

Use of this class is similar to CArrayRing . There are a couple of differences:

  • The Init() method is used for initialization of the buffer by the definite value;
  • The Resize() method is absent as the size of the buffer is always constant.
Alternative:   Exp_ColorJJRSX_Tm_Plus - EA MetaTrader 5


📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks