#Indicator
'**************************************************************
'*   Short-Term Volume and Price Oscillator  (SVAPO.txt)
'*     by Jeremy Williams
'*	   Dec. 10, 2007
'*
'*	Adapted from Technical Analysis of Stocks and Commodities
'*     November 2007
'*
'*  Summary: 
'*
'*      This stop uses the RSI to dynamically adjust the cushion
'*  of a trailing profit stop, allowing more advanced forms of 
'*  trade management. For more information see "Profit Locking and
'*  The Relative Price Channel" in the January 2007 issue of 
'*  Technical Analysis of Stocks and Commodities.
'*
'*  Parameters:
'*
'*  BearishPeriods -  Specifies the number of  bearish periods 
'*
'*  Factor- Multiplicative Factor specifying the stop cushion
'*
'*  ChannelPeriods - Specifies the number of periods for the channel
'* 
'*  Overbought  - Specifies the RSI level corresponding to an
'*                  overbought condition
'*
'*  ChannelSmoothing - Specifies the smoothing for the Overbought Channel
'*
'************************************************************** 

#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
VAvg = SMA( v, SVAPOPeriod * 5) 
VMax = 2*VAvg[1]
Vc = IIF(V > VMax, VMax, V)
VSlope = LNREG_SLOPE(V, SVAPOPeriod)
VEMA1 = EMA(VSlope , SVAPOPeriod)
VEMA2 = EMA(VEMA1, SVAPOPeriod)
VEMA3 = EMA(VEMA2, SVAPOPeriod)
VTrend = 3*VEMA1 -3*VEMA2 + VEMA3

'Calculate 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)
' Calculate TEMA after warmup
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