Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal


The class to create the ring buffer - library for MetaTrader 5

Konstantin Gruzdev | English Русский 中文 Español Deutsch 日本語 Português

votes: 37
2012.12.27 11:56


The ring buffer is one of the form of organization to store data. Usually it represents itself finite-length array to the entry where the oldest elements are replaced by the newest data. Thus, there is always access to a certain number of the last data. Mostly used for the asynchronous reading/writing of stream data. For further details look here.

When writing the Expert Advisors and indicators often doesn't need to store the calculated values for all bars. It will be enough to keep handy the last data, for example, for 100 bars. The ring buffer is suitable for this. Obvious advantages:

  • calculations increase
  • memory economy
  • easy to use, no need to worry about the exit beyond the array.

сlass CArrayRing


#include <IncOnRingBuffer\CArrayRing.mqh>

File of the CArrayRing.mqh class need to be placed in the IncOnRingBuffer folder which is necessary to create in MQL5\Include\. There are examples to the links below which this class uses.

Class methods 

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


  • when the buffer size decrease then, as usual, not the last, but the oldest elements are missing
  • in the given realization of the ring buffer the indexing is accepted as in time series, i.e. opposite to the usual 


There are three examples of using the ring buffer to the moment of publication:


Translated from Russian by MetaQuotes Software Corp.
Original code:

Exp_ColorLeManTrend Exp_ColorLeManTrend

The Exp_ColorLeManTrend trading system is based on changes of the trend direction displayed by the ColorLeManTrend indicator

Disparity Index Disparity Index

Disparity Index (difference index) displays the difference between the close price and chosen Moving Averages in percentage. It is recommended to use in combination with the other candlestick models

The class for drawing Moving Average using the ring buffer The class for drawing Moving Average using the ring buffer

The class is designed for calculation of Moving Averages (Moving Average) using the algorithm of the ring buffer.

Combinatorics Combinatorics

Initial library of combinatorics functions.