//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 Gray
#property indicator_color2 LimeGreen
#property indicator_color3 PaleVioletRed
#property indicator_color4 LimeGreen
#property indicator_color5 PaleVioletRed
#property indicator_color6 DarkGray
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 2
#property indicator_width6 2
#property strict

//
//
//
//
//

extern ENUM_TIMEFRAMES    TimeFrame  = PERIOD_CURRENT;  // Time frame to use
extern int                T3Period   = 27;              // T3 period
extern ENUM_APPLIED_PRICE T3Price    = PRICE_CLOSE;     // Price to use
extern double             T3Hot      = 0.7;             // T3 hot
extern bool               T3Original = false;           // Tim Tillson calculation (true) or Fulks/Matulich calculation (false)
extern double             Treshold   = 10;              // Treshold

//
//
//
//
//

double hma[];
double TrendUP[];
double TrendDN[];
double HistoUP[];
double HistoDN[];
double HistoNN[];

string indicatorFileName;
bool   returnBars;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(6);
   SetIndexBuffer(0,HistoNN); SetIndexLabel(0,"trend");      SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,HistoUP); SetIndexLabel(1,"retrace UP"); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,HistoDN); SetIndexLabel(2,"retrace DN"); SetIndexStyle(2,DRAW_HISTOGRAM);   
   SetIndexBuffer(3,TrendUP); SetIndexLabel(3,"sig UP");     SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,167);
   SetIndexBuffer(4,TrendDN); SetIndexLabel(4,"sig DN");     SetIndexStyle(4,DRAW_ARROW); SetIndexArrow(4,167);
   SetIndexBuffer(5,hma);

      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         returnBars        = TimeFrame==-99;
         TimeFrame         = MathMax(TimeFrame,_Period);
   IndicatorShortName(timeFrameToString(TimeFrame)+" T3 slope ("+(string)T3Period+")");
   return(0);
}
int deinit() { return(0); }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double work[][3];
#define _hma 0
#define _hmo 1
#define _hms 2

int start()
{
   int i,r,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) { HistoNN[0] = MathMin(limit+1,Bars-1); return(0); }

   //
   //
   //
   //
   //

   if (TimeFrame == Period())
   {
      if (ArrayRange(work,0)!=Bars) ArrayResize(work,Bars);
      for(i=limit,r=Bars-i-1; i>=0; i--,r++)
      {
         work[r][_hma] = iT3(iMA(NULL,0,1,0,MODE_SMA,T3Price,i),T3Period,T3Hot,T3Original,i,0); if (r<3) continue;
         work[r][_hmo] = 100.0*(work[r][_hma]-work[r-1][_hma])/work[r-1][_hma];
         work[r][_hms] = (4.0*work[r][_hmo]+3.0*work[r-1][_hmo]+2.0*work[r-2][_hmo]+work[r-3][_hmo])/10.0;
                hma[i] = work[r][_hmo];
                
         //
         //
         //
         //
         //
                  
         double minVal = MathMin(work[r][_hmo],work[r][_hms]);
         double maxVal = MathMax(work[r][_hmo],work[r][_hms]);
         double diffP  = 0; if (minVal != 0) diffP = MathAbs(100 * (maxVal-minVal)/minVal);

         HistoUP[i] = EMPTY_VALUE;
         HistoDN[i] = EMPTY_VALUE;
         HistoNN[i] = EMPTY_VALUE;
         if (i<Bars-1)
         {
            TrendUP[i] = TrendUP[i+1];
            TrendDN[i] = TrendDN[i+1];
               if (work[r][_hmo] > work[r][_hms] && diffP > Treshold) { TrendUP[i] = 0; TrendDN[i] = EMPTY_VALUE; }
               if (work[r][_hmo] < work[r][_hms] && diffP > Treshold) { TrendDN[i] = 0; TrendUP[i] = EMPTY_VALUE; }
               if (work[r][_hmo] > 0 && TrendUP[i]== 0) if(High[i] <= High[i+1]) HistoUP[i] = work[r][_hmo]; else HistoNN[i]= work[r][_hmo];
               if (work[r][_hmo] < 0 && TrendDN[i]== 0) if(Low[i]  >= Low[i+1])  HistoDN[i] = work[r][_hmo]; else HistoNN[i]= work[r][_hmo];
         }               
      }
      return(0);
   }
   
   //
   //
   //
   //
   //
   
   limit = (int)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]);
         HistoNN[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,T3Period,T3Price,T3Hot,T3Original,Treshold,0,y);
         HistoUP[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,T3Period,T3Price,T3Hot,T3Original,Treshold,1,y);
         HistoDN[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,T3Period,T3Price,T3Hot,T3Original,Treshold,2,y);
         TrendUP[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,T3Period,T3Price,T3Hot,T3Original,Treshold,3,y);
         TrendDN[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,T3Period,T3Price,T3Hot,T3Original,Treshold,4,y);
         hma[i]     = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,T3Period,T3Price,T3Hot,T3Original,Treshold,5,y);
   }   
   return(0);
}


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
//

double workT3[][6];
double workT3Coeffs[][6];
#define _period 0
#define _c1     1
#define _c2     2
#define _c3     3
#define _c4     4
#define _alpha  5

//
//
//
//
//

double iT3(double price, double period, double hot, bool original, int i, int instanceNo=0)
{
   if (ArrayRange(workT3,0) !=Bars)                 ArrayResize(workT3,Bars);
   if (ArrayRange(workT3Coeffs,0) < (instanceNo+1)) ArrayResize(workT3Coeffs,instanceNo+1);

   if (workT3Coeffs[instanceNo][_period] != period)
   {
     workT3Coeffs[instanceNo][_period] = period;
        double a = hot;
            workT3Coeffs[instanceNo][_c1] = -a*a*a;
            workT3Coeffs[instanceNo][_c2] = 3*a*a+3*a*a*a;
            workT3Coeffs[instanceNo][_c3] = -6*a*a-3*a-3*a*a*a;
            workT3Coeffs[instanceNo][_c4] = 1+3*a+a*a*a+3*a*a;
            if (original)
                 workT3Coeffs[instanceNo][_alpha] = 2.0/(1.0 + period);
            else workT3Coeffs[instanceNo][_alpha] = 2.0/(2.0 + (period-1.0)/2.0);
   }
   
   //
   //
   //
   //
   //
   
   int buffer = instanceNo*6;
   int r = Bars-i-1;
   if (r == 0)
      {
         workT3[r][0+buffer] = price;
         workT3[r][1+buffer] = price;
         workT3[r][2+buffer] = price;
         workT3[r][3+buffer] = price;
         workT3[r][4+buffer] = price;
         workT3[r][5+buffer] = price;
      }
   else
      {
         workT3[r][0+buffer] = workT3[r-1][0+buffer]+workT3Coeffs[instanceNo][_alpha]*(price              -workT3[r-1][0+buffer]);
         workT3[r][1+buffer] = workT3[r-1][1+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][0+buffer]-workT3[r-1][1+buffer]);
         workT3[r][2+buffer] = workT3[r-1][2+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][1+buffer]-workT3[r-1][2+buffer]);
         workT3[r][3+buffer] = workT3[r-1][3+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][2+buffer]-workT3[r-1][3+buffer]);
         workT3[r][4+buffer] = workT3[r-1][4+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][3+buffer]-workT3[r-1][4+buffer]);
         workT3[r][5+buffer] = workT3[r-1][5+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][4+buffer]-workT3[r-1][5+buffer]);
      }

   //
   //
   //
   //
   //
   
   return(workT3Coeffs[instanceNo][_c1]*workT3[r][5+buffer] + 
          workT3Coeffs[instanceNo][_c2]*workT3[r][4+buffer] + 
          workT3Coeffs[instanceNo][_c3]*workT3[r][3+buffer] + 
          workT3Coeffs[instanceNo][_c4]*workT3[r][2+buffer]);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}