//+------------------------------------------------------------------+
//|                                                          Kase CD |
//|                                                      Kase CD.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "copyleft mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 Goldenrod

//
//
//
//
//

extern int KPeriod = 8;

//
//
//
//
//

double PkGreen[];
double PkRed[];
double Line[];
double RWHL[];
double Pk[];
double PPk[];
double Kcd[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(7);
      SetIndexBuffer(0,PkGreen); SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,PkRed);   SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,Line);
      SetIndexBuffer(3,RWHL);
      SetIndexBuffer(4,Pk);
      SetIndexBuffer(5,PPk);
      SetIndexBuffer(6,Kcd);
            for (int i=0; i < indicator_buffers; i++)
                  SetIndexDrawBegin(i,2*KPeriod+10);
   IndicatorShortName("Kase CD ("+KPeriod+")");
   return(0);
}

//
//
//
//
//

int start()
{
   double square      = MathSqrt(KPeriod);
   int    countedBars = IndicatorCounted();
   int    i,limit;

   if (countedBars<0) return(-1);
   if (countedBars>0) countedBars--;
         limit = Bars-countedBars;
   
   //
   //
   //
   //
   //
   
   for(i = limit; i >=0; i--)
   {
      double range = iATR(NULL,0,KPeriod,i);
         if (range != 0)
               RWHL[i] = ((High[i]-Low[i+KPeriod]) - (High[i+KPeriod]-Low[i]))/(range*square);
         else  RWHL[i] = 0;
         PPk[i] = iLwma(RWHL,3,i);
         Pk[i]  = iSma(PPk,3,i);
         Kcd[i] = Pk[i]-iSma(Pk,8,i);
      
      //
      //
      //
      //
      //
      
      double mn = iSma(Kcd,KPeriod,i);
      double sd = iDeviation(Kcd,KPeriod,i,mn);
      double v1 = MathMax( 2.08,mn+(1.33*sd));
      double v2 = MathMin(-1.92,mn-(1.33*sd));
           
         if(Kcd[i+1] >= 0 && Kcd[i] > 0) Line[i] = v1;
         else
            if(Kcd[i+1] <= 0 && Kcd[i] < 0)
                  Line[i] = v2;
            else  Line[i] = 0;
         if(Kcd[i+1]>Kcd[i]) { PkRed[i] = Kcd[i];      PkGreen[i] = EMPTY_VALUE; }
         else                { PkRed[i] = EMPTY_VALUE; PkGreen[i] = Kcd[i];      }
   }
   
   //
   //
   //
   //
   //
   
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double iDeviation(double& array[], int period, int pos, double dMA)
{
// double dMA  = iSma(array,period,pos);
   double dSum = 0;
      for(int i=0; i<period; i++,pos++) dSum += (array[pos]-dMA)*(array[pos]-dMA);
   return(MathSqrt(dSum/period));
}

//
//
//
//
//

double iSma(double& array[], int period, int pos)
{
   double sum = 0.0;
      for(int i=0; i<period; i++,pos++)  sum += array[pos];
   return(sum/period);     
}

//
//
//
//
//

double iLwma(double& array[], int period, int pos)
{
   double sum = 0.0;
      for(int i=0; i<period; i++,pos++) sum += (period-i)*array[pos];
   return(sum/((period+1)*period*0.5));     
}