//+------------------------------------------------------------------+
//|                                                       VHF_v1.mq4 |
//|                        (Vertical Horizontal Filter by Adam White}|
//|                                  Copyright © 2006, Forex-TSD.com |
//|                         Written by IgorAD,igorad2003@yahoo.co.uk |   
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                           modification by mladen |                                      
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 SkyBlue
#property indicator_width1 2

//
//
//
//
//

extern int    Length     = 14;
extern int    T3Period   = 5;
extern double T3Hot      = 0.70;
extern int    T3Price    = PRICE_CLOSE; 
extern bool   T3Original = false;
extern int    mamode     = MODE_LWMA;

//
//
//
//
//

double VHF[];
double Del[];
double atr[];
double work[];
double temp[];

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

int init()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0,VHF); SetIndexLabel(0,"VHF"); SetIndexDrawBegin(0,Length+T3Period);
   SetIndexBuffer(1,Del);
   SetIndexBuffer(2,atr);
   SetIndexBuffer(3,work);
   SetIndexBuffer(4,temp);
   
   
   
    
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   IndicatorShortName("adaptive T3 Vhf  ("+Length+")");
   return(0);
}

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

int start()
{
   int counted_bars = IndicatorCounted();
   int limit,i;
   
   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--)
   {
      
      double hi  = High[i];
      double lo  = Low[i];
      
      if(i==Bars-1) 
      temp[i]=hi-lo;
      
      else
      {
      
      double cl = Close[i+1];
      temp[i]=MathMax(hi,cl)-MathMin(lo,cl);
      }
   }
   
   //
   //
   //
   //
   //
   
   for(i=limit; i>=0; i--)   
   {
      work[i] = iMAOnArray(temp,Bars,T3Period,0,mamode,i);
         double max = work[ArrayMaximum(work,T3Period,i)];
         double min = work[ArrayMinimum(work,T3Period,i)];
         
         if (min!=max)
               atr[i] = 1.0-(work[i]-min)/(max-min);
         else  atr[i] = 0.5;
         
               Del[i] = MathAbs(Close[i] - Close[i+1]);
   
         double Noise = 0; for (int k=0; k<Length; k++) Noise += Del[k+i];
      if (Noise > 0)
      
            VHF[i] = iT3((Close[iHighest(NULL,0,MODE_CLOSE,Length,i)]-Close[iLowest(NULL,0,MODE_CLOSE,Length,i)])/Noise,T3Period*(atr[i]+1.0)/2.0,T3Hot,T3Original,i);
      else  VHF[i] = iT3(0                                                                                             ,T3Period*(atr[i]+1.0)/2.0,T3Hot,T3Original,i);
   }
	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 forInstance=0)
{
   if (ArrayRange(workT3,0) !=Bars)                  ArrayResize(workT3,Bars);
   if (ArrayRange(workT3Coeffs,0) < (forInstance+1)) ArrayResize(workT3Coeffs,forInstance+1);

   if (workT3Coeffs[forInstance][_period] != period)
   {
     workT3Coeffs[forInstance][_period] = period;
        double a = hot;
            workT3Coeffs[forInstance][_c1] = -a*a*a;
            workT3Coeffs[forInstance][_c2] = 3*a*a+3*a*a*a;
            workT3Coeffs[forInstance][_c3] = -6*a*a-3*a-3*a*a*a;
            workT3Coeffs[forInstance][_c4] = 1+3*a+a*a*a+3*a*a;
            if (original)
                 workT3Coeffs[forInstance][_alpha] = 2.0/(1.0 + period);
            else workT3Coeffs[forInstance][_alpha] = 2.0/(2.0 + (period-1.0)/2.0);
   }
   
   //
   //
   //
   //
   //
   
   int buffer = forInstance*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[forInstance][_alpha]*(price              -workT3[r-1][0+buffer]);
         workT3[r][1+buffer] = workT3[r-1][1+buffer]+workT3Coeffs[forInstance][_alpha]*(workT3[r][0+buffer]-workT3[r-1][1+buffer]);
         workT3[r][2+buffer] = workT3[r-1][2+buffer]+workT3Coeffs[forInstance][_alpha]*(workT3[r][1+buffer]-workT3[r-1][2+buffer]);
         workT3[r][3+buffer] = workT3[r-1][3+buffer]+workT3Coeffs[forInstance][_alpha]*(workT3[r][2+buffer]-workT3[r-1][3+buffer]);
         workT3[r][4+buffer] = workT3[r-1][4+buffer]+workT3Coeffs[forInstance][_alpha]*(workT3[r][3+buffer]-workT3[r-1][4+buffer]);
         workT3[r][5+buffer] = workT3[r-1][5+buffer]+workT3Coeffs[forInstance][_alpha]*(workT3[r][4+buffer]-workT3[r-1][5+buffer]);
      }

   //
   //
   //
   //
   //
   
   return(workT3Coeffs[forInstance][_c1]*workT3[r][5+buffer] + 
          workT3Coeffs[forInstance][_c2]*workT3[r][4+buffer] + 
          workT3Coeffs[forInstance][_c3]*workT3[r][3+buffer] + 
          workT3Coeffs[forInstance][_c4]*workT3[r][2+buffer]);
}

//
//
//
//
//

