'**************************************************************
'*   Percent D Indicator (PercentD.txt)
'*     by Jeremy Williams
'*	   January 13, 2012
'*
'*	Adapted from Technical Analysis of Stocks and Commodities
'*     March 2012
'*
'*  Summary: 
'*
'*      This indicator plots a the Percent D line of a 
'*  Stochastics Indicator. The smoothing periods for this 
'*  indicator are set at 3 and 9 respectively. For more 
'*  information see "Introducing SwamiCharts" in the March 2012
'*  edition of Technical Analysis of Stocks and Commodities.
'*
'*  Parameters:
'*  
'*  Periods- Sets the number of periods used for the base 
'*  calculation of the Stochastics indicator
'*
'*  Notes:
'*
'*  The two smoothing parameters for a traditional stochastics
'*  are fixed at 3 and 9 respectively for this indicator.
'*
'************************************************************** 

#Indicator
#Param "Periods",14

Dim HH As Single
Dim LL As Single
Dim Num As Single
Dim Denom AS Single
Dim D As Single

' Calculate the highs and lows
HH= HHV(Periods)
LL=LLV(Periods)

' Calculate numerator and denominator with smoothing
' Note: alpha of .5 equates to a 3 period EMA
Num = .5*(Close-LL)+.5*Num[1]
Denom = .5*(HH-LL)+.5*Denom[1]

' Calcuate the %D line
' Note: alpha of .2 equates to a 9 period EMA
If Denom <>0 Then 
	D= .2*Num/Denom +.8*D[1]
Else
	D= 0
End IF

' Plot the Percent D indicator
Plot("PercentD", D)

' Return the calculated Value
Return D