//------------------------------------------------------------------
#property copyright "mladen"
#property link      "www.forex-station.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color5  Blue
#property indicator_width1  4
#property indicator_width2  2
#property indicator_width3  4
#property indicator_width4  2
#property indicator_width5  1

//
//
//
//
//

extern int            CCIPeriod       = 233;
extern int            StochPeriod     = 34;
extern int            StochSlowing    =  13;
extern ENUM_STO_PRICE StochPrice      = STO_CLOSECLOSE;
extern int            AtrPeriod       = 30;
extern double         OverSold        = -10;
extern double         OverBought      =  10;
extern color          OverSoldColor   = clrGreen; 
extern color          OverBoughtColor = clrRed; 

//
//
//
//
//

double cci[];
double cciUpa[];
double cciUpb[];
double cciDna[];
double cciDnb[];
double prices[];
double trend[];
double slope[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(8);
      SetIndexBuffer(0,cciUpa); SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,cciUpb); SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,cciDna); SetIndexStyle(2,DRAW_HISTOGRAM);
      SetIndexBuffer(3,cciDnb); SetIndexStyle(3,DRAW_HISTOGRAM);
      SetIndexBuffer(4,cci);
      SetIndexBuffer(5,prices);
      SetIndexBuffer(6,trend);
      SetIndexBuffer(7,slope);
         SetLevelValue(0,OverBought);
         SetLevelValue(1,OverSold);
         
         //
         //
         //
         //
         //
         
   IndicatorShortName(" ATR CCI 2 ("+CCIPeriod+","+StochPeriod+","+StochSlowing+")");
   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);

   //
   //
   //
   //
   //

      for(int i=limit; i>=0; i--)
      {
         prices[i]  = (iStochastic(NULL,0,StochPeriod,1,StochSlowing,MODE_SMA,StochPrice,MODE_MAIN,i)-50)*iATR(NULL,0,AtrPeriod,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];
            slope[i]  = slope[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 (cci[i]>cci[i+1])                      slope[i] =  1;
               if (cci[i]<cci[i+1])                      slope[i] = -1;
               if (trend[i]==1)
                  if (slope[i]==1)
                        cciUpa[i] = cci[i];
                  else  cciUpb[i] = cci[i];
               if (trend[i]==-1)
                  if (slope[i]==-1)
                        cciDna[i] = cci[i];
                  else  cciDnb[i] = cci[i];
      }
      return(0);
}