//+------------------------------------------------------------------+
//|                                                   CCI simple.mq4 |
//+------------------------------------------------------------------+
#property copyright "www,forex-tsd.com"
#property link      "www,forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  PaleVioletRed
#property indicator_width1  2
#property indicator_level1  0
#property indicator_level2  -100
#property indicator_level3  +100

//
//
//
//
//

extern int CCIPeriod  = 25;
extern int CCIPrice   = PRICE_TYPICAL;
extern int WeightMode = 0;

double cci[];
double prices[];

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

int init()
{
   IndicatorBuffers(2);
      SetIndexBuffer(0,cci);
      SetIndexBuffer(1,prices);
         WeightMode = MathMax(MathMin(WeightMode,3),0);
   IndicatorShortName("CCI ("+CCIPeriod+")");
   return(0);
}

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

int start()
{
   int counted_bars=IndicatorCounted();
   int i,k,limit;

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

   //
   //
   //
   //
   //

   for(i=limit; i>=0; i--)
   {
      prices[i] = iMA(NULL,0,1,0,MODE_SMA,CCIPrice,i);
      double weight = 0;
      double avg    = 0; 
      double dev    = 0; 
         switch(WeightMode)
         {
            case 0 :
               for(k=0; k<CCIPeriod; k++) { avg +=         prices[i+k];      }  avg /= (CCIPeriod);
               for(k=0; k<CCIPeriod; k++) { dev += MathAbs(prices[i+k]-avg); }  dev /= (CCIPeriod);
               break;
            case 1 :               
               for(k=0; k<CCIPeriod; k++) { avg +=         prices[i+k]*Volume[i+k]; weight += Volume[i+k]; }  avg /= (weight);
               for(k=0; k<CCIPeriod; k++) { dev += MathAbs(prices[i+k]-avg);                               }  dev /= (CCIPeriod);
               break;
            case 2 :
               for(k=0; k<CCIPeriod; k++) { avg +=                     prices[i+k];                             }  avg /= (CCIPeriod);
               for(k=0; k<CCIPeriod; k++) { dev += Volume[i+k]*MathAbs(prices[i+k]-avg); weight += Volume[i+k]; }  dev /= (weight);
               break;
            case 3 :
               for(k=0; k<CCIPeriod; k++) { avg += Volume[i+k]*        prices[i+k];      weight += Volume[i+k]; }  avg /= (weight);
               for(k=0; k<CCIPeriod; k++) { dev += Volume[i+k]*MathAbs(prices[i+k]-avg);                        }  dev /= (weight);
               break;
         }
         
         //
         //
         //
         //
         //
              
         if (dev!=0)
               cci[i] = (prices[i]-avg)/(0.015*dev);
         else  cci[i] = 0;          
   }
   return(0);
}