//------------------------------------------------------------------
//
//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 2
#property strict

//
//
//
//
//

enum enDisplayType
{
   dis_lines, // Display as colored lines
   dis_histo, // Display as colored histogram with colored lines
   dis_binary // Display as colored simple 0-1 histogram
};
extern int                RSIPeriod    = 50;          // Rsi period
extern ENUM_APPLIED_PRICE RSIPrice     = PRICE_CLOSE; // Price
extern int                CCIPeriod    = 50;          // CCI period
extern double             OverSold     = -100;        // Oversold level
extern double             OverBought   =  100;        // Overbought level
extern double             SmoothPeriod = 5;            // Smoothing period
extern color              ColorUp      = clrLimeGreen; // Color up
extern color              ColorDown    = clrOrangeRed; // Color down
extern enDisplayType      DisplayAs    = dis_lines;    // Display as :

double cci[];
double ccida[];
double ccidb[],histbu[],histbd[];
double slope[],rsi[];

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

int init()
{
   IndicatorBuffers(7);
   int lstyle = DRAW_LINE;      if (DisplayAs==dis_binary) lstyle = DRAW_NONE;
   int bstyle = DRAW_HISTOGRAM; if (DisplayAs==dis_lines)  bstyle = DRAW_NONE;
   IndicatorBuffers(8);
   SetIndexBuffer(0,histbu); SetIndexStyle(0,bstyle,EMPTY,EMPTY,ColorUp);
   SetIndexBuffer(1,histbd); SetIndexStyle(1,bstyle,EMPTY,EMPTY,ColorDown);
   SetIndexBuffer(2,cci);    SetIndexStyle(2,lstyle,EMPTY,EMPTY,ColorUp);
   SetIndexBuffer(3,ccida);  SetIndexStyle(3,lstyle,EMPTY,EMPTY,ColorDown);
   SetIndexBuffer(4,ccidb);  SetIndexStyle(4,lstyle,EMPTY,EMPTY,ColorDown); 
   SetIndexBuffer(5,rsi);    SetIndexStyle(5,lstyle);
   SetIndexBuffer(6,slope); 
      
         SetLevelValue(0,OverBought);
         SetLevelValue(1,OverSold);
         SetLevelValue(2,0);
   IndicatorShortName("CCI ("+(string)CCIPeriod+") of ("+(string)RSIPeriod+") rsi");
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit=MathMin(Bars,Bars-1);

   //
   //
   //
   //
   //

   if (slope[limit]==-1) CleanPoint(limit,ccida,ccidb);
   for(int i=limit; i>=0; i--) rsi[i] = iSsm(iRSI(NULL,0,RSIPeriod,RSIPrice,i),SmoothPeriod,i);
   for(int i=limit; i>=0; i--)
   {
      cci[i]   = iCCIOnArray(rsi,0,CCIPeriod,i);
      ccida[i] = EMPTY_VALUE;
      ccidb[i] = EMPTY_VALUE;
      if (i<Bars-1) slope[i] = slope[i+1];
            if (DisplayAs==dis_lines)
            {
               if (i<Bars-1 && cci[i]<cci[i+1]) slope[i] = -1;
               if (i<Bars-1 && cci[i]>cci[i+1]) slope[i] =  1;
            }
            else
            {
               if (cci[i]<0) slope[i] = -1;
               if (cci[i]>0) slope[i] =  1;
            }               
            if (DisplayAs!=dis_binary)
               if (slope[i]==-1) PlotPoint(i,ccida,ccidb,cci);
            if (DisplayAs==dis_binary)
            {
               if (slope[i]== 1) { histbu[i] = 1; histbd[i] = 0; }
               if (slope[i]==-1) { histbd[i] = 1; histbu[i] = 0; }
               cci[i] = 0;
            }
            if (DisplayAs==dis_histo)
            {
               if (slope[i]== 1) { histbu[i] = cci[i]; histbd[i] = 0; }
               if (slope[i]==-1) { histbd[i] = cci[i]; histbu[i] = 0; }
            }
   }
   return(0);
}

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

#define Pi 3.14159265358979323846264338327950288
double workSsm[][2];
#define _tprice  0
#define _ssm     1

double workSsmCoeffs[][4];
#define _speriod 0
#define _sc1    1
#define _sc2    2
#define _sc3    3

double iSsm(double price, double period, int i, int instanceNo=0)
{
   if (period<=1) return(price);
   if (ArrayRange(workSsm,0) !=Bars)                 ArrayResize(workSsm,Bars);
   if (ArrayRange(workSsmCoeffs,0) < (instanceNo+1)) ArrayResize(workSsmCoeffs,instanceNo+1);
   if (workSsmCoeffs[instanceNo][_speriod] != period)
   {
      workSsmCoeffs[instanceNo][_speriod] = period;
      double a1 = MathExp(-1.414*Pi/period);
      double b1 = 2.0*a1*MathCos(1.414*Pi/period);
         workSsmCoeffs[instanceNo][_sc2] = b1;
         workSsmCoeffs[instanceNo][_sc3] = -a1*a1;
         workSsmCoeffs[instanceNo][_sc1] = 1.0 - workSsmCoeffs[instanceNo][_sc2] - workSsmCoeffs[instanceNo][_sc3];
   }

   //
   //
   //
   //
   //

      int s = instanceNo*2; i=Bars-i-1;
      workSsm[i][s+_ssm]    = price;
      workSsm[i][s+_tprice] = price;
      if (i>1)
      {  
          workSsm[i][s+_ssm] = workSsmCoeffs[instanceNo][_sc1]*(workSsm[i][s+_tprice]+workSsm[i-1][s+_tprice])/2.0 + 
                               workSsmCoeffs[instanceNo][_sc2]*workSsm[i-1][s+_ssm]                                + 
                               workSsmCoeffs[instanceNo][_sc3]*workSsm[i-2][s+_ssm]; }
   return(workSsm[i][s+_ssm]);
}


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

void CleanPoint(int i,double& first[],double& second[])
{
   if (i>Bars-2) return;
   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 (i>Bars-3) return;
   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; }
}