//+------------------------------------------------------------------+
//|                                                          CCI.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "Commodity Channel Index"
#property strict

#property indicator_separate_window
#property indicator_buffers    1
#property indicator_color1     LightSeaGreen
#property indicator_level1    -100.0
#property indicator_level2     100.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT
//--- input parameter
input int InpCCIPeriod1=8; // CCI Period
input int InpCCIPeriod2=13; // CCI Period
input int InpCCIPeriod3=21; // CCI Period
input ENUM_APPLIED_PRICE Price=0; // Price
//--- buffers
double ExtCCIBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
//--- 2 additional buffers are used for counting.
   IndicatorBuffers(1);
//--- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtCCIBuffer);
//--- check for input parameter
   if(InpCCIPeriod1<=1 || InpCCIPeriod2<=1 || InpCCIPeriod3<=1)
     {
      Print("Wrong input parameter CCI Period");
      return(INIT_FAILED);
     }
//---
   SetIndexDrawBegin(0,InpCCIPeriod3);
//--- name for DataWindow and indicator subwindow label
   short_name="CCI("+IntegerToString(InpCCIPeriod1)+", " +IntegerToString(InpCCIPeriod2)+ ", " +IntegerToString(InpCCIPeriod3)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Commodity Channel Index                                          |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int    i=0;
   while(i<rates_total-InpCCIPeriod3)
     {
         ExtCCIBuffer[i]=(iCCI(Symbol(),0,InpCCIPeriod1,Price,i)+iCCI(Symbol(),0,InpCCIPeriod2,Price,i)+iCCI(Symbol(),0,InpCCIPeriod3,Price,i))/3;
      i++;
     }
//---
   return(rates_total);
  }
//+------------------------------------------------------------------+
