High Low Volume Smoothing

Author: Copyright © 2019, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
High Low Volume Smoothing
ÿþ//+------------------------------------------------------------------+

//|                                    High Low Volume Smoothing.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.000"

//---

#include <MovingAverages.mqh>

//---

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_plots   1

//--- plot Force

#property indicator_label1  "HLW"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrRoyalBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- input parameters

input int                  Inp_Number_Bars   = 3;           // Number of bars

input ENUM_MA_METHOD       InpSmoothMethod   = MODE_EMA;    // Smoothing type

input int                  InpSmootPeriod    = 3;           // Smoothing Period (<= 0 -> off)

input ENUM_APPLIED_VOLUME  InpAppliedVolume  = VOLUME_TICK;  // Volumes

//--- indicator buffers

double   HLWBufferSmoothing[];

double   HLWBuffer[];

//---

int      weight;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   if(Inp_Number_Bars<1)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      "0@0<5B@ \"Number of bars\" =5 <>65B 1KBL @025= =C;N!":

                      "Parameter \"Number of bars\" cannot be zero!";

      //--- when testing, we will only output to the log about incorrect input parameters

      Print(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      return(INIT_PARAMETERS_INCORRECT);

     }

//--- indicator buffers mapping

   if(InpSmootPeriod>0)

     {

      SetIndexBuffer(0,HLWBufferSmoothing,INDICATOR_DATA);

      SetIndexBuffer(1,HLWBuffer,INDICATOR_CALCULATIONS);

     }

   else

     {

      SetIndexBuffer(0,HLWBuffer,INDICATOR_DATA);

      SetIndexBuffer(1,HLWBufferSmoothing,INDICATOR_CALCULATIONS);

     }

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- name for DataWindow and indicator subwindow label

   string text="";

   if(InpSmootPeriod>0)

     {

      switch(InpSmoothMethod)

        {

         case MODE_EMA:

            text=" EMA("+IntegerToString(InpSmootPeriod)+")";

            break;

         case MODE_SMMA:

            text=" SMMA("+IntegerToString(InpSmootPeriod)+")";

            break;

         case MODE_LWMA:

            text=" LWMA("+IntegerToString(InpSmootPeriod)+")";

            break;

         default:

            text=" SMA("+IntegerToString(InpSmootPeriod)+")";

        }

     }

   IndicatorSetString(INDICATOR_SHORTNAME,"HLW"+"("+IntegerToString(Inp_Number_Bars)+")"+text);

//---- sets drawing line empty value

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

//---

   return(INIT_SUCCEEDED);

  }

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

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

  {

//--- check for bars count

   if(rates_total<Inp_Number_Bars-1)

      return(0);// not enough bars for calculation

//--- main loop

   int i=0,limit=0;

//--- first calculation or number of bars was changed

   if(prev_calculated==0)// first calculation

     {

      ArrayInitialize(HLWBuffer,0.0);

      limit=Inp_Number_Bars;

      //---

      double firstValue=0.0;

      for(i=0; i<limit; i++)

         HLWBuffer[i]=0.0;

     }

   else

      limit=prev_calculated-1;

//--- main loop

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

     {

      double firstValue=0.0;

      for(int j=i-Inp_Number_Bars+1; j<=i; j++)

        {

         double vol=(InpAppliedVolume==VOLUME_TICK)?tick_volume[j]:volume[j];

         firstValue+=(high[j]-low[j])*vol;

        }

      HLWBuffer[i]=firstValue;

     }

//---

   if(InpSmootPeriod>0)

     {

      switch(InpSmoothMethod)

        {

         case MODE_EMA:

            ExponentialMAOnBuffer(rates_total,prev_calculated,0,InpSmootPeriod,HLWBuffer,HLWBufferSmoothing);

            break;

         case MODE_SMMA:

            SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSmootPeriod,HLWBuffer,HLWBufferSmoothing); // original

            break;

         case MODE_LWMA:

            LinearWeightedMAOnBuffer(rates_total,prev_calculated,0,InpSmootPeriod,HLWBuffer,HLWBufferSmoothing,weight);

            break;

         default:

            SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSmootPeriod,HLWBuffer,HLWBufferSmoothing);

        }

     }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

Comments