Home Technical Indicator Post

Mastering the ADX Wilder Indicator with the Ring Buffer in MetaTrader 5

Attachments
1356.zip (10.07 KB, Download 0 times)

Description

If you’re looking to enhance your trading toolkit, the CADXWOnRingBuffer class is a fantastic resource for calculating the Average Directional Movement Index Wilder (commonly referred to as ADX Wilder) using the ring buffer algorithm. This method can significantly streamline your trading strategies.

Class Declaration

class CADXWOnRingBuffer

File Inclusion

#include <IncOnRingBuffer\CADXWOnRingBuffer.mqh>

Make sure to place the CADXWOnRingBuffer.mqh file in the IncOnRingBuffer folder within your MQL5\\Include\ directory. Two sample files that demonstrate the use of this class are included in the description. Additionally, the ring buffer class and the Moving Average class must also reside in this folder.

Class Methods Overview

//--- initialization method:
bool Init(  // returns false on error, true on success
   int ma_period = 14, // period for Moving Average smoothing
   ENUM_MA_METHOD ma_method = MODE_SMMA, // smoothing method
   int size_buffer = 256, // size of the ring buffer
   bool as_series = false // true if time series, false for standard indexing
);
//--- calculation method 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 the last call
   const double &high[], // high prices array
   const double &low[], // low prices array
   const double &close[] // close prices array
);
//--- calculation method based on individual series elements:
double MainOnValue( // returns the ADXW value for a specific element
   const int rates_total, // size of the array
   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 number of bars needed to draw the indicator
string NameADXW(); // returns the indicator name
string NameNDI(); // returns the name of the negative directional movement line
string NamePDI(); // returns the name of the positive directional movement line
string MAMethod(); // returns the smoothing method as a text line
int MAPeriod(); // returns the smoothing period
int Size(); // returns the size of the ring buffer

You can retrieve the calculated data from the ring buffer just like from a regular array. Here’s an example:

#include <IncOnRingBuffer\CADXWOnRingBuffer.mqh>
CADXWOnRingBuffer adxw;

...

//+------------------------------------------------------------------+
//| 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[])
{
//--- calculation of the indicator:
   adxw.MainOnArray(rates_total, prev_calculated, high, low, close);

   ...
   
//--- copy the data from the "adxw" ring buffers to the indicator:
   for(int i=start; i<rates_total; i++)
   {
       ADXW_Buffer[i] = adxw[rates_total - 1 - i]; // average directional movement index Wilder
       PDI_Buffer[i]  = adxw.pdi[rates_total - 1 - i]; // positive directional index
       NDI_Buffer[i]  = adxw.ndi[rates_total - 1 - i]; // negative directional index
   }

   ...
}

Keep in mind that indexing in the ring buffer operates the same way as in time series.

Example Applications

  1. The Test_ADXW_OnArrayRB.mq5 file illustrates how to calculate the indicator based on price time series. It shows the application of the MainOnArray() method.
  2. The Test_ADXW_OnValueRB.mq5 file demonstrates the use of the MainOnValue() method. It first calculates and displays the ADXW indicator, then computes three lines of the ADXW indicator based on the ring buffer.


Output of the Test_ADXW_OnArrayRB.mq5 with a ring buffer size of 256 elements



Output of the Test_ADXW_OnValueRB.mq5 with a ring buffer size of 256 elements

 

This guide incorporates developments from MetaQuotes Software Corp., Integer, and GODZILLA.

Related Posts

Comments (0)