//+------------------------------------------------------------------+
//|                                                     i-g-cci2.mq5 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_color1 DodgerBlue
#property indicator_color2 Lavender
#property indicator_color3 DeepPink
#property indicator_level1     0.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT


input int InpKPeriod=1; // K Period
input int InpDPeriod=3; // D Period
input int InpSlowing=11; // Slowing
input double CCI    = 34;
input double c1     = 0.5;
input double c2     = 0.5;
input double c3     = 0.5;
input int    p      = 6;
input int MaPeriod  = 61; 
input ENUM_MA_METHOD MaMethod = MODE_SMA; 
input int    period  = 21;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double CCI1, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, max, min;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   IndicatorSetString(INDICATOR_SHORTNAME, "igcci2");
   SetIndexBuffer(0, ExtMapBuffer1, INDICATOR_DATA);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(1, ExtMapBuffer2, INDICATOR_DATA);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(2, ExtMapBuffer3, INDICATOR_DATA);
   SetIndexStyle(2, DRAW_LINE);

   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // No action needed
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
{
   if (rates_total <= 0)
      return 0;

   int limit = rates_total - prev_calculated;

   for (int pos = rates_total - limit; pos < rates_total; pos++)
   {
      double dResult1 = 0.0;
      double a[];

      ArrayInitialize(a, EMPTY_VALUE);
      ArrayResize(a, 0);

      for (int i = 0; i < p; i++)
      {
         ArrayResize(a, ArraySize(a) + 1);
         a[ArraySize(a) - 1] =
            iCCI(_Symbol, 0, CCI, PRICE_TYPICAL, pos + i) * c1 +
            iCCI(_Symbol, 0, CCI, PRICE_TYPICAL, pos + i - 1) * c2 -
            iCCI(_Symbol, 0, CCI, PRICE_TYPICAL, pos + i) * c3 +
            iCCI(_Symbol, 0, CCI, PRICE_TYPICAL, pos + i - 2) * c2 -
            iCCI(_Symbol, 0, CCI, PRICE_TYPICAL, pos + i - 1) * c3 +
            iCCI(_Symbol, 0, CCI, PRICE_TYPICAL, pos + i - 3) * c2 -
            iCCI(_Symbol, 0, CCI, PRICE_TYPICAL, pos + i - 2) * c3 +
            iCCI(_Symbol, 0, CCI, PRICE_TYPICAL, pos + i - 4) * c2 -
            iCCI(_Symbol, 0, CCI, PRICE_TYPICAL, pos + i - 3) * c3 +
            iCCI(_Symbol, 0, CCI, PRICE_TYPICAL, pos + i - 5) * c2 -
            iCCI(_Symbol, 0, CCI, PRICE_TYPICAL, pos + i - 4) * c3;
      }

      double a_max = ArrayMaximum(a);
      double a_min = ArrayMinimum(a);
      max = a_max;
      min = a_min;
      dResult1 = (max + min);
      ExtMapBuffer1[pos] = dResult1;

      ExtMapBuffer3[pos] = iMAOnArray(ExtMapBuffer1, rates_total, MaPeriod, 0, MaMethod, pos);
   }

   for (int j = 0; j < rates_total; j++)
   {
      if (j < prev_calculated)
         ExtMapBuffer2[j] = EMPTY_VALUE;
      else
         ExtMapBuffer2[j] = iCustom(_Symbol, 0, "Stochastic", InpKPeriod, InpDPeriod, InpSlowing, 0, j);
   }

   return(rates_total);
}
