//+----------------------------------------------------------+
//|                                 Ehlers' fisher transform |
//|                                                   mladen |
//+----------------------------------------------------------+
#property  copyright "mladen"
#property  link      "mladenfx@gmail.com"

#property  indicator_separate_window
#property  indicator_buffers   5
#property  indicator_color1    LimeGreen
#property  indicator_color2    Red
#property  indicator_color3    Red
#property  indicator_color4    LimeGreen
#property  indicator_color5    DimGray
#property  indicator_width1    2
#property  indicator_width2    2
#property  indicator_width3    2
#property  indicator_width4    2
#property  indicator_width5    2
#property indicator_level1     0.0
#property indicator_levelcolor Gold

//
//
//
//
//
 
extern string TimeFrame        = "Current time frame"; 
extern int    period           = 10;
extern int    PriceType        = PRICE_MEDIAN;
extern double PriceSmoothing   = 0.3;    // =0.67 bei Fisher_m10 
extern double IndexSmoothing   = 0.3;    // =0.50 bei Fisher_m10
extern bool   HistogramOnSlope = false;

extern bool   alertsOn         = true;
extern bool   alertsOnSlope    = true;
extern bool   alertsOnCurrent  = false;
extern bool   alertsMessage    = true;
extern bool   alertsSound      = true;
extern bool   alertsEmail      = false;

//
//
//
//
//

double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];
double buffer5[];
double Prices[];
double Values[];
double trend[];
double slope[];

string indicatorFileName;
bool   returnBars;
bool   calculateValue;
int    timeFrame;

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

int init()
{
   IndicatorBuffers(7);
      SetIndexBuffer(0,buffer1); SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,buffer2); SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,buffer3); SetIndexStyle(2,DRAW_HISTOGRAM);
      SetIndexBuffer(3,buffer4); SetIndexStyle(3,DRAW_HISTOGRAM);
      SetIndexBuffer(4,buffer5);
      SetIndexBuffer(5,Prices);
      SetIndexBuffer(6,Values);
      
         //
         //
         //
         //
         //
      
         indicatorFileName = WindowExpertName();
         returnBars        = TimeFrame=="returnBars";        if (returnBars)     return(0);
         calculateValue    = TimeFrame=="calculateValue";    if (calculateValue) return(0);
         timeFrame         = stringToTimeFrame(TimeFrame);

         //
         //
         //
         //
         //
            
   
   IndicatorShortName(timeFrameToString(timeFrame)+" Ehlers\' Fisher transform ("+period+")");
return(0);
}


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

int start()
{
   int  counted_bars=IndicatorCounted();
   int  i,r,limit;

   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
           limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { buffer1[0] = limit+1; return(0); }

   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame==Period())
   {
      if (ArrayRange(trend,0)!=Bars) ArrayResize(trend,Bars);
      if (ArrayRange(slope,0)!=Bars) ArrayResize(slope,Bars);   
      for(i=limit, r=Bars-i-1; i>=0; i--,r++)
      { 
         Prices[i] = iMA(NULL,0,1,0,MODE_SMA,PriceType,i);
      
      //
      //
      //
      //
      //
                  
         double MaxH = Prices[ArrayMaximum(Prices,period,i)];
         double MinL = Prices[ArrayMinimum(Prices,period,i)];
         if (MaxH!=MinL)
               Values[i]  = 0.33*2*((Prices[i]-MinL)/(MaxH-MinL)-0.5)+PriceSmoothing*Values[i+1];
         else  Values[i]  = 0.00;
               Values[i]  = MathMin(MathMax(Values[i],-0.999),0.999); 
               buffer5[i] = IndexSmoothing*MathLog((1+Values[i])/(1-Values[i]))+IndexSmoothing*buffer5[i+1];

      // 
      //
      //
      //
      //

         if(buffer5[i]==buffer5[i+1])
            {
               buffer1[i]=buffer1[i+1];
               buffer2[i]=buffer2[i+1];
               buffer3[i]=buffer3[i+1];
               buffer4[i]=buffer4[i+1];
               continue;
            }
      
         //
         //
         //
         //
         //
         
            buffer1[i]=EMPTY_VALUE;
            buffer2[i]=EMPTY_VALUE;
            buffer3[i]=EMPTY_VALUE;
            buffer4[i]=EMPTY_VALUE;
            trend[r]  = trend[r-1];
            slope[r]  = slope[r-1];
            if (buffer5[i] > 0)            trend[r] =  1;
            if (buffer5[i] < 0)            trend[r] = -1;
            if (buffer5[i] > buffer5[i+1]) slope[r] =  1;
            if (buffer5[i] < buffer5[i+1]) slope[r] = -1;
            
            if (HistogramOnSlope)
            {
              if (trend[r] ==  1 && slope[r] ==  1) buffer1[i] = buffer5[i];
              if (trend[r] ==  1 && slope[r] == -1) buffer2[i] = buffer5[i];
              if (trend[r] == -1 && slope[r] == -1) buffer3[i] = buffer5[i];
              if (trend[r] == -1 && slope[r] ==  1) buffer4[i] = buffer5[i];
            }
            else
            {                  
              if (trend[r] ==  1) buffer1[i] = buffer5[i];
              if (trend[r] == -1) buffer3[i] = buffer5[i];
            }
   }
   manageAlerts();
   return(0);
   }      
   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
             
          if (ArrayRange(trend,0)!=Bars) ArrayResize(trend,Bars);
          if (ArrayRange(slope,0)!=Bars) ArrayResize(slope,Bars);   
          for (i=limit, r=Bars-i-1; i>=0; i--,r++)
          { 
             int y = iBarShift(NULL,timeFrame,Time[i]);
                buffer5[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",period,PriceType,PriceSmoothing,IndexSmoothing,4,y);
                buffer1[i] = EMPTY_VALUE;
                buffer2[i] = EMPTY_VALUE;
                buffer3[i] = EMPTY_VALUE;
                buffer4[i] = EMPTY_VALUE;
                
                trend[r]  = trend[r-1];
                slope[r]  = slope[r-1];
                if (buffer5[i] > 0)            trend[r] =  1;
                if (buffer5[i] < 0)            trend[r] = -1;
                if (buffer5[i] > buffer5[i+1]) slope[r] =  1;
                if (buffer5[i] < buffer5[i+1]) slope[r] = -1;
            
                if (HistogramOnSlope)
                {
                  if (trend[r] ==  1 && slope[r] ==  1) buffer1[i] = buffer5[i];
                  if (trend[r] ==  1 && slope[r] == -1) buffer2[i] = buffer5[i];
                  if (trend[r] ==- 1 && slope[r] == -1) buffer3[i] = buffer5[i];
                  if (trend[r] == -1 && slope[r] ==  1) buffer4[i] = buffer5[i];
                }
                else
                {                  
                  if (trend[r] ==  1) buffer1[i] = buffer5[i];
                  if (trend[r] == -1) buffer3[i] = buffer5[i];
                } 
                         
      }
  manageAlerts();
  return(0);
} 

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

void manageAlerts()
{
   if (!calculateValue && alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = Bars-iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar))-1;
      
      //
      //
      //
      //
      //
      
      if (alertsOnSlope)
      {
         if (slope[whichBar] != slope[whichBar-1])
         {
            if (slope[whichBar] == 1) doAlert(whichBar,"slope changed to up");
            if (slope[whichBar] ==-1) doAlert(whichBar,"slope changed to down");
         }         
      }
      else
      {
         if (trend[whichBar] != trend[whichBar-1])
         {
            if (trend[whichBar] == 1) doAlert(whichBar,"crossed zero line up");
            if (trend[whichBar] ==-1) doAlert(whichBar,"crossed zero line 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 =  StringConcatenate(Symbol()," ",timeFrameToString(timeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Ehlers Fisher Transform ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," Ehlers Fisher Transform "),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};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = StringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}

//
//
//
//
//

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}     