AD Custom Smoothing

Author: Copyright © 2021, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
AD Custom Smoothing
ÿþ//+------------------------------------------------------------------+

//|                                          AD Custom Smoothing.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43161 |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43161"

#property description "Accumulation/Distribution Custom Smoothing"

#property version   "1.002"

#include <MovingAverages.mqh>

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot A/D

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrLightSeaGreen

#property indicator_label1  "A/D"

//--- plot A/D Smoothing

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrOrange

#property indicator_label2  "A/D"

//--- input parameters

input group             "A/D"

input ENUM_APPLIED_VOLUME  InpVolumeType  = VOLUME_TICK; // Volume type

input group             "Smoothing"

input ENUM_MA_METHOD       Inp_ma_method  = MODE_SMA;    // Smoothing type

input int                  Inp_ma_period  = 6;           // Smoothing averaging period

//--- indicator buffers

double ADBuffer[];

double ADSmoothingBuffer[];

//---

int       m_weightsum;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

void OnInit()

  {

//--- indicator digits

   IndicatorSetInteger(INDICATOR_DIGITS,0);

//--- indicator short name

   IndicatorSetString(INDICATOR_SHORTNAME,"A/D Custom Smoothing");

//--- indicator buffers mapping

   SetIndexBuffer(0,ADBuffer);

   SetIndexBuffer(1,ADSmoothingBuffer);

//--- set index draw begin

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,1);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,1+Inp_ma_period);

//---

   m_weightsum=0;

  }

//+------------------------------------------------------------------+

//| Accumulation/Distribution                                        |

//+------------------------------------------------------------------+

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[])

  {

//--- check for bars count

   if(rates_total<2)

      return(0); //exit with zero result

//--- get current position

   int pos=prev_calculated-1;

   if(pos<0)

      pos=0;

//--- calculate with appropriate volumes

   if(InpVolumeType==VOLUME_TICK)

      Calculate(rates_total,pos,high,low,close,tick_volume);

   else

      Calculate(rates_total,pos,high,low,close,volume);

//--- smoothing

   switch(Inp_ma_method)

     {

      case  MODE_SMA:

         SimpleMAOnBuffer(rates_total,prev_calculated,0,Inp_ma_period,ADBuffer,ADSmoothingBuffer);

         break;

      case  MODE_EMA:

         ExponentialMAOnBuffer(rates_total,prev_calculated,0,Inp_ma_period,ADBuffer,ADSmoothingBuffer);

         break;

      case  MODE_SMMA:

         SmoothedMAOnBuffer(rates_total,prev_calculated,0,Inp_ma_period,ADBuffer,ADSmoothingBuffer);

         break;

      default: // MODE_LWMA

         LinearWeightedMAOnBuffer(rates_total,prev_calculated,0,Inp_ma_period,ADBuffer,ADSmoothingBuffer,m_weightsum);

         break;

     }

//---

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| Calculating with selected volume                                 |

//+------------------------------------------------------------------+

void Calculate(const int rates_total,const int pos,

               const double &high[],

               const double &low[],

               const double &close[],

               const long &volume[])

  {

//--- main cycle

   for(int i=pos; i<rates_total && !IsStopped(); i++)

     {

      //--- get some data from arrays

      double hi=high[i];

      double lo=low[i];

      double cl=close[i];

      //--- calculate new AD

      double sum=(cl-lo)-(hi-cl);

      if(hi==lo)

         sum=0.0;

      else

         sum=(sum/(hi-lo))*volume[i];

      if(i>0)

         sum+=ADBuffer[i-1];

      ADBuffer[i]=sum;

     }

  }

//+------------------------------------------------------------------+

Comments