Mastering OsMA with Ring Buffer: A Guide for MetaTrader 5 Traders

Mike 2013.01.08 00:45 148 0 0
Attachments

Description

Welcome, fellow traders! Today, we’re diving into the COsMAOnRingBuffer class, a nifty tool for calculating the Moving Average of Oscillator (OsMA) using a ring buffer algorithm. If you’re looking to enhance your trading strategies with technical indicators in MetaTrader 5, you’re in the right place!

Declaration

class COsMAOnRingBuffer : public CArrayRing

Title

#include <IncOnRingBuffer\COsMAOnRingBuffer.mqh>

To get started, make sure you place the COsMAOnRingBuffer.mqh file into the IncOnRingBuffer folder, which you’ll need to create in MQL5\Include\. I've attached two example files used by this class, so check them out. You’ll also want to ensure that the files for the ring buffer, MACD, and Moving Average classes are 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 indicator buffer:int MainOnArray( // returns the number of processed elements
   constint rates_total, // the size of the array array[]
   constint prev_calculated, // processed elements on the previous call
   constdouble &array[] // the array of the input values
   );
//--- the method of calculation based on the separate series elements of the arraydouble MainOnValue( // returns the OsMA value for the set element
   constint rates_total, // the size of the array
   constint prev_calculated, // processed elements of the array
   constint begin, // from where the significant data of the array starts
   constdouble value, // significant elements of the array
   constint index // the element index
   );
//--- the methods to access to data:int BarsRequired(); // Returns the necessary number of bars for drawing the indicatorstring Name() // Returns the name of the indicatorstring FastMethod() // Returns the method of smoothing of the fast line in the form of the text linestring SlowMethod() // Returns the method of smoothing of the slow line in the form of the text linestring SignalMethod() // Returns the method of smoothing of the signal line in the form of the text lineint FastPeriod() // Returns the period of smoothing of the fast lineint SlowPeriod() // Returns the period of smoothing the slow lineint SignalPeriod() // Returns the period smoothing of signal lineint Size(); // Returns the size of the ring buffer

Accessing the calculated data from the ring buffer is as straightforward as working with a typical array. Here’s a quick snippet:

//--- the class with the methods of the indicator calculation OsMA:#include <IncOnRingBuffer\COsMAOnRingBuffer.mqh>
COsMAOnRingBuffer osma;

...

//+------------------------------------------------------------------+//| Custom indicator iteration function//+------------------------------------------------------------------+intOnCalculate(constint rates_total, 
                constint prev_calculated, 
                constint begin, 
                constdouble &price[]) 
  {
//--- the calculation of the indicator based on a price time series:
   osma.MainOnArray(rates_total, prev_calculated, price);

...

//--- use the data from the "osma" ring buffers,// copy the data to the indicator buffer:
   for(int i=start;i<rates_total;i++)
      OsMABuffer[i]=osma[rates_total-1-i]; // indicator histogram//--- return value of prev_calculated for next call:
   return(rates_total);
  }

Just a heads up: indexing in the ring buffer works just like in time series.

Examples

  1. The Test_OsMA_OnArrayRB.mq5 file demonstrates the OsMA calculation based on the price time series using the MainOnArray() method.
  2. The Test_OsMA_OnValueRB.mq5 file showcases the MainOnValue() method. First, the OsMA indicator is calculated and displayed. Then, based on this ring buffer, another OsMA indicator is drawn.


The outcome of the Test_OsMA_OnArrayRB.mq5 with a ring buffer size of 256 elements.



The outcome of the Test_OsMA_OnValueRB.mq5 with a ring buffer size of 256 elements.

 

Special thanks to the developments of MetaQuotes Software Corp., Integer, and GODZILLA for their contributions.

List
Comments 0