//+------------------------------------------------------------------
//|
//+------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1  clrDimGray
#property indicator_color2  clrDimGray
#property indicator_color3  clrGray
#property indicator_color4  clrDeepSkyBlue
#property indicator_color5  clrSandyBrown
#property indicator_width3  2
#property indicator_width4  2
#property indicator_width5  2
#property indicator_style1  STYLE_DOT
#property indicator_style2  STYLE_DOT

//
//
//
//
//

#import "dynamicZone.dll"
   double dzBuyP(double& sourceArray[],double probabiltyValue, int lookBack, int bars, int i, double precission );
   double dzSellP(double& sourceArray[],double probabiltyValue, int lookBack, int bars, int i, double precission );
#import

//
//
//
//
//


extern ENUM_TIMEFRAMES TimeFrame     = PERIOD_CURRENT;
extern int    TTFbars                =   15;
extern double t3Period               =   14;
extern double t3Hot                  =  0.7;
extern bool   t3Original             = false;
extern int    DzLookBackBars         = 35;
extern double DzStartBuyProbability  = 0.1;
extern double DzStartSellProbability = 0.1;
extern bool   showSignals            = true;
extern bool   alertsOn               = false;
extern bool   alertsOnCurrent        = true;
extern bool   alertsMessage          = true;
extern bool   alertsSound            = false;
extern bool   alertsEmail            = false;
extern bool   Interpolate            = true;

//
//
//
//
//

double ttf[];
double lev[];
double zl[];
double sigu[];
double sigd[];
double trend[];
double trends[];
string indicatorFileName;
bool   returnBars;

//+------------------------------------------------------------------
//|
//+------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(7);
      SetIndexBuffer(0,zl);
      SetIndexBuffer(1,lev);
      SetIndexBuffer(2,ttf);
      SetIndexBuffer(3,sigu); SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,159);
      SetIndexBuffer(4,sigd); SetIndexStyle(4,DRAW_ARROW); SetIndexArrow(4,159);
      SetIndexBuffer(5,trend);
      SetIndexBuffer(6,trends);
   
      //
      //
      //
      //
      //
      
      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame==-99;
      TimeFrame         = MathMax(TimeFrame,_Period);
   IndicatorShortName(timeFrameToString(TimeFrame)+" DZ trend scalp ("+TTFbars+","+t3Period+")");
   return(0);
}

//+------------------------------------------------------------------
//|
//+------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int limit,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { zl[0] = limit+1; return(0); }
         if (TimeFrame != Period())
         {
            limit = MathMax(limit,MathMin(Bars,iCustom(NULL,TimeFrame,indicatorFileName,-99,4,0)*TimeFrame/Period()));
            for(int i=limit; i>=0; i--)
            {
               int y = iBarShift(NULL,TimeFrame,Time[i]);
               zl[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,TTFbars,t3Period,t3Hot,t3Original,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,showSignals,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,0,y);
               lev[i]  = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,TTFbars,t3Period,t3Hot,t3Original,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,showSignals,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,1,y);
               ttf[i]  = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,TTFbars,t3Period,t3Hot,t3Original,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,showSignals,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,2,y);
               sigu[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,TTFbars,t3Period,t3Hot,t3Original,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,showSignals,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,3,y);
               sigd[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,TTFbars,t3Period,t3Hot,t3Original,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,showSignals,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,4,y);

               //
               //
               //
               //
               //
      
                  if (y==iBarShift(NULL,TimeFrame,Time[i-1]) || !Interpolate) continue;

               //
               //
               //
               //
               //

               datetime time = iTime(NULL,TimeFrame,y);
                  for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
                  for(int j = 1; j < n; j++)
                  {
                     zl[i+j]  = zl[i]  + (zl[i+n] -zl[i] )*j/n;
                     lev[i+j] = lev[i] + (lev[i+n]-lev[i])*j/n;
                     ttf[i+j] = ttf[i] + (ttf[i+n]-ttf[i])*j/n;
                     if (sigu[i]!=EMPTY_VALUE) sigu[i+j] = lev[i+j];
                     if (sigd[i]!=EMPTY_VALUE) sigd[i+j] = lev[i+j];
                  }
            }
            return(0);
         }            

   //
   //
   //
   //
   //
   
   for(i=limit; i >= 0; i--)
   {
      double HighestHighRecent = High[i]; double HighestHighOlder = High[iHighest(NULL,0,MODE_HIGH,TTFbars,i+1)];
      double LowestLowRecent   = Low[i];  double LowestLowOlder   = Low[iLowest(NULL,0,MODE_LOW,TTFbars,i+1)];
      
         double BuyPower  = HighestHighRecent-LowestLowOlder;
         double SellPower = HighestHighOlder -LowestLowRecent;
               ttf[i] = iT3((BuyPower-SellPower)/(0.5*(BuyPower+SellPower))*100.0,t3Period,t3Hot,t3Original,i);
               zl[i]  = dzSellP(ttf,0.5,DzLookBackBars,Bars,i,0.0001);
      
      //
      //
      //
      //
      //
      
      trend[i] = trend[i+1];
         if (ttf[i] > zl[i]) trend[i] =  1;
         if (ttf[i] < zl[i]) trend[i] = -1;
         if (trend[i]== 1) lev[i] = dzSellP(ttf,DzStartBuyProbability,DzLookBackBars,Bars,i,0.0001);
         if (trend[i]==-1) lev[i] = dzBuyP (ttf,DzStartBuyProbability,DzLookBackBars,Bars,i,0.0001);
      
         if (showSignals)
         {
            sigu[i] = EMPTY_VALUE;
            sigd[i] = EMPTY_VALUE;
            trends[i] = 0;
               if (ttf[i] > lev[i]) trends[i] =  1;
               if (ttf[i] < lev[i]) trends[i] = -1;
               if (trends[i]== 1 && trend[i]== 1) sigu[i] = lev[i];
               if (trends[i]==-1 && trend[i]==-1) sigd[i] = lev[i];
         }
   }
   manageAlerts();
   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 (period<=1) return(price);
   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]);
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; 
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar] ==  1) doAlert(whichBar,"up");
         if (trend[whichBar] == -1) doAlert(whichBar,"down");
      }
   }
}

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[forBar]) {
       previousAlert  = doWhat;
       previousTime   = Time[forBar];

       //
       //
       //
       //
       //

       message = timeFrameToString(_Period)+" "+StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," "," trend scalp trend changed to ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"trend scalp "),message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

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("");
}