Watch how to download trading robots for free
Find us on Telegram!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Indicators

The class for drawing the OBV using the ring buffer - indicator for MetaTrader 5

Views:
6003
Rating:
(21)
Published:
2013.01.16 15:38
Updated:
2016.11.22 07:32
\MQL5\Indicators\OnRingBuffer\ \MQL5\Include\IncOnRingBuffer\
carrayring.mqh (6.64 KB) view
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

Description

The COBVOnRingBuffer class is designed for calculation the technical indicator On Balance Volume (On Balance Volume, OBV) using the algorithm of  the ring buffer.  

Declaration

class COBVOnRingBuffer : public CArrayRing

Title

#include <IncOnRingBuffer\COBVnRingBuffer.mqh>

File of the COBVOnRingBuffer.mqh class must 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 also must be in this folder.

Class methods

//--- initialization method:
bool Init(                                        // if error it returns false, if success - true
   int                 period      = 0,           // the OBV period
   int                 size_buffer = 256,         // the size of the ring buffer
   bool                as_series   = false        // true, if a time series, otherwise - false
   );

The OBV period is used as in case of Integer: if the value is 0 (default) the indicator works like the one included to the terminal - it will be calculated for all bars of the chart. In case of any other positive value specified number of bars is used (Moving Average principle) for value calculation at each chart bar. 

//--- the method of calculation based on a time series or the indicator buffers:          
int MainOnArray(                  // returns the number of the processed elements  
   const int     rates_total,     // the size of the arrays
   const int     prev_calculated, // processed elements on the previous call
   const double& price[],         // price
   const long&   volume[]);       // volume
   );
//--- the method of calculation based on a separate elements of the array           
double MainOnValue(              // returns the OBV value for the set element (bar)
   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 starts
   const double price,           // price
   const long   volume,          // volume
   const int    index            // the element (bar) 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
int                 Period();       // Returns the period
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 calculation of the OBV indicator:
#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[])
  {
//--- the calculation of the indicator based on a time series:
      obv.MainOnArray(rates_total,prev_calculated,close,tick_volume);

...

//--- use the data from the ring buffer "obv",
//    for example, copy the data in the indicator buffer:
   for(int i=start;i<rates_total && !IsStopped();i++)
      OBV_Buffer[i] = obv[rates_total-1-i]; // indicator line

...

//--- 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 indicator calculates the Test_OBV_OnArrayRB.mq5 file on the basis of the price time series. The MainOnArray() method application is demonstrated
  2. The Test_OBV_OnValueRB.mq5 file demonstrates the use of the MainOnValue() method. At first the OBV indicator is calculated and drawn. Then on the basis of this indicator ring buffer one more OBV is drawn. 


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



The result of the work of the Test_OBV_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 Ltd.
Original code: https://www.mql5.com/ru/code/1402

The 4-color АО indicator The 4-color АО indicator

The 4-color АО indicator.

StepSto_v1 StepSto_v1

The trend indicator. The indicator line of level 50 can serve as the point to determine the trend

RoundPrice-Ext RoundPrice-Ext

A very convenient indicator to confirm any signals of entry to the market

Renko_v1 Renko_v1

The trend indicator which draws the boundaries of the channel