Home Technical Indicator Post

Mastering MACD with Ring Buffer for MetaTrader 5: A Trader's Guide

Attachments
1361.zip (9.66 KB, Download 0 times)

Description

If you’re looking to enhance your trading strategy, the CMACDOnRingBuffer class is your go-to tool for calculating the Moving Average Convergence/Divergence (MACD) indicator. This nifty piece of code employs the ring buffer algorithm, making it efficient and effective.

How to Get Started

To utilize the CMACDOnRingBuffer.mqh class, you’ll need to place the file in your MQL5\Include\IncOnRingBuffer folder. For your convenience, I’ve attached examples that demonstrate how to use this class effectively. Just remember, you’ll also need the ring buffer class and the Moving Average class in the same folder to ensure everything runs smoothly.

Class Methods Overview

//--- initialization method:
bool Init( 
                                   // returns false if there’s an error, true if successful
   int fast_period = 12, // period for fast Moving Average smoothing
   int slow_period = 26, // period for slow Moving Average smoothing
   int signal_period = 9, // period for signal Moving Average smoothing
   ENUM_MA_METHOD fast_method = MODE_EMA, // method for fast Moving Average smoothing
   ENUM_MA_METHOD slow_method = MODE_EMA, // method for slow Moving Average smoothing
   ENUM_MA_METHOD signal_method = MODE_SMA, // method for signal Moving Average smoothing
   int size_buffer = 256, // size of the ring buffer
   bool as_series = false // true if a time series, false for regular indexing
);

With this setup, you’re ready to dive into the world of MACD calculations!

Accessing Indicator Data

Accessing the calculated MACD data from the ring buffer is just like pulling from a regular array. Here’s a quick example:

//--- class for MACD 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[]) 
{ 
    macd.MainOnArray(rates_total, prev_calculated, price);

    for(int i=start; i < rates_total; i++) { 
      MainBuffer[i] = macd[rates_total - 1 - i]; 
      SignalBuffer[i] = macd.signal[rates_total - 1 - i]; 
    } 
    return (rates_total);
}

Just a heads up: indexing in the ring buffer works the same way as in the time series.

Example Projects

  • The Test_MACD_OnArrayRB.mq5 file shows how to calculate the indicator based on price time series using the MainOnArray() method.
  • The Test_MACD_OnValueRB.mq5 file demonstrates the MainOnValue() method, where the MACD indicator is calculated and drawn, followed by another MACD indicator based on the first one’s ring buffer.


Output of Test_MACD_OnArrayRB.mq5 with a ring buffer size of 256 elements


Output of Test_MACD_OnValueRB.mq5 with a ring buffer size of 256 elements

Special thanks to MetaQuotes Software Corp., Integer, and GODZILLA for their contributions to this development.

Related Posts

Comments (0)