Home Technical Indicator Post

Mastering the Stochastic Oscillator with Ring Buffer in MetaTrader 5

Attachments
1372.zip (10.26 KB, Download 0 times)

Description

Hey traders! Today, we're diving into the CStochasticOnRingBuffer class, which is crafted specifically for calculating the Stochastic Oscillator using a ring buffer algorithm. If you're looking to enhance your trading strategies with this powerful indicator, you’re in the right place!

Declaration

class CStochasticOnRingBuffer

Title

#include <IncOnRingBuffer\CStochasticOnRingBuffer.mqh>

Make sure to place the CStochasticOnRingBuffer.mqh file in the IncOnRingBuffer folder within your MQL5\Include directory. Don’t forget; you’ll also need the ring buffer class and the Moving Average class files in this folder for everything to work smoothly.

Class Methods

//--- initialization method:
bool Init(// returns false if there's an error, true if successful
   int period_k = 5, // period %K
   int period_d = 3, // period %D
   int period_s = 3, // period of slowing %K
   ENUM_MA_METHOD method = MODE_SMA, // smoothing method for %D
   int size_buffer = 256, // size of the ring buffer
   bool as_series = false // true if a time series, false for regular indexing
   )
//--- main calculation method:
int MainOnArray(// returns the number of processed elements
   const int rates_total, // size of the arrays
   const int prev_calculated, // processed elements in the last call
   const double &high[], // high values array
   const double &low[], // low values array
   const double &close[] // close prices array
   );
//--- calculation method using individual array elements:
double MainOnValue(// returns Stochastic value for the given element
   const int rates_total, // size of the element
   const int prev_calculated, // processed elements
   const int begin, // starting point of significant data
   const double high, // maximum value
   const double low, // minimum value
   const double close, // close price
   const int index // element index
   );
//--- methods to access data:
int BarsRequired(); // Returns the bars needed to draw the indicator
string Name(); // Returns the indicator name
string NameSignal(); // Returns the signal line name
string Method(); // Returns smoothing method as text
int PeriodK(); // Returns %K period
int PeriodS(); // Returns slowing %K period
int PeriodD(); // Returns %D period
int Size(); // Returns ring buffer size

Accessing calculated data from the ring buffer is as straightforward as accessing a regular array. Here’s an example:

//--- class with Stochastic calculation methods:
#include <IncOnRingBuffer\CStochasticOnRingBuffer.mqh>
CStochasticOnRingBuffer st;

...

//+------------------------------------------------------------------+
//| Custom indicator calculation 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[])
  {
//--- indicator calculation based on price time series:
   st.MainOnArray(rates_total, prev_calculated, high, low, close);

...

//--- using data from ring buffers                         

Related Posts

Comments (0)