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

#property  indicator_separate_window
#property  indicator_buffers 5
#property  indicator_color1  FireBrick
#property  indicator_color2  ForestGreen
#property  indicator_color3  DimGray
#property  indicator_color4  PaleVioletRed
#property  indicator_color5  YellowGreen
#property  indicator_width4  2
#property  indicator_style3  STYLE_DOT
#property  indicator_style5  STYLE_DOT

//
//
//
//
//

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

//
//
//
//
//
 
extern int    period                 = 16;
extern int    PriceType              = PRICE_TYPICAL;
extern bool   showSignalLine         = true;
extern int    DzLookBackBars         = 30;
extern double DzStartBuyProbability  = 0.2;
extern double DzStartSellProbability = 0.2;
extern int    smoozing =5;
extern bool   Move = false;

//
//
//
//
//

double buffer1[];
double buffer2[];
double         ExtBuffer1[];
double         ExtBuffer2[];
double         ExtBuffer3[];
double Prices[];
double Values[];
double sli[];
double bli[];
double zli[];

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

int init()
{
   IndicatorBuffers(10);
      SetIndexBuffer(0,bli);
      SetIndexBuffer(1,sli);
      SetIndexBuffer(2,zli);
      SetIndexBuffer(3,buffer1);
      SetIndexBuffer(4,buffer2);
      SetIndexBuffer(5,Prices);
      SetIndexBuffer(6,Values);
      SetIndexBuffer(7,ExtBuffer1);
      SetIndexBuffer(8,ExtBuffer2);
      SetIndexBuffer(9,ExtBuffer3);
   IndicatorShortName("Ehlers\' Fisher transform ("+period+")("+smoozing+")");
   return(0);
}


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

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

   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
           limit = MathMin(Bars-counted_bars,Bars-1);

     //
   //
         
   for(i=limit; i>=0; i--)
   {
      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)+0.67*Values[i+1];
         else  Values[i] = 0.00;
               Values[i] = MathMin(MathMax(Values[i],-0.999),0.999); 

       //
      //

      buffer1[i] = 0.5*MathLog((1+Values[i])/(1-Values[i]))+0.5*buffer1[i+1];
      }
    //
    //  
    
    
     for( i=limit; i>=0; i--)
     {
      ExtBuffer3[i]=iMAOnArray(buffer1,0,smoozing,0,MODE_EMA,i);
      if(ExtBuffer3[i]<0)
        {
         if(ExtBuffer3[i+1]>0) {
            if(!Move) {
               ExtBuffer2[i]=ExtBuffer3[i];
               ExtBuffer2[i+1]=ExtBuffer3[i+1]; 
               ExtBuffer1[i]=EMPTY_VALUE;
            }
            else {
               ExtBuffer1[i]=ExtBuffer3[i];
               ExtBuffer1[i+1]=ExtBuffer3[i+1]; 
               ExtBuffer2[i]=ExtBuffer3[i];
            }
         }
         
         else {
            ExtBuffer2[i]=ExtBuffer3[i];
//            ExtBuffer1[i]=0.0;
            ExtBuffer1[i]=EMPTY_VALUE;
         }
        }
      else
        {
         if(ExtBuffer3[i+1]<0) {
            if(!Move) {
               ExtBuffer1[i]=ExtBuffer3[i];
               ExtBuffer1[i+1]=ExtBuffer3[i+1]; 
               ExtBuffer2[i]=EMPTY_VALUE;
            }
            else {
               ExtBuffer2[i]=ExtBuffer3[i];
               ExtBuffer2[i+1]=ExtBuffer3[i+1]; 
               ExtBuffer1[i]=ExtBuffer3[i];
            }
         }
         else {
            ExtBuffer1[i]=ExtBuffer3[i];
//            ExtBuffer2[i]=0.0;
            ExtBuffer2[i]=EMPTY_VALUE;
         }
        }
    
    
    
    
    
    
    
      if (showSignalLine)
            buffer2[i] = buffer1[i+1];
            bli[i]     = dzBuyP (buffer1, DzStartBuyProbability,  DzLookBackBars, Bars, i,0.0001);
            sli[i]     = dzSellP(buffer1, DzStartSellProbability, DzLookBackBars, Bars, i,0.0001);
            zli[i]     = dzSellP(buffer1, 0.5,                    DzLookBackBars, Bars, i,0.0001);
   }
   return(0);
}