Home Technical Indicator Post

Mastering DEMA with the CDEMAOnRingBuffer Class for MetaTrader 5

Attachments
1416.zip (9.14 KB, Download 0 times)

Description

If you're looking to enhance your trading strategies, the CDEMAOnRingBuffer class is a game-changer for calculating the Double Exponential Moving Average (DEMA) using a ring buffer algorithm. This tool is designed to streamline your analysis on MetaTrader 5, allowing you to leverage the power of DEMA effectively.

Declaration

class CDEMAOnRingBuffer : public CArrayRing

Title

#include <IncOnRingBuffer\CDEMAnRingBuffer.mqh>

Make sure to place the CDEMAOnRingBuffer.mqh file into the IncOnRingBuffer folder located in your MQL5\Include\ directory. Included in this folder, you’ll also find two example files that showcase how to use this class. Additionally, the class files for the ring buffer and the Moving Average are essential and should also be stored in this folder.

Class Methods

//--- initialization method:
bool Init(                                // returns false on error, true on success
   int            period      = 12,       // DEMA period
   ENUM_MA_METHOD method      = MODE_EMA, // smoothing method
   int            size_buffer = 256,      // size of the ring buffer
   bool           as_series   = false     // true if a time series, otherwise false
   );
//--- calculation method based on time series or indicator buffers:
int MainOnArray(                      // returns number of processed elements
   const int     rates_total,     // size of the array
   const int     prev_calculated, // processed elements from previous call
   const double& price[],         // array for calculation
   );
//--- method for calculation based on specific array elements:
double MainOnValue(              // returns DEMA value for specified element (bar)
   const int     rates_total,     // size of the array
   const int     prev_calculated, // processed elements of the array
   const int     begin,           // start index of significant data
   const double value,           // value of the element (bar)
   const int     index            // index of the element (bar)
   );

//--- methods for accessing data:
int                 BarsRequired(); // Returns necessary number of bars to draw the indicator
string              Name();         // Returns the indicator's name
int                 Period();       // Returns the period
int                 Size();         // Returns size of the ring buffer
double              MA(int index);  // Returns the Moving Average value, indexed like in time series

You can access the calculated DEMA data from the ring buffer just like you would from a regular array. For example:

//--- class with DEMA indicator calculation methods:
#include <IncOnRingBuffer\CDEMAOnRingBuffer.mqh>
CDEMAOnRingBuffer dema;
...
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate (const int rates_total,      // size of array price[]
                 const int prev_calculated,  // processed bars on previous call
                 const int begin,            // start index of significant data
                 const double& price[])      // array for calculation
  {
//--- calculating the indicator based on time series:
    dema.MainOnArray(rates_total,prev_calculated,price);
...
//--- using data from the "dema" ring buffer,
//    for example, copying data into the indicator buffer:
   for(int i=start;i<rates_total && !IsStopped();i++)
      DEMA_Buffer[i] = dema[rates_total-1-i]; // DEMA indicator line
...
//--- returning value of prev_calculated for next call:
   return(rates_total);
  }

When calculating the DEMA, it also calculates the Moving Average with the same parameters. You can access the data from the MA ring buffer using the MA method (int index):

//--- using data from Moving Average ring buffer,
//    for example, copying data into the indicator buffer:
   for(int i=start;i<rates_total && !IsStopped();i++)
      MA_Buffer[i] = dema.MA(rates_total-1-i); // Moving Average indicator line

Please note that indexing in the ring buffers aligns with the time series.

Examples

  1. The Test_DEMA_OnArrayRB.mq5 file demonstrates the indicator's calculation using the price time series with the MainOnArray() method.
  2. The Test_DEMA_OnValueRB.mq5 file showcases the MainOnValue() method. Initially, the DEMA indicator is calculated and displayed, followed by drawing another DEMA based on this indicator's ring buffer.


Result of Test_DEMA_OnArrayRB.mq5 with a ring buffer size of 256 elements



Result of Test_DEMA_OnValueRB.mq5 with a ring buffer size of 256 elements

 

Special thanks to the development team at MetaQuotes Software Corp., Integer, and GODZILLA for their contributions.

Related Posts

Comments (0)