
#property copyright "Copyright © 2008, 2hrfx.com"
#property link      "http://www.2hrfx.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 ForestGreen
#property indicator_color3 Red

extern ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT;
extern ENUM_APPLIED_PRICE Price = 0;
extern int Length = 20;
extern int Displace = 0;
extern int Filter = 0;
extern int Color = 1;
extern int ColorBarBack = 1;
extern double Deviation = 0.0;
double MABuffer[];
double UpBuffer[];
double DnBuffer[];
double trend[];
double alfa[];
int i;
int Phase;
int Len;
int Cycle = 4;
double Coeff;
double beta;
double t;
double sum;
double Weight;
double g;
double pi = 3.1415926535;
string indicatorFileName;
bool   returnBars;

int init() {
   IndicatorBuffers(4);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, MABuffer);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, UpBuffer);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(2, DnBuffer);
   SetIndexBuffer(3, trend);
   IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS));
   string short_name = "TCCI(" + Length + ")";
   IndicatorShortName(short_name);
   SetIndexLabel(0, "TCCI");
   SetIndexLabel(1, "Up");
   SetIndexLabel(2, "Dn");
   SetIndexShift(0, Displace);
   SetIndexShift(1, Displace);
   SetIndexShift(2, Displace);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexEmptyValue(2, EMPTY_VALUE);
   SetIndexDrawBegin(0, Length * Cycle + Length);
   SetIndexDrawBegin(1, Length * Cycle + Length);
   SetIndexDrawBegin(2, Length * Cycle + Length);
   Coeff = 3.0 * pi;
   Phase = Length - 1;
   Len = Length * Cycle + Phase;
   ArrayResize(alfa, Len);
   Weight = 0;
   for (i = 0; i < Len - 1; i++) {
      if (i <= Phase - 1) t = 1.0 * i / (Phase - 1);
      else t = (i - Phase + 1) * (2.0 * Cycle - 1.0) / (Cycle * Length - 1.0) + 1.0;
      beta = MathCos(pi * t);
      g = 1.0 / (Coeff * t + 1.0);
      if (t <= 0.5) g = 1;
      alfa[i] = g * beta;
      Weight += alfa[i];
   }
      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame == -99;
      TimeFrame         = MathMax(TimeFrame,_Period);
   return (0);
}

int start() {
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
            int limit = MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { MABuffer[0] = limit+1; return(0); }
           if (TimeFrame!=Period())
           {
               limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
               for(i=limit; i>=0; i--)
               {
                  int y = iBarShift(NULL,TimeFrame,Time[i]);               
                     MABuffer[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Price,Length,Displace,Filter,Color,ColorBarBack,Deviation,0,y);
                     UpBuffer[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Price,Length,Displace,Filter,Color,ColorBarBack,Deviation,1,y);
                     DnBuffer[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Price,Length,Displace,Filter,Color,ColorBarBack,Deviation,2,y);
               }
               return(0);
           }
    
   
      for (i = 1; i < Length * Cycle + Length; i++) {
         MABuffer[Bars - i] = 0;
         UpBuffer[Bars - i] = 0;
         DnBuffer[Bars - i] = 0;
      }
   for (int shift = limit; shift >= 0; shift--) {
      sum = 0;
      double price = 0;
      for (i = 0; i <= Len - 1; i++) {
         if (Price == 0) price = Close[shift + i];
         else {
            if (Price == 1) price = Open[shift + i];
            else {
               if (Price == 2) price = High[shift + i];
               else {
                  if (Price == 3) price = Low[shift + i];
                  else {
                     if (Price == 4) price = (High[shift + i] + (Low[shift + i])) / 2.0;
                     else {
                        if (Price == 5) price = (High[shift + i] + (Low[shift + i]) + (Close[shift + i])) / 3.0;
                        else
                           if (Price == 6) price = (High[shift + i] + (Low[shift + i]) + 2.0 * (Close[shift + i])) / 4.0;
                     }
                  }
               }
            }
         }
         sum += alfa[i] * price;
      }
      if (Weight > 0.0) MABuffer[shift] = (Deviation / 100.0 + 1.0) * sum / Weight;
      if (Filter > 0)
         if (MathAbs(MABuffer[shift] - (MABuffer[shift + 1])) < Filter * Point) MABuffer[shift] = MABuffer[shift + 1];

      if (Color > 0) {
         trend[shift] = trend[shift + 1];
         if (MABuffer[shift] - (MABuffer[shift + 1]) > Filter * Point) trend[shift] = 1;
         if (MABuffer[shift + 1] - MABuffer[shift] > Filter * Point) trend[shift] = -1;
         if (trend[shift] > 0.0) {
            UpBuffer[shift] = MABuffer[shift];
            if (trend[shift + ColorBarBack] < 0.0) UpBuffer[shift + ColorBarBack] = MABuffer[shift + ColorBarBack];
            DnBuffer[shift] = EMPTY_VALUE;
         }
         if (trend[shift] < 0.0) {
            DnBuffer[shift] = MABuffer[shift];
            if (trend[shift + ColorBarBack] > 0.0) DnBuffer[shift + ColorBarBack] = MABuffer[shift + ColorBarBack];
            UpBuffer[shift] = EMPTY_VALUE;
         }
      }
   }
   return (0);
}