#Indicator
'**************************************************************************
'*   Short-Term Volume and Price Oscillator  (SVAPOModified.txt)
'*     Modified for use without volume data
'*     by Jeremy Williams
'*	   Dec. 10, 2007
'*
'*	Adapted from Technical Analysis of Stocks and Commodities
'*     February 2008
'*
'*  Summary: 
'*
'*      This indicator is based on the Short Term Volume and Price
'*  Oscillator by Sylvain Vervoort in the November 2007 issue of 
'*  Technical analysis of stocks and commodities. It has been modified
'*  for the February 2008 article to be used without volume data. 
'*  For more information see "Trading Divergences" in the February 2008
'*  issue of Technical Analysis of Stocks and Commodities.
'*
'*  Parameters:
'*
'*  SVAPOPeriod -  Specifies the number of periods for SVAPO calculation 
'*
'*  Cutoff- Specifies the minimal price movement
'*
'*  SDHigh - Specifies the number of Standard deviations for the top band
'* 
'*  SDLow  - Specifies the number of Standard deviations for the low band
'*
'*  SDPeriod - Specifies the number of periods for the Standard Deviation
'*
'************************************************************************* 

#Param "SVAPOPeriod", 8
#Param "Cutoff" , 1
#Param "SDHigh", 1.5
#Param "SDLow", 1.3
#Param "SDPeriod" , 100

' Heiken Ashi candle variables
Dim haOpen		As Single
Dim haClose		As Single
Dim haHigh		As Single
Dim haLow		As Single
Dim haC			As Single


' Intermediate variables to simplify the calculations
Dim haEMA1		As Single
Dim haEMA2		As Single
Dim haEMA3		As Single
Dim VAvg		As Single
Dim VMax		As Single
Dim Vc			As Single
Dim VSlope		As Single
Dim VEMA1		As Single
Dim VEMA2		As Single
Dim VEMA3		As Single
Dim VTrend		As Single
Dim Downward	As Single
Dim Total 		As Single
Dim mySum		As Single
Dim indEMA1		As Single
Dim indEMA2		As Single
Dim indEMA3		As Single
Dim mySD		As Single


' Output Variables
Dim indValue	As Single
Dim UpperBand	As Single
Dim LowerBand	As Single

' Calculate the Heiken Ashi variables
haOpen = (O[1] + H[1] + L[1] + C[1])*.25
haHigh = IIF( haOpen > H ,haOpen, H)
haLow = IIF (haOpen < L , haOpen, L)
haClose = (( o + h + l + c) * .25 + haOpen + haHigh +haLow)*.25

' Calculate the Heiken Ashi TEMA smoothed Close
haEMA1 = EMA (haClose,SVAPOPeriod / 1.6)
haEMA2 = EMA (haEMA1, SVAPOPeriod / 1.6)
haEMA3 = EMA (haEMA2, SVAPOPeriod / 1.6)
haC = 3*haEMA1 - 3*haEMA2 + haEMA3

' Calculate the Volume Statistics and Volume Trend
' Here were are using price data to mimmick volume.
VAvg = SMA( C, SVAPOPeriod * 5) 
VMax = 2*VAvg[1]
Vc = C
VSlope = LNREG_SLOPE(C,SVAPOPeriod)
VEMA1 = EMA(VSlope , SVAPOPeriod)
VEMA2 = EMA(VEMA1, SVAPOPeriod)
VEMA3 = EMA(VEMA2, SVAPOPeriod)
VTrend = 3*VEMA1 -3*VEMA2 + VEMA3

'Calculate the sum for the SVAPO
Downward = IIF( haC<haC[1]*(1-Cutoff/1000) AND _
			 (VTrend > VTrend[1] OR VTrend[1]>VTrend[2]), _ 
			 0 - Vc , 0 )
			 
Total = IIF( haC > haC[1]*(1+Cutoff/1000) AND _ 
			 (VTrend>=VTrend[1] OR VTrend[1] >= VTrend[2]), _ 
			  Vc , Downward)
			  
mySum = SUM(Total,SVAPOPeriod)/(VAvg+1)

' After warm up calculate the TEMA
If bar > 10*SVAPOPeriod + 5 Then
	indEMA1	= EMA(mySum, SVAPOPeriod)
	indEMA2	= EMA(indEMA1, SVAPOPeriod)
	indEMA3 = EMA(indEMA2, SVAPOPeriod)
	indValue = 3*indEMA1 - 3*indEMA2 + indEMA3
Else 
	indValue = 0
End If

' After Warm-up period, calculate the Bands
If bar > SDPeriod + 2*SVAPOPeriod +1 Then
	mySD = StD(indValue, SDPeriod)
	UpperBand = mySD*SDHigh
	LowerBand = 0-mySD*SDLow
End If

' Plot the Bands
Plot("SVAPO(Mod)", indValue)
Plot("Upper Band", UpperBand)
Plot("Lower Band", LowerBand)

' Return the value calculated by the indicator
Return indValue