'**********************************************************************
'*   VFI Indicator
'*     by Jeremy Williams
'*	   February 20,2006
'*
'*	Adapted from Technical Analysis of Stocks and Commodities
'*    June 2004 and July 2004
'*
'*  Summary: 
'*
'*      This indicator tracks volume based on the direction of price
'*  movement. It is similar to the On Balance Volume Indicator.
'*  For more information see "Using Money Flow to Stay with the Trend",
'*  and "Volume Flow Indicator Performance" in the June 2004 and 
'*  July 2004 editions of Technical Analysis of Stocks and
'*  Commodities.
'*
'*  Parameters:
'*
'*  Period= Specifies the number of Periods used for VFI calculation
'*
'*  Coef = Specifies the fixed scaling factor for the VFI calculation
'*
'*  VolCoef= Specifies the cutoff for maximum volume in the VFI calculation
'*             
'*  Smoothing= Specifies the number of periods used in the short moving average
'*
'******************************************************************** 

#Indicator
#Param "Period",130
#Param "Coef", .2
#Param "VolCoef",2.5
#Param "SD_Period",30
#Param "Smoothing",3

Dim VINTER 			As Single
Dim Cutoff 			As Single
Dim myLogs 			As Single
Dim INTER			As Single
Dim RawSum			As Single
Dim RawValue 		As Single
Dim MAVolume 		As Single
Dim myVFI 			As Single
Dim myVolume 		As Single
Dim PriceChange 	As Single
Dim Multiplier 		As Single
Dim myTerm 			As Single

' Calculate the cutoff, Moving Average of Volume, and price change

myLogs = Log(Typical())					' Calculation of Cutoff using the formula
INTER = myLogs-myLogs[1]				' 
VINTER = STD(INTER,SD_Period)			' Std. Dev.( ln (Typical Price)- ln (Typical Price[1]) )
Cutoff = Coef*VINTER*C					'********************************************

MAVolume = SMA(V,Period)				' Calculate Volume's Moving Average

PriceChange = Typical()-Typical()[1]	' Caluclate Price Change

' Determine the maximum volume to be added,
If V > VolCoef * MAVolume[1] then 
	myVolume = VolCoef * MAVolume[1]
Else
	myVolume = V
End if

' Determine whether the volume is up volume (multiplier +1) or 
' down volume (multiplier -1). If price change is smaller than cutoff
' do not count volume (multipler 0). 
If PriceChange > Cutoff then 
	Multiplier = 1
Else if PriceChange < 0 - Cutoff then
	Multiplier = -1
Else
	Multiplier = 0
End if 

' After initialization period, Calculate the raw and smoothed values of the VFI.
if bar > period then
	myterm=multiplier*myVolume
	RawSum=Sum(myTerm,period)
	RawValue = RawSum/MAVolume[1]
	myVFI = EMA(RawValue,Smoothing)
else 
	myvfi = 0
end if

Plot("VFI",myVFI)                		' Plot the VFI

Return myVFI  							' Return the value calculated by the indicator