McClellan Oscillator (smoother)(dsl)

Author: © mladen, 2019
Price Data Components
Series array that contains close prices for each barSeries array that contains open prices of each bar
0 Views
0 Downloads
0 Favorites
McClellan Oscillator (smoother)(dsl)
ÿþ//------------------------------------------------------------------

#property copyright   "© mladen, 2019"

#property link        "mladenfx@gmail.com"

#property description "McClellan Oscillator using \"smoother\" with dsl lines"

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

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   3

#property indicator_label1  "Signal line up"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrDarkGray

#property indicator_label2  "Signal line down"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrDarkGray

#property indicator_label3  "McClellan Oscillator"

#property indicator_type3   DRAW_COLOR_LINE

#property indicator_color3  clrSilver,clrMediumSeaGreen,clrOrangeRed

#property indicator_width3  2



//

//

//



input string   inpBaseCurrency     = "USD"; // Base currency

input int      inpFastPeriod       =  19;   // Fast smoother period

input int      inpSlowPeriod       =  39;   // Slow smoother period

input double   inpPeriodMultiplier =  1;    // Periods multiplier

input int      inpLevels           = 10;    // DSL signal period



//

//---

//



double val[],valc[],dslup[],dsldn[],_alphaDsl;

struct sSymbolInfo

{

   string name;

   int    bars;

   bool   inverse;

};

sSymbolInfo _symbols[]; int _symbolsSize=0;



//------------------------------------------------------------------

//

//------------------------------------------------------------------ 

//

//

//



int OnInit()

{

   //

   //--- indicator buffers mapping

   //

         SetIndexBuffer(0,dslup);

         SetIndexBuffer(1,dsldn);

         SetIndexBuffer(2,val ,INDICATOR_DATA);

         SetIndexBuffer(3,valc,INDICATOR_COLOR_INDEX);



         //

         //

         //

         

            int _totalSymbols = SymbolsTotal(true);

               for(int i=0; i<_totalSymbols; i++)

               {

                  string _symbol  = SymbolName(i,true);

                  int    _foundAt = StringFind(_symbol,inpBaseCurrency);

                  if (_foundAt!=WRONG_VALUE)

                  {

                     _symbolsSize = ArrayResize(_symbols,_symbolsSize+1);

                     _symbols[_symbolsSize-1].name    = _symbol;

                     _symbols[_symbolsSize-1].inverse = (_foundAt>0);

                     _symbols[_symbolsSize-1].bars    = 0;

                  }

               }    

               iSmootherFast.init(inpFastPeriod*inpPeriodMultiplier);

               iSmootherSlow.init(inpSlowPeriod*inpPeriodMultiplier);

               _alphaDsl = 2.0 / (1.0 + (inpLevels>1? inpLevels : 1));

         

   //

   //--- indicator short name assignment

   //

   

   IndicatorSetString(INDICATOR_SHORTNAME,"McClellan "+inpBaseCurrency+" oscillator (of "+(string)_symbolsSize+" symbols) ("+(string)(inpFastPeriod*inpPeriodMultiplier)+","+(string)(inpSlowPeriod*inpPeriodMultiplier)+","+(string)inpLevels+")");

   return (INIT_SUCCEEDED);

}

void OnDeinit(const int reason) { }



//------------------------------------------------------------------

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

{

   for (int i = 0; i<_symbolsSize; i++) _symbols[i].bars = iBars(_symbols[i].name,0);



   //

   //---

   //



   int i=prev_calculated-1; if (i<0) i=0; for (; i<rates_total && !_StopFlag; i++)

   {

      double _up=0,_dn=0,_advancesDeclines=0;

      for (int k=0; k<_symbolsSize; k++)

      {

         int _shift = iBarShift(_symbols[k].name,0,time[i]);

         if (_shift != WRONG_VALUE)

         {

            double _close = iClose(_symbols[k].name,0,_shift);

            double _open  = iOpen(_symbols[k].name,0,_shift);

            if (_symbols[k].inverse)

            {

               if (_close<_open) _up++;

               if (_close>_open) _dn++;

            }

            else

            {

               if (_close>_open) _up++;

               if (_close<_open) _dn++;

            }

         }            

         _advancesDeclines = (_up+_dn!=0) ? (_up-_dn)/(_up+_dn) : 0;

      }

      val[i] = iSmootherFast.calculate(_advancesDeclines,i,rates_total)-iSmootherSlow.calculate(_advancesDeclines,i,rates_total);

      if (val[i]>0)

            { dsldn[i] = (i>0) ? dsldn[i-1] : val[i]; dslup[i] = (i>0) ? dslup[i-1]+_alphaDsl*(val[i]-dslup[i-1]) : val[i]; }

      else  { dslup[i] = (i>0) ? dslup[i-1] : val[i]; dsldn[i] = (i>0) ? dsldn[i-1]+_alphaDsl*(val[i]-dsldn[i-1]) : val[i]; }

      valc[i] = (val[i]>dslup[i]) ? 1 :(val[i]<dsldn[i]) ? 2 : 0;

   }

   return(i);

}



//------------------------------------------------------------------

// Custom functions

//------------------------------------------------------------------

//

//---

//



class CSmoother

{

   private :

      double m_period;

      double m_alpha;

      double m_beta;

      int    m_arraySize;

      struct CSmootherStruct

      {

         double l0;

         double l1;

         double l2;

         double l3;

         double l4;

      };

      CSmootherStruct m_array[];

   public :

      CSmoother() : m_arraySize(-1), m_alpha(1), m_beta(1) {}

     ~CSmoother() {}

   

      //

      //

      //

      

      bool init(double period)

      {

         m_period = (period>1) ? period : 1;

         m_beta   = 0.45 * (m_period-1.0)/(0.45 * (m_period-1)+2.0);

         m_alpha  = m_beta*m_beta*m_beta;

            return(true);

      }

      

      double calculate(double value, int i, int bars)

      {

         if (m_arraySize<bars) { m_arraySize = ArrayResize(m_array,bars+500); if (m_arraySize<bars) return(0); }



         //

         //

         //

                    

         if (i>0)

         {

               m_array[i].l0 = (1.0-m_alpha)*value + m_alpha*m_array[i-1].l0;

               m_array[i].l1 = (value-m_array[i].l0)*(1-m_beta) + m_beta*m_array[i-1].l1;

               m_array[i].l2 =  m_array[i].l0 + m_array[i].l1;

               m_array[i].l3 = (m_array[i].l2 - m_array[i-1].l4)*(1.0-m_alpha)*(1.0-m_alpha) + m_alpha*m_alpha*m_array[i-1].l3;

               m_array[i].l4 =  m_array[i-1].l4 + m_array[i].l3;

         }                    

         else { m_array[i].l0 = m_array[i].l1 = m_array[i].l4 = value; m_array[i].l2 = m_array[i].l3 = 0; }

         return(m_array[i].l4);

      }

};

CSmoother iSmootherFast,iSmootherSlow;

//------------------------------------------------------------------

Comments