//------------------------------------------------------------------
#property copyright "mladen"
#property link      "www,forex-tsd.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1  Silver
#property indicator_width1  3
#property indicator_width2  3
#property indicator_width3  3
#property indicator_width4  3
#property indicator_width5  3

//
//
//
//
//

extern int    CCIPeriod       = 14;
extern int    CCIPrice        = PRICE_TYPICAL;
extern int    AvgPeriod       =  5;
extern int    AvgMethod       =  1;
extern double OverSold        = -100;
extern double OverBought      =  100;
extern color  OverSoldColor   = LimeGreen; 
extern color  OverBoughtColor = DarkOrange; 

//
//
//
//
//

double cci[];
double cciUpa[];
double cciUpb[];
double cciDna[];
double cciDnb[];
double prices[];
double trend[];

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

int init()
{
   IndicatorBuffers(7);
      SetIndexBuffer(0,cci);
      SetIndexBuffer(1,cciUpa); SetIndexStyle(1,DRAW_LINE,EMPTY,EMPTY,OverSoldColor);
      SetIndexBuffer(2,cciUpb); SetIndexStyle(2,DRAW_LINE,EMPTY,EMPTY,OverSoldColor);
      SetIndexBuffer(3,cciDna); SetIndexStyle(3,DRAW_LINE,EMPTY,EMPTY,OverBoughtColor);
      SetIndexBuffer(4,cciDnb); SetIndexStyle(4,DRAW_LINE,EMPTY,EMPTY,OverBoughtColor);
      SetIndexBuffer(5,prices);
      SetIndexBuffer(6,trend);
         SetLevelValue(0,OverBought);
         SetLevelValue(1,OverSold);
         
         //
         //
         //
         //
         //
         
         AvgPeriod = MathMax(AvgPeriod,1);
         AvgMethod = MathMax(MathMin(AvgMethod,3),0);
         string name;
            switch (AvgMethod)
            {
               case 0 : name = "SMA";  break;
               case 1 : name = "EMA";  break;
               case 2 : name = "SMMA"; break;
               case 3 : name = "LWMA"; break;
            }
   IndicatorShortName(name+" CCI ("+CCIPeriod+","+AvgPeriod+")");
   return(0);
}

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

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

   //
   //
   //
   //
   //

      if (trend[limit]== 1) CleanPoint(limit,cciUpa,cciUpb);
      if (trend[limit]==-1) CleanPoint(limit,cciDna,cciDnb);
      for(int i=limit; i>=0; i--)
      {
         prices[i]  = iMA(NULL,0,AvgPeriod,0,AvgMethod,CCIPrice,i);
         double avg = 0; for(k=0; k<CCIPeriod; k++) avg +=         prices[i+k];      avg /= CCIPeriod;
         double dev = 0; for(k=0; k<CCIPeriod; k++) dev += MathAbs(prices[i+k]-avg); dev /= CCIPeriod;
            if (dev!=0)
                  cci[i] = (prices[i]-avg)/(0.015*dev);
            else  cci[i] = 0;
         
            //
            //
            //
            //
            //
         
            cciUpa[i] = EMPTY_VALUE;
            cciUpb[i] = EMPTY_VALUE;
            cciDna[i] = EMPTY_VALUE;
            cciDnb[i] = EMPTY_VALUE;
            trend[i] = trend[i+1];
               if (cci[i]>OverBought)                    trend[i] =  1;
               if (cci[i]<OverSold)                      trend[i] = -1;
               if (cci[i]>OverSold && cci[i]<OverBought) trend[i] =  0;
               if (trend[i] ==  1) PlotPoint(i,cciUpa,cciUpb,cci);
               if (trend[i] == -1) PlotPoint(i,cciDna,cciDnb,cci);
      }
      return(0);
}

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

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;
      }
}