//------------------------------------------------------------------
//
//------------------------------------------------------------------
#property copyright "www,forex-tsd.com"
#property link      "www,forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 8

#property indicator_color1  DarkKhaki           //->TrendCCI 
#property indicator_color2  CLR_NONE            //->rsi
#property indicator_color3  MediumSeaGreen      //->CCITrendUp
#property indicator_color4  Red                 //->CCITrendDown
#property indicator_color5  DarkGray            //->CCINoTrend
#property indicator_color6  Gold                //->CCITimeBar
#property indicator_color7  Snow                //->EntryCCI
#property indicator_color8  DarkKhaki           //->ZeroLine
  
#property indicator_level1  350
#property indicator_level2  250
#property indicator_level3  100
#property indicator_level4  0
#property indicator_level5 -100
#property indicator_level6 -250
#property indicator_level7 -350

//
//
//
//
//

extern int    RSIPeriod        = 50;
extern int    RSIPrice         = PRICE_CLOSE;
extern int    EntryCCI_Period  = 6;          
extern int    TrendCCI_Period  = 50;         
extern int    Trend_period     = 2;          
extern double SmoothPeriod     = 5;
extern int    CountBars        = 1000; 
extern bool   Zero_Cross_Alert = true;


extern int     LineSize1       = 2;
extern int     LineSize2       = 3;
extern int     LineSize3       = 2;

double TrendCCI[];  
double rsi[];
   
double CCITrendUp[];
double CCITrendDown[];
double CCINoTrend[];
double CCITimeBar[];
double EntryCCI[];
double ZeroLine[];

int trendUp, trendDown;

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

int init()
{
   IndicatorBuffers(8);
      SetIndexBuffer(0, TrendCCI);
      SetIndexStyle (0, DRAW_LINE, STYLE_SOLID,LineSize1);
      SetIndexLabel (0, "TrendCCI");         
      SetIndexBuffer(1, rsi);
      SetIndexBuffer(2, CCITrendUp);
      SetIndexStyle (2, DRAW_HISTOGRAM, 0,LineSize1);
      SetIndexBuffer(3, CCITrendDown);
      SetIndexStyle (3, DRAW_HISTOGRAM, 0,LineSize1);
      SetIndexBuffer(4, CCINoTrend);
      SetIndexStyle (4, DRAW_HISTOGRAM, 0,LineSize2);
      SetIndexBuffer(5, CCITimeBar);
      SetIndexStyle (5, DRAW_HISTOGRAM, 0,LineSize2);
      SetIndexBuffer(6, EntryCCI);
      SetIndexStyle (6, DRAW_LINE, STYLE_SOLID,LineSize2);
      SetIndexLabel (6, "EntryCCI");      
      SetIndexBuffer(7, ZeroLine);   
      SetIndexStyle (7, DRAW_LINE, STYLE_SOLID,LineSize3);

      
   IndicatorShortName("Double CCI ("+TrendCCI_Period+" / "+EntryCCI_Period+") of ("+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-counted_bars,Bars-1);

   static datetime prevtime = 0;
   //
   //
   //
   //
   //

   for(int i=limit; i>=0; i--) rsi[i] = iRSI(NULL,0,RSIPeriod,RSIPrice,i);
   for(    i=limit; i>=0; i--) TrendCCI[i] = iSsm(iCCIOnArray(rsi,0,TrendCCI_Period,i),SmoothPeriod,i,0);
   for(    i=limit; i>=0; i--) EntryCCI[i] = iSsm(iCCIOnArray(rsi,0,EntryCCI_Period,i),SmoothPeriod,i,1);
//
   for(    i=limit; i>=0; i--) 
   {
      CCINoTrend[i]   = 0;
      CCITrendDown[i] = 0;
      CCITimeBar[i]   = 0;
      CCITrendUp[i]   = 0;
      ZeroLine[i]     = 0;
      
      if(TrendCCI[i] > 0 && TrendCCI[i+1] < 0) 
      {
         if (trendDown >  Trend_period) trendUp = 0;
      }
      if (TrendCCI[i] > 0) 
      {
         if (trendUp <  Trend_period){
            CCINoTrend[i] = TrendCCI[i];
            trendUp++;
         }
         if (trendUp ==  Trend_period) {
            CCITimeBar[i] = TrendCCI[i];
            trendUp++;
         }
         if (trendUp >  Trend_period) {
            CCITrendUp[i] = TrendCCI[i];
         }
      }
      if(TrendCCI[i] < 0 && TrendCCI[i+1] > 0) 
      {
         if (trendUp >  Trend_period) trendDown = 0;
      }
      if (TrendCCI[i] < 0) 
      {
         
         if (trendDown <  Trend_period){
            CCINoTrend[i] = TrendCCI[i];
            trendDown++;
         }
         if (trendDown ==  Trend_period) {
            CCITimeBar[i] = TrendCCI[i];
            trendDown++;
         }
         if (trendDown >  Trend_period) {
            CCITrendDown[i] = TrendCCI[i];
         }
      }
   }   

   if (Zero_Cross_Alert == true) {
      if (prevtime == Time[0]) {
         return(0);
      }
      else {
         if(EntryCCI[0] < 0) {
            if((TrendCCI[0] < 0) && (TrendCCI[1] >= 0)) {
               Alert(Symbol(), " M", Period(), " Trend & Entry CCI Have both crossed below zero");
            }
         }
         else if(EntryCCI[0] > 0) {
            if((TrendCCI[0] > 0) && (TrendCCI[1] <= 0)) {
               Alert(Symbol(), " M", Period(), " Trend & Entry CCI Have both crossed above zero");
            }
         }
         prevtime = Time[0];
      }
   }   
   return(0);
}

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

double workSsm[][4];
#define _price  0
#define _ssm    1

double workSsmCoeffs[][4];
#define _period 0
#define _c1     1
#define _c2     2
#define _c3     3
#define Pi 3.14159265358979323846264338327950288

//
//
//
//
//

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][_period] != period)
   {
      workSsmCoeffs[instanceNo][_period] = period;
      double a1 = MathExp(-1.414*Pi/period);
      double b1 = 2.0*a1*MathCos(1.414*Pi/period);
         workSsmCoeffs[instanceNo][_c2] = b1;
         workSsmCoeffs[instanceNo][_c3] = -a1*a1;
         workSsmCoeffs[instanceNo][_c1] = 1.0 - workSsmCoeffs[instanceNo][_c2] - workSsmCoeffs[instanceNo][_c3];
   }

   //
   //
   //
   //
   //

      i = Bars-i-1;
      int s = instanceNo*2;   
          workSsm[i][s+_price] = price;
          workSsm[i][s+_ssm]   = workSsmCoeffs[instanceNo][_c1]*(workSsm[i][s+_price]+workSsm[i-1][s+_price])/2.0 + 
                                 workSsmCoeffs[instanceNo][_c2]*workSsm[i-1][s+_ssm]                              + 
                                 workSsmCoeffs[instanceNo][_c3]*workSsm[i-2][s+_ssm];
   return(workSsm[i][s+_ssm]);
}