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

Description

The CAMAOnRingBuffer class is designed for calculation of the technical indicator Adaptive Moving Average (Adaptive Moving Average, AMA) using the algorithm of the ring buffer. 

Declaration

class CAMAOnRingBuffer : public CArrayRing

Title

#include <IncOnRingBuffer\CAMAOnRingBuffer.mqh>

File of the CAMAOnRingBuffer.mqh class must be placed in 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 of the class of the ring buffer and the class of Efficiency Ratio also must be in this folder.

Class methods

//--- initialization method:
bool Init(                                  // if error it returns false, if successful - true
   int            period        = 10,       // the period of AMA 
   int            fast_period   = 2,        // the period of fast EMA 
   int            slow_period   = 30,       // the period of slow EMA
   int            size_buffer   = 256,      // the size of the ring buffer, the number of stored data
   bool           as_series     = false     // true, if a time series, false - if a usual indexing of the input data
   );
//--- the method of calculation based on a time series or indicator buffer:          
int MainOnArray(                  // returns the number of processed elements  
   const int     rates_total,     // the size of the array array[]
   const int     prev_calculated, // processed elements on the previous call
   const double &array[]          // the array of the input values
   );
//--- //--- the method of calculation based on the separate series elements of the array           
double MainOnValue(              // returns the AMA 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 data of the array starts
   const double value,           // the element value of the array
   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 FastPeriod();       // Returns the period of fast EMA smoothing  
int     SlowPeriod();       // Returns the period of slow EMA smoothing
int    Period();            // Returns the period of AMA
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:  Symbol Label - indicator MetaTrader 4
//--- the class with methods of calculation of the AMA indicator:
#include <IncOnRingBuffer\CAMAOnRingBuffer.mqh>
CAMAOnRingBuffer ama;

...

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int    rates_total, 
                const int    prev_calculated, 
                const int    begin, 
                const double &price[]) 
  {
//--- calculation of the indicator based onprice time series:
   ama.MainOnArray(rates_total,prev_calculated,price);

...

//--- use the data from the ring buffer "ama",
//    for example, copy the data in the indicator buffer:
   for(int i=start;i<rates_total;i++)
      AMA_Buffer[i] = ama[rates_total-1-i];          // indicator line

//--- return value of prev_calculated for next call:
   return(rates_total);
  }

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

Examples

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


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



The result of the work of the Test_AMA_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.

Alternative:  CCI of average - floating levels - indicator MetaTrader 5


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