//+------------------------------------------------------------------+
//|                                       Candle Channel wArrows.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+


#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1  LimeGreen
#property indicator_color2  Orange
#property indicator_color3  Orange
#property indicator_color4  LimeGreen
#property indicator_color5  Orange
#property indicator_color6  Orange
#property indicator_color7  Green
#property indicator_color8  Red

#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_width4  2
#property indicator_width5  2
#property indicator_width6  2
#property indicator_width7  1
#property indicator_width8  1
//
//
//
//
//

int    Price       =  2;
int    Price2      =  3;
int    AdaptPeriod =  1;

extern double Length = 5;
extern bool ShowArrows=true;


double ssm[];
double ssmda[];
double ssmdb[];
double slope[];
double ssm2[];
double ssmda2[];
double ssmdb2[];
double slope2[];
double arr_up[];
double arr_dn[];
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(10);
   SetIndexBuffer(0,ssm);
   SetIndexBuffer(1,ssmda);
   SetIndexBuffer(2,ssmdb);
   SetIndexBuffer(3,ssm2);
   SetIndexBuffer(4,ssmda2);
   SetIndexBuffer(5,ssmdb2);

   SetIndexBuffer(6, arr_up);
   SetIndexBuffer(7, arr_dn);  

   if(ShowArrows)
   {
      SetIndexStyle(6, DRAW_ARROW);
      SetIndexArrow(6, 233);
      SetIndexStyle(7, DRAW_ARROW);
      SetIndexArrow(7, 234);
   }
   else
   {
      SetIndexStyle(6, DRAW_NONE);
      SetIndexStyle(7, DRAW_NONE);   
   }   
   SetIndexBuffer(8,slope);
   SetIndexBuffer(9,slope2);
   IndicatorShortName ("super smoother");
   return(0);
}
int deinit() { return(0); }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit = MathMin(Bars-counted_bars,Bars-1);

   int i;
   
   if (slope[limit]==-1) CleanPoint(limit,ssmda,ssmdb);
   for(i=limit; i >= 0; i--)
   {
      double dev = iStdDev(NULL,0,AdaptPeriod,0,MODE_SMA,Price,i);
      double avg = iSma(dev,AdaptPeriod,i,0);
      if (dev!=0) 
          double period = Length*avg/dev;
      else          period = Length; 
      if (period<3) period = 3;
      ssm[i] = iSmooth(iMA(NULL,0,1,0,MODE_SMA,Price,i),period,i,0);
      ssmda[i] = EMPTY_VALUE;
      ssmdb[i] = EMPTY_VALUE;
      slope[i] = slope[i+1];
      if (ssm[i]>ssm[i+1]) slope[i] =  1;
      if (ssm[i]<ssm[i+1]) slope[i] = -1;
      if (slope[i] == -1) PlotPoint(i,ssmda,ssmdb,ssm);
   }   
   
   if (slope2[limit]==-1) CleanPoint(limit,ssmda2,ssmdb2);
   for(i=limit; i >= 0; i--)
   {
      double dev2 = iStdDev(NULL,0,AdaptPeriod,0,MODE_SMA,Price2,i);
      double avg2 = iSma(dev,AdaptPeriod,i,1);
      if (dev2!=0) 
          double period2 = Length*avg2/dev2;
      else          period2 = Length; 
      if (period2<3) period2 = 3;
      ssm2[i] = iSmooth(iMA(NULL,0,1,0,MODE_SMA,Price2,i),period2,i,1);
      ssmda2[i] = EMPTY_VALUE;
      ssmdb2[i] = EMPTY_VALUE;
      slope2[i] = slope2[i+1];
      if (ssm2[i]>ssm2[i+1]) slope2[i] =  1;
      if (ssm2[i]<ssm2[i+1]) slope2[i] = -1;
      if (slope2[i] == -1) PlotPoint(i,ssmda2,ssmdb2,ssm2);
   }   

   for(i=limit-1; i>=1; i--)
   {
      int j=i;
      double var1 = 0;
      double var2 = 0;
      for (j = i; j <= i + 9; j++) var2 += MathAbs(iHigh(Symbol(),0,j) - iHigh(Symbol(),0,j));
      var1 = var2 / 10.0;
      arr_up[i] = arr_dn[i] = EMPTY_VALUE;
      double close1=iClose(Symbol(),0,i);
      if(close1>ssm[i])
      {
         arr_up[i]=ssm2[i] - (var1);
      }
      else if(close1<ssm2[i])
      {
         arr_dn[i]=ssm[i] + (var1);
      }
   }      


   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workSmooth[][10];
double iSmooth(double price,int length,int r, int instanceNo=0)
{
   if (ArrayRange(workSmooth,0)!=Bars) ArrayResize(workSmooth,Bars); instanceNo *= 5; r = Bars-r-1;
 	if(r<=2) { workSmooth[r][instanceNo] = price; workSmooth[r][instanceNo+2] = price; workSmooth[r][instanceNo+4] = price; return(price); }
   
   //
   //
   //
   //
   //
   
	double alpha = 0.45*(length-1.0)/(0.45*(length-1.0)+2.0);
   	  workSmooth[r][instanceNo+0] =  price+alpha*(workSmooth[r-1][instanceNo]-price);
	     workSmooth[r][instanceNo+1] = (price - workSmooth[r][instanceNo])*(1-alpha)+alpha*workSmooth[r-1][instanceNo+1];
	     workSmooth[r][instanceNo+2] =  workSmooth[r][instanceNo+0] + workSmooth[r][instanceNo+1];
	     workSmooth[r][instanceNo+3] = (workSmooth[r][instanceNo+2] - workSmooth[r-1][instanceNo+4])*MathPow(1.0-alpha,2) + MathPow(alpha,2)*workSmooth[r-1][instanceNo+3];
	     workSmooth[r][instanceNo+4] =  workSmooth[r][instanceNo+3] + workSmooth[r-1][instanceNo+4]; 
   return(workSmooth[r][instanceNo+4]);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

double workSma[][4];
double iSma(double price, int period, int r, int instanceNo=0)
{
   if (ArrayRange(workSma,0)!= Bars) ArrayResize(workSma,Bars); instanceNo *= 2; r = Bars-r-1;

   //
   //
   //
   //
   //
      
   workSma[r][instanceNo] = price;
   if (r>=period)
          workSma[r][instanceNo+1] = workSma[r-1][instanceNo+1]+(workSma[r][instanceNo]-workSma[r-period][instanceNo])/period;
   else { workSma[r][instanceNo+1] = 0; for(int k=0; k<period && (r-k)>=0; k++) workSma[r][instanceNo+1] += workSma[r-k][instanceNo];  
          workSma[r][instanceNo+1] /= k; }
   return(workSma[r][instanceNo+1]);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]  = from[i];
         second[i] = EMPTY_VALUE;
      }
}