//------------------------------------------------------------------
#property copyright ""
#property link      ""
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  White
#property indicator_width1  3
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1  20
#property indicator_level2  80
#property indicator_levelcolor DarkGray
#property strict

//
//
//
//
//

extern ENUM_APPLIED_PRICE Price          = PRICE_CLOSE; // Price
extern double             Level          = 100;         // Level for gradient
extern int                StartFrom      = 6;           // Starting CCI period
extern int                EndWith        = 48;          // Ending CCI period
extern int                BarsToDraw     = 150;         // Total number of bars to draw
extern int                BarsWidth      = 4;           // Bars display width
extern double             Smooth         = 8;           // CCI smoothing
extern double             SmoothPhase    = 0;           // Smoothing phase
extern string             UniqueID       = "Swami CCI"; // Unique ID
extern bool               GenericVisible = true;        // Generic CCI visible?
extern color              ColorUp        = clrLimeGreen;// Color for up
extern color              ColorDown      = clrHotPink;  // Color for down

double cci[];
double step;
double totalSteps;
int    window;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//

int init()
{
   int type = DRAW_NONE; if (GenericVisible) type = DRAW_LINE;
   SetIndexBuffer(0,cci); SetIndexStyle(0,type);
      
         //
         //
         //
         //
         //
         
         StartFrom  = MathMax(StartFrom, 6);
         EndWith    = MathMin(EndWith  ,99);
         totalSteps = EndWith-StartFrom+1.0;
         step       = 100.0/(totalSteps-1);
      IndicatorShortName(UniqueID);
   return(0);
}

//
//
//
//
//

int deinit() { ObjectsDeleteAll(0,UniqueID+":"); return(0); }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int i,counted_bars=IndicatorCounted();
      if (counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         window = WindowFind(UniqueID);

         //
         //
         //
         //
         //

         for (i=limit; i>=0 ; i--)
         {
            cci[i] = 0;         
            for (int k=StartFrom; k<EndWith; k++)
            {
               double tcci = (100.0*MathMin(MathMax(iCCI(NULL,0,k,Price,i),-Level),Level)/Level+100.0)/2.0;
               if (i<BarsToDraw)
               {
                     plot((string)k,(k-StartFrom)*step,(k-StartFrom+1)*step,i,i,gradientColor((int)tcci,101,ColorDown,ColorUp),BarsWidth);
               }                        
               cci[i] += tcci;
            }
            cci[i] = iSmooth(cci[i]/totalSteps,Smooth,SmoothPhase,i);
         }
         
   //
   //
   //
   //
   //

   SetIndexDrawBegin(0,Bars-BarsToDraw);
   return(0);
}


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void plot(string namex,double valueA, double valueB, int shiftA, int shiftB, color theColor, int width=0,int style=STYLE_SOLID)
{
   string   name = UniqueID+":"+namex+(string)Time[shiftA];
   
   //
   //
   //
   //
   //
   
   if (ObjectFind(name) == -1) 
   {
       ObjectCreate(name,OBJ_TREND,window,Time[shiftA],valueA,Time[shiftB],valueB);
          ObjectSet(name,OBJPROP_RAY,false);
          ObjectSet(name,OBJPROP_BACK,true);
          ObjectSet(name,OBJPROP_STYLE,style);
          ObjectSet(name,OBJPROP_WIDTH,width);
   }
   ObjectSet(name,OBJPROP_COLOR,theColor);
   ObjectSet(name,OBJPROP_PRICE1,valueA);
   ObjectSet(name,OBJPROP_PRICE2,valueB);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

color gradientColor(int tstep, int _totalSteps, color from, color to)
{
   tstep = MathMax(MathMin(tstep,_totalSteps-1),0);
      color newBlue  = getColor(tstep,_totalSteps,(from & 0XFF0000)>>16,(to & 0XFF0000)>>16)<<16;
      color newGreen = getColor(tstep,_totalSteps,(from & 0X00FF00)>> 8,(to & 0X00FF00)>> 8) <<8;
      color newRed   = getColor(tstep,_totalSteps,(from & 0X0000FF)    ,(to & 0X0000FF)    )    ;
      return(newBlue+newGreen+newRed);
}
color getColor(int stepNo, int _totalSteps, color from, color to)
{
   double _step = (from-to)/(_totalSteps-1.0);
   return((color)MathRound(from-_step*stepNo));
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double wrk[][10];

#define bsmax  5
#define bsmin  6
#define volty  7
#define vsum   8
#define avolty 9

//
//
//
//
//

double iSmooth(double price, double length, double phase, int i, int s=0)
{
   if (length <=1) return(price);
   if (ArrayRange(wrk,0) != Bars) ArrayResize(wrk,Bars);
   
   int r = Bars-i-1; 
      if (r==0) { int k=0; for(; k<7; k++) wrk[r][k+s]=price; for(; k<10; k++) wrk[r][k+s]=0; return(price); }

   //
   //
   //
   //
   //
   
      double len1   = MathMax(MathLog(MathSqrt(0.5*(length-1)))/MathLog(2.0)+2.0,0);
      double pow1   = MathMax(len1-2.0,0.5);
      double del1   = price - wrk[r-1][bsmax+s];
      double del2   = price - wrk[r-1][bsmin+s];
      double div    = 1.0/(10.0+10.0*(MathMin(MathMax(length-10,0),100))/100);
      int    forBar = MathMin(r,10);
	
         wrk[r][volty+s] = 0;
               if(MathAbs(del1) > MathAbs(del2)) wrk[r][volty+s] = MathAbs(del1); 
               if(MathAbs(del1) < MathAbs(del2)) wrk[r][volty+s] = MathAbs(del2); 
         wrk[r][vsum+s] =	wrk[r-1][vsum+s] + (wrk[r][volty+s]-wrk[r-forBar][volty+s])*div;
         
         //
         //
         //
         //
         //
   
         wrk[r][avolty+s] = wrk[r-1][avolty+s]+(2.0/(MathMax(4.0*length,30)+1.0))*(wrk[r][vsum+s]-wrk[r-1][avolty+s]);
               double dVolty = (wrk[r][avolty+s] > 0) ? wrk[r][volty+s]/wrk[r][avolty+s] :0;   
	               if (dVolty > MathPow(len1,1.0/pow1)) dVolty = MathPow(len1,1.0/pow1);
                  if (dVolty < 1)                      dVolty = 1.0;

      //
      //
      //
      //
      //
	        
   	double pow2 = MathPow(dVolty, pow1);
      double len2 = MathSqrt(0.5*(length-1))*len1;
      double Kv   = MathPow(len2/(len2+1), MathSqrt(pow2));

         if (del1 > 0) wrk[r][bsmax+s] = price; else wrk[r][bsmax+s] = price - Kv*del1;
         if (del2 < 0) wrk[r][bsmin+s] = price; else wrk[r][bsmin+s] = price - Kv*del2;
	
   //
   //
   //
   //
   //
      
      double R     = MathMax(MathMin(phase,100),-100)/100.0 + 1.5;
      double beta  = 0.45*(length-1)/(0.45*(length-1)+2);
      double alpha = MathPow(beta,pow2);

         wrk[r][0+s] = price + alpha*(wrk[r-1][0+s]-price);
         wrk[r][1+s] = (price - wrk[r][0+s])*(1-beta) + beta*wrk[r-1][1+s];
         wrk[r][2+s] = (wrk[r][0+s] + R*wrk[r][1+s]);
         wrk[r][3+s] = (wrk[r][2+s] - wrk[r-1][4+s])*MathPow((1-alpha),2) + MathPow(alpha,2)*wrk[r-1][3+s];
         wrk[r][4+s] = (wrk[r-1][4+s] + wrk[r][3+s]); 

   //
   //
   //
   //
   //

   return(wrk[r][4+s]);
}