Description
The COBVOnRingBuffer class is crafted to calculate the On Balance Volume (OBV) indicator using the ring buffer algorithm. This is an essential tool for traders looking to enhance their volume analysis.
Declaration
class COBVOnRingBuffer : public CArrayRing
File Inclusion
#include <IncOnRingBuffer\COBVOnRingBuffer.mqh>Make sure to place the COBVOnRingBuffer.mqh file in the IncOnRingBuffer folder, which should be created within your MQL5\Include\ directory. Two example files that utilize this class are included in the description, and don’t forget to have the ring buffer class file in this folder as well.
Class Methods
//--- initialization method: bool Init( // returns false on error, true on success int period = 0, // the OBV period int size_buffer = 256, // size of the ring buffer bool as_series = false // true for time series, otherwise false );
The OBV period works similarly to the Integer class: if set to 0 (the default), the indicator functions as it does in the terminal, calculating across all chart bars. For any other positive value, it will utilize that number of bars for calculations at each chart bar, akin to a Moving Average.
//--- method for calculation based on time series or indicator buffers: int MainOnArray( // returns the number of processed elements const int rates_total, // size of the arrays const int prev_calculated, // processed elements from previous call const double& price[], // price array const long& volume[]); // volume array
//--- method for calculation based on individual elements of the array: double MainOnValue( // returns the OBV value for a specific element (bar) const int rates_total, // size of the array const int prev_calculated, // processed elements of the array const int begin, // where significant data starts const double price, // price const long volume, // volume const int index // element (bar) index );
//--- methods for data access: int BarsRequired(); // Returns the number of bars needed to display the indicator string Name(); // Returns the indicator's name int Period(); // Returns the period int Size(); // Returns the size of the ring buffer
To access the calculated data from the ring buffer, you can treat it just like a regular array. For instance:
//--- class for OBV indicator calculations: #include <IncOnRingBuffer\COBVOnRingBuffer.mqh> COBVOnRingBuffer obv; ... //+------------------------------------------------------------------+ //| Custom indicator iteration function //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime& time[], const double& open[], const double& high[], const double& low[], const double& close[], const long& tick_volume[], const long& volume[], const int& spread[]) { //--- calculating indicator based on time series: obv.MainOnArray(rates_total, prev_calculated, close, tick_volume); ... //--- use data from the ring buffer "obv", // for example, copy data into the indicator buffer: for(int i=start; i<rates_total && !IsStopped(); i++) OBV_Buffer[i] = obv[rates_total - 1 - i]; // indicator line ... //--- return prev_calculated for the next call: return(rates_total); }
Please remember that indexing in the ring buffer is consistent with time series indexing.
Examples
- The
Test_OBV_OnArrayRB.mq5file showcases OBV calculations based on the price time series, demonstrating the usage of theMainOnArray()method. - The
Test_OBV_OnValueRB.mq5file illustrates theMainOnValue()method. Initially, the OBV indicator is calculated and displayed, followed by a secondary OBV drawn from this indicator's ring buffer.

Outcome from the Test_OBV_OnArrayRB.mq5 with a ring buffer size of 256 elements

Outcome from the Test_OBV_OnValueRB.mq5 with a ring buffer size of 256 elements
Code development contributions from MetaQuotes Software Corp., Integer, and GODZILLA.
Related Posts
- Hourly Buffers for Data Collection in MetaTrader 5: A Simple Guide
- Unlock Trading Insights with Volume Profile + Range v6.0 for MetaTrader 5
- Understanding On Balance Volume: A Trader’s Guide
- Mastering the MACD Candle Indicator for MetaTrader 4
- Mastering the On Balance Volume (OBV) Indicator for Trading Success