//+------------------------------------------------------------------+
//|                                           FX Sniper's T3 CCI.mq4 |
//|                                                        FX Sniper |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "FX Sniper:  T3-CCI :-)"
#property link      "http://dunno.com  :-)/"

#property indicator_separate_window
#property indicator_buffers 9
#property indicator_color1   Blue
#property  indicator_color2  Green
#property  indicator_color3  Red
#property  indicator_color4  PaleGreen
#property  indicator_color5  HotPink
#property  indicator_color6  LimeGreen
#property  indicator_color7  Pink
#property  indicator_color8  LimeGreen
#property  indicator_color9  Pink
#property  indicator_width8  1
#property  indicator_width9  1

//----
extern int CCI_Period = 14;
extern int T3_Period = 5;
extern double b = 0.618;

extern double level_1 = 100;
extern double level_2 = 200;
extern double exit_level = 0;
//----
double e1, e2, e3, e4, e5, e6;
double c1, c2, c3, c4;
double n, w1, w2, b2, b3;
double cci[];
double cciHup[];
double cciHdn[];
double arr_up_1[];
double arr_down_1[];
double arr_up_2[];
double arr_down_2[];
double arrow_up[];
double arrow_down[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators setting
    SetIndexBuffer(0, cci);
    SetIndexBuffer(1, cciHup);
    SetIndexBuffer(2, cciHdn);
//----
    SetIndexStyle(0, DRAW_LINE);
    SetIndexStyle(1, DRAW_HISTOGRAM);
    SetIndexStyle(2, DRAW_HISTOGRAM); 
//----        
    IndicatorShortName("FXST3CCI(" + CCI_Period + ", " + T3_Period + ")");
    SetIndexLabel(0, "FXST3CCI");     
   SetIndexLabel(1, NULL);
   SetIndexLabel(2, NULL); 
     
     
   
   SetIndexBuffer(3,arr_up_1);
   SetIndexStyle(3,DRAW_ARROW);  
   SetIndexArrow(3, 233);  
   SetIndexLabel(3,"bullish");
   
   SetIndexBuffer(4,arr_down_1);
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexArrow(4, 234);  
   SetIndexLabel(4,"bearish");
  
   SetIndexBuffer(5,arr_up_2);
   SetIndexStyle(5,DRAW_ARROW);  
   SetIndexArrow(5, 233);  
   
   SetIndexBuffer(6,arr_down_2);
   SetIndexStyle(6,DRAW_ARROW);
   SetIndexArrow(6, 234);  
     
   SetIndexBuffer(7,arrow_up);
   SetIndexStyle(7,DRAW_ARROW);  
   SetIndexArrow(7, 108);  
   
   SetIndexBuffer(8,arrow_down);
   SetIndexStyle(8,DRAW_ARROW);
   SetIndexArrow(8, 108);  
    
    SetLevelValue(0, level_1);
    SetLevelValue(1, -level_1);
    SetLevelValue(2, level_2);
    SetLevelValue(3, -level_2);
    SetLevelStyle(0,0,Yellow);     
     
     
//---- variable reset
    b2 = b*b;
    b3 = b2*b;
    c1 = -b3;
    c2 = (3*(b2 + b3));
    c3 = -3*(2*b2 + b + b3);
    c4 = (1 + 3*b + b3 + 3*b2);
    n = T3_Period;
//----
    if(n < 1) 
        n = 1;
    n = 1 + 0.5*(n - 1);
    w1 = 2 / (n + 1);
    w2 = 1 - w1;    
    

    
    
//----
    return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars = IndicatorCounted();
//---- check for possible errors
   if(counted_bars < 0) 
       return(-1);
//---- last counted bar will be recounted
   if(counted_bars > 0)   counted_bars--;
   limit = Bars - counted_bars;  
//---- indicator calculation
   for(int i = limit; i >= 0; i--)
     {   
       cci[i] = iCCI(NULL, 0, CCI_Period, PRICE_TYPICAL, i);
       e1 = w1*cci[i] + w2*e1;
       e2 = w1*e1 + w2*e2;
       e3 = w1*e2 + w2*e3;
       e4 = w1*e3 + w2*e4;
       e5 = w1*e4 + w2*e5;
       e6 = w1*e5 + w2*e6;    
       cci[i] = c1*e6 + c2*e5 + c3*e4 + c4*e3;  
       //----
       if(cci[i] >= 0)           cciHup[i] = cci[i];
       else                      cciHup[i] = 0;          //----
       if(cci[i] < 0 )           cciHdn[i] = cci[i];
       else                      cciHdn[i] = 0; 
       
       
      if( cci[i+1] < level_1 && cci[i] > level_1 ) arr_up_1[i] = level_1;
      if( cci[i+1] < level_2 && cci[i] > level_2 ) arr_up_2[i] = level_2;
   
      if( cci[i+1] > -level_1 && cci[i] < -level_1 ) arr_down_2[i] = -level_1;
      if( cci[i+1] > -level_2 && cci[i] < -level_2 ) arr_down_2[i] = -level_2;
       
      if( cci[i+1] > exit_level && cci[i] < exit_level ) arrow_down[i] = exit_level;
      if( cci[i+1] < -exit_level && cci[i] > -exit_level  ) arrow_up[i] = -exit_level;
       
     }   
//----
   return(0);
  }
//+------------------------------------------------------------------+