Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2012.12.28 08:51
Indicators

The class to draw MACD using the ring buffer - indicator for MetaTrader 5

| English Русский 中文 Español Deutsch 日本語 Português

Views:
4882
Rating:
votes: 15
\MQL5\Include\IncOnRingBuffer\
carrayring.mqh (6.64 KB)view
\MQL5\Indicators\IncOnRingBuffer\

Description

The CMACDOnRingBuffer class is designed for calculation of the technical indicator Moving Average Convergence/Divergence (Moving Average Convergence/Divergence, MACD) using the algorithm of the ring buffer

Declaration

class CMACDOnRingBuffer

Title

#include <IncOnRingBuffer\CMACDOnRingBuffer.mqh>

File of the CMACDOnRingBuffer.mqh class should 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 with the class of the ring buffer and the class of Moving Average also must be in this folder.

Class methods

//--- initialization method:
bool Init(                                  // if error it returns false, if successful - true
   int            fast_period   = 12,       // the period of fast Moving Average smoothing 
   int            slow_period   = 26,       // the period of slow Moving Average smoothing 
   int            signal_period = 9,        // the period of signal Moving Average smoothing 
   ENUM_MA_METHOD fast_method   = MODE_EMA, // the method of fast Moving Average smoothing
   ENUM_MA_METHOD slow_method   = MODE_EMA, // the method of slow Moving Average smoothing
   ENUM_MA_METHOD signal_method = MODE_SMA, // the method of signal Moving Average smoothing
   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 the 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 MACD 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 start
   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 NameMain()        // Returns the name of the main line of the indicator
string NameSignal()      // Returns the name of the indicator signal line
string FastMethod()      // Returns the method of smoothing of fast line in the form of the text line
string SlowMethod()      // Returns the method of smoothing of the slow line in the form of the text line
string SignalMethod()    // Returns the method of smoothing of the signal line in the form of the text line
int    FastPeriod()      // Returns the period of smoothing of fast line
int    SlowPeriod()      // Returns the period of smoothing of slow line
int    SignalPeriod()    // Returns the period of smoothing of signal line
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:

//--- the class with the methods of the MACD indicator calculations:
#include <IncOnRingBuffer\CMACDOnRingBuffer.mqh>
CMACDOnRingBuffer macd;

...

//+------------------------------------------------------------------+
//| 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 based on price time series:
   macd.MainOnArray(rates_total,prev_calculated,price);

...

//--- use the data from the "macd" ring buffers,
//    copy the data in the indicator buffers:
   for(int i=start;i<rates_total;i++)
     {
      MainBuffer[i]   = macd[rates_total-1-i];          // indicator histogram
      SignalBuffer[i] = macd.signal[rates_total-1-i];   // signal line of the indicator
     }
//--- 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_MACD_OnArrayRB.mq5 file calculates the indicator based on price time series. The MainOnArray() method application is demonstrated
  2. The Test_MACD_OnValueRB.mq5 file demonstrates the use of the MainOnValue() method. At first the MACD indicator is calculated and drawn. Then on the basis of the ring buffer of this indicator, one more MACD indicator is drawn. 


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



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

Translated from Russian by MetaQuotes Software Corp.
Original code: https://www.mql5.com/ru/code/1361

EF_distance EF_distance

Another variation on Moving Averages...

Exp_CorrectedAverage Exp_CorrectedAverage

The breakthrough system with the CorrectedAverage Moving Average.

PriceChannel Signal PriceChannel Signal

This indicator is the conversion of famous Igorad's indicator namely PriceChannel_Signal_v1 with re-enter feature.

XMA_TrendSignal XMA_TrendSignal

The indicator that fixes three states of the market.