The class for drawing the ATR using the ring buffer – indicator MetaTrader 5

Description

The CATROnRingBuffer class is designed for calculation of the Average True Range indicator (Average True Range, ATR) using the algorithm of the ring buffer. 

Declaration

class CATROnRingBuffer

Title

#include <IncOnRingBuffer\CATROnRingBuffer.mqh>

File of the CATROnRingBuffer.mqh should be placed to the IncOnRingBuffer folder that need to be established in MQL5\Include\. Two files with the examples used by the class from this folder are attached to the description. File with the class of the ring buffer and with the class of Moving Average also must be in this folder.

Class methods

//--- initialization method:
bool Init(                                // if error it returns false, if successful - true
   int            ma_period   = 14,       // the period of Moving Average smoothing 
   ENUM_MA_METHOD ma_method   = MODE_SMA, // the method of Moving Average smoothing
   int            size_buffer = 256,      // the ring buffer size, the number of stored data
   bool           as_series   = false     // true, if a time series, false if a usual indexing of the input data
   );             
//--- method of calculation based on a time series or indicator buffers:          
int MainOnArray(                  // returns the number of processed elements  
   const int     rates_total,     // the size of the arrays
   const int     prev_calculated, // processed elements on the previous call
   const double &high[]           // the maximum value array
   const double &low[]            // the minimum value array
   const double &close[]          // close price array
   );
//--- method of calculation based on separate series elements in the array           
double MainOnValue(              // returns the ATR value for the set element
   const int    rates_total,     // the size of the array
   const int    prev_calculated, // processed elements of the array
   const int    begin,           // from where the significant values of the array starts
   const double high,            // the maximum value 
   const double low,             // the minimum value 
   const double close,           // close price 
   const int    index            // the element index
   );
//--- methods of access to the data:
int    BarsRequired();   // Returns the necessary number of bars to draw the indicator
string Name();           // Returns the name of the indicator
string MAMethod();       // Returns the method of smoothing in the form of the text line  
int    MAPeriod();       // Returns the period of smoothing
int    Size();           // Returns the size of the ring buffer

To get the calculated data of the indicator from the ring buffer is possible as from the usual array. For example:

Alternative:  MA World - EA MetaTrader 5
#include <IncOnRingBuffer\CATROnRingBuffer.mqh>
CATROnRingBuffer atr;

...

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
//--- calculation of the indicator:
   atr.MainOnArray(rates_total,prev_calculated,high,low,close);

   ...

//--- copy data from the "atr" ring buffer to the indicator:  
   for(int i=start;i<rates_total;i++)
     {
      ATR_Buffer[i]=atr[rates_total-1-i];
     }
   return(rates_total);
  }

Please note that indexing in the ring buffer is the same as in the time series.

Examples

  1. The Test_ATR_OnArrayRB.mq5 file calculates the indicator on the basis of the price time series. The MainOnArray() method application is demonstrated
  2. The Test_ATR_OnValueRB.mq5 file demonstrates the use of the MainOnValue() method. At first the ATR indicator is calculated and drawn. Then on the basis of the ring buffer of this indicator, one more line of the ATR indicator is calculated. 


The result of the work of the Test_ATR_OnArrayRB.mq5 with the size of the ring buffer of 256 elements



The result of the work of the Test_ATR_OnValueRB.mq5 with the size of the ring buffer of 256 elements


When writing code the developments of MetaQuotes Software Corp., Integer and GODZILLA were used.


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