Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Class for drawing DEMA using ring buffer - MetaTrader 5 script | Forex indicator download - MT4/MT5 resources

author EAcpu | 2 reads | 0 comments |

The class for drawing the DEMA using the ring buffer - indicator for MetaTrader 5

The class for drawing the DEMA using the ring buffer - indicator for MetaTrader 5

The CDEMAOnRingBuffer class is designed for calculating the technical indicator Double Exponential Moving Average (DEMA) ring buffer using the following algorithm.

 Class CDEMAOnRingBuffer: publicCArrayRing

title

 #include

The file of CDEMAOnRingBuffer.mqh class must be placed in the IncOnRingBuffer folder that needs to be created in MQL5\Include\. Attached to the description are two files containing examples used by the classes in this folder. Files with the following categories Ring Buffer and Moving Average must also be in this folder.

 //--- Initialization method:
boolean initialization ( // return false if error, true if success integer period = 12 , // DEMA period ENUM_MA_METHOD method = mode_EMA , // smoothing method integer buffer size = 256 , // size of ring buffer boolean as_series = false // true if time series, otherwise - false );
 //--- Calculation method based on time series or indicator buffer:
Integer MainOnArray( // Returns the number of elements processed constant integer rate_total, // the size of the array constant integer previously calculated, // the number of elements processed on the last call constant integer & prices[], // the array used for calculation );
 //--- Method for calculation based on individual series elements of an array
Double the main value ( // returns the DEMA value constant integer of the set element (bar) rate_total, // the size of the array constant integer previously calculated, // the processed array element constant integer start, // where the important data of the array begins
c double value, // element (bar) value
c Angst integer index // element (bar) index );
 //--- Methods to access data:
Integer BarRequired(); // Returns the number of bars required to draw the indicator
string name(); // returns the name of the indicator
integer period (); // returns period
integer size(); // Returns the size of the ring buffer
Double MA ( integer index); // Returns the value of the moving average, indexed like a time series

Calculated data of the indicator can be obtained from the ring buffer just like from a usual array. For example:

//--- Class with DEMA indicator calculation method:
#include CDEMAOnRingBuffer dema;

... //+----------------------------------------------------------------------------------+
//|Custom indicator iteration function |
//+------------------------------------------------------------------+
When integer calculation ( const integer rates_total, // size of array prices[] constant integer previously calculated, // constant integer start of bar processed at last call , // const double & prices[] where important data starts ) // array for calculation { //--- calculation of indicators based on time series: dema.MainOnArray(rates_total,prev_calculated,price);

... //--- use data from the "dema" ring buffer,
// For example, copy the data in the indicator buffer: for ( integer i = start;i  Stopped();i++)
      DEMA_Buffer[i] = dema[rates_total- 1 -i]; // DEMA indicator line ... //--- Return the prev_calculated value for the next call: return (rates_total);
  }

Calculating DEMA also performs the same parameters as calculating moving averages . We can get data ( integer exponent) from MA ring buffer using MA method:

 //--- use data from the moving average ring buffer,
// For example, copy the data in the indicator buffer: for ( integer i = start;i  Stopped();i++)
      MA_Buffer[i] = dema.MA(rates_total- 1 -i); // Moving average indicator

Note that the index in the ring buffer is the same as the time series .

The class for drawing the DEMA using the ring buffer - indicator for MetaTrader 5

Working result of Test_DEMA_OnArrayRB.mq5, ring buffer size is 256 elements

The class for drawing the DEMA using the ring buffer - indicator for MetaTrader 5
Working result of Test_DEMA_OnValueRB.mq5, ring buffer size is 256 elements

When writing code for the development of MetaQuotes Software Inc. , Integer and Godzilla were used.


Attachment download

📎 cmaonringbuffer.mqh (14.9 KB)

📎 carrayring.mqh (6.64 KB)

📎 cdemaonringbuffer.mqh (8.17 KB)

📎test_dema_onarrayrb.mq5 (3.03 KB)

📎test_dema_onvaluerb.mq5 (3.44 KB)

Source: MQL5 #1416

Verification code Refresh