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


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
#includeThe 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: #includeCDEMAOnRingBuffer 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;iStopped();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 .
Working result of Test_DEMA_OnArrayRB.mq5, ring buffer size is 256 elements When writing code for the development of MetaQuotes Software Inc. , Integer and Godzilla were used. 📎 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

Working result of Test_DEMA_OnValueRB.mq5, ring buffer size is 256 elements Attachment download
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •