//+------------------------------------------------------------------+
//|                          Ehlers distance coefficient filter zone |
//|                                                                  |
//| idea for this indicator by heikobaer                             |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Red
#property indicator_width1 2
#property indicator_width2 2

//
//
//
//
//

extern int  Length              = 20;
extern bool OriginalCalculation = false;

//
//
//
//
//

double filterHi[];
double filterLo[];

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

int init()
{
   SetIndexBuffer(0,filterHi);
   SetIndexBuffer(1,filterLo);
   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--) 
   {
      filterHi[i] = iEhlers(iMA(NULL,0,1,0,MODE_SMA,PRICE_HIGH,i),Length,0,i);
      filterLo[i] = iEhlers(iMA(NULL,0,1,0,MODE_SMA,PRICE_LOW ,i),Length,1,i);
   }      
   return(0);
  }


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

double  enlf_work[][4];
#define enlf_price    0
#define enlf_smooth   1

//
//
//
//
//

double  iEhlers(double price,int length, int forBuffer, int i)
{   
   if (ArrayRange(enlf_work,0)!=Bars) ArrayResize(enlf_work,Bars);
   
   int r = Bars-i-1;
   int s = forBuffer*2;

   //
   //
   //
   //
   //
      
      enlf_work[r][s+enlf_price]  = price;
      if (OriginalCalculation)
            enlf_work[r][s+enlf_smooth] = enlf_work[r][s+enlf_price];
      else  enlf_work[r][s+enlf_smooth] = (enlf_work[r][s+enlf_price]+2.0*enlf_work[r-1][s+enlf_price]+2.0*enlf_work[r-2][s+enlf_price]+enlf_work[r-3][s+enlf_price])/6.0;

      //
      //
      //
      //
      //
      
      double sumCoef = 0;
      double num     = 0;
      for (int k=0; k<length; k++)
      {
         double distance = 0;
         for (int j=1; j<length; j++)
            distance += MathPow(enlf_work[r-k][s+enlf_smooth]-enlf_work[r-k-j][s+enlf_smooth],2);
            sumCoef  += distance;
            num      += distance*enlf_work[r-k][s+enlf_smooth];
      } 
      
      if (sumCoef !=0)
            return(num/sumCoef);
      else  return(0);       
}