//+------------------------------------------------------------------+
//|                                                       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 |                                      
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"

#property indicator_separate_window
#property indicator_buffers 2

#property indicator_color1 SkyBlue
#property indicator_width1 2
#property indicator_color2 Tomato
#property indicator_width2 0
#property indicator_style2 2

//---- input parameters
extern int     Length         = 28;  // Period of VHF
extern int     Smooth         = 5;   // Period of Smoothing MA  
extern int     MASmooth       = 0;   // Mode of Smoothing MA 
//---- indicator buffers
double VHF[];
double VHFSmooth[];
double Del[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   IndicatorBuffers(3);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,VHF);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,VHFSmooth);
   SetIndexBuffer(2,Del);
    
   string short_name;
//---- indicator line
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
   short_name="VHF("+Length+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"VHF");
   SetIndexLabel(1,"Smooth");
//----
    
   SetIndexDrawBegin(0,Length+Smooth);
   SetIndexDrawBegin(1,Length+Smooth);
//----
 
   return(0);
  }

//+------------------------------------------------------------------+
//| VHF_v1                                                           |
//+------------------------------------------------------------------+
int start()
{
   int    i,shift, counted_bars=IndicatorCounted(),limit;
   double price;      
   if ( counted_bars > 0 )  limit=Bars-counted_bars;
   if ( counted_bars < 0 )  return(0);
   if ( counted_bars ==0 )  limit=Bars-1; 
   if ( counted_bars < 1 ) 
   
   for(i=1;i<Length;i++) 
   {
   VHF[Bars-i]=0;    
   VHFSmooth[Bars-i]=0;  
   }
   
   for(shift=limit;shift>=0;shift--) 
   {	
   Del[shift] = Close[shift] - Close[shift+1];
   
   double Noise=0;
   for (i=0;i<=Length-1;i++) Noise += MathAbs(Del[shift+i]);
   
   double HCP = Close[iHighest(NULL,0,MODE_CLOSE,Length,shift)];
   double LCP = Close[iLowest (NULL,0,MODE_CLOSE,Length,shift)];            
   
   if (Noise > 0) VHF[shift] = (HCP-LCP)/Noise;
   }
   
   for(shift=limit;shift>=0;shift--) 
   VHFSmooth[shift] = iMAOnArray(VHF,0,Smooth,0,MASmooth,shift);  

//----
	return(0);	
}

