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


The CMACDOnRingBuffer class is designed for calculating the technical indicator Moving Average Convergence/Divergence ( Moving Average Convergence/Divergence , MACD) ring buffer using the following algorithm.
ClassCMACDOnRingBuffertitle
#includeFiles of the CMACDOnRingBuffer.mqh class should 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 for class RingBuffer and class Moving Average must also be in this folder.
//--- Initialization method: Boolean initialization ( // returns false if error, returns true integer if successful fast_period = 12 , // period integer for fast moving average smoothing slow period = 26 , // period integer for slow moving average smoothing signal period = 9 , // period for signal moving average smoothing ENUM_MA_METHOD fast method = mode_EMA , // Fast moving average smoothing method ENUM_MA_METHOD slow method = mode_EMA , // Slow moving average smoothing method ENUM_MA_METHOD signal method = mode_SMA , // signal moving average smoothing method integer size buffer = 256 , // size of the ring buffer, the amount of data stored boolean as_series = false // true if it is a time series, false if the usual index of the input data );
//--- 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 batch[] const integer previously calculated, // The number of elements processed on the last call const double & batch[] // Array of input values );
//--- Array-based calculation method for individual series elements double the primary value ( // returns the MACD value of the collection element constant integer rate_total, // the size of the array constant integer previously calculated, // the processed array element constant integer start, // the place where the important data of the array starts constant integer the value of the constant double , // the element value of the array constant integer exponent // element index );
//--- Methods to access data: Integer BarRequired(); // Returns the number of bars required to draw the indicator StringName () // Returns the name of the indicator String name Main() // Returns the name of the indicator's main line Thin rope name signal() // Returns the name of the indicator signal line String fast method() // Method that returns fast line smoothing as a line of text String slow method() // Method that returns smooth slow lines as lines of text Thin rope signal method() // Method that returns signal line smoothing in the form of text lines Integer fast period() // Returns the smooth period of the fast line Integer slow period() // Returns the smooth period of the slow line Integer signal period() // Returns the smooth period of the signal line integer size(); // Returns the size of the ring buffer
Calculated data of the indicator can be obtained from the ring buffer just like from a usual array. For example:
//--- Class with MACD indicator calculation method: #includeCMCDOnRingBuffer macd; ... //+----------------------------------------------------------------------------------+ //|Custom indicator iteration function | //+------------------------------------------------------------------+ integer calculation ( const integer rate_total, constant integer previously calculated, Starting with a constant integer , const double &price[]) { //--- Indicator calculation based on price time series: macd.MainOnArray(rates_total,prev_calculated,price); ... //--- use data from the "macd" ring buffer, // Copy the data in the indicator buffer: for ( integer i=start;i 1 -i]; // Indicator histogram SignalBuffer[i] = macd.signal[rates_total- 1 -i]; // Signal line of the indicator } //--- Returns the prev_calculated value of the next call: return (rates_total); }
Note that the index in the ring buffer is the same as the time series .
Working result of Test_MACD_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) 📎cmacdonringbuffer.mqh (9.84 KB) 📎test_macd_onarrayrb.mq5 (3.65 KB) 📎test_macd_onvaluerb.mq5 (4.8 KB) Source: MQL5 #1361

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