'**********************************************************************
'*   Z-Score Indicator
'*     by Jeremy Williams
'*	   February 21,2006
'*
'*	Adapted from Technical Analysis of Stocks and Commodities
'*    February 2003
'*
'*  Summary: 
'*
'*      This indicator measures the normalized distance from the  
'*  N period Moving Average using the statistical formula for Z-value.
'*  For more information see "Z-Score Indicator" in the 
'*  February 2003 edition of Technical Analysis of Stocks and
'*  Commodities.
'*
'*  Parameters:
'*
'*  	N= Specifies the number of Periods used for the Average and
'*             Standard Deviation calculations used to determine the 
'*             Z-Score.
'*
'******************************************************************** 

#Indicator
#Param "N",20

Dim myAverage 	As Single
Dim mySD 		As Single
Dim Value 		As Single
Dim OutValue 	As Single

' Value statement can be changed to other data series. For example,
' to calculate the Z-Score Indicator of the RSI use:
' Value = RSI(Periods)

Value = C
								
myAverage = Average(Value,N)	' Calculate the Average and Standard Deviation
mySD = STD(Value,N)

OutValue = Value - myAverage	' Calculate the Z-Score using the formula:
OutValue = OutValue / mySD		' Z = ( Value - Average ) / Standard Deviation 

If Bar < N then					' At Initialization set OutValue to 0 until
	OutValue = 0				' Standard Deviation is not zero.
End If

Plot("Z-Score",OutValue)		' Plot the Z-score and the 1-Std. Dev. Band
PlotLabel(-1)
PlotLabel(1)

Return OutValue					' Return the Z-value