Tuesday, September 07, 2010

Average Deviation [ADEV]

Descripton
Summary:
This indicator script measures the average deviations.

Calculation:
The absolute deviations of the underlying indicator values from the average of the underlying indicator values over a specified number of periods.



Variables
TypeIdentifierDescription
Integer_indicatorKeyUse for the underlying indicator key on which to calculate the Average Deviation.
Integer_periodsUse for the number of periods to include in the Average Deviation calculation.

OnInitialize
Function Parameters
TypeIdentifierDescription
IndicatorindicatorKeyUse for the underlying indicator key on which to calculate the Average Deviation.
IntegerperiodsUse for the number of periods to include in the Average Deviation calculation. [Default 10]
Implementation
	 ' Assign the parameters to script variables.
	_indicatorKey = indicatorKey
	_periods = periods

OnValues
Function Parameters
TypeIdentifierDescription
IntegerbarIndex
Integerlength
Implementation
	 ' Get the values of the underlying indicator, indexed by bar index. 
	Define values() As Number = IndicatorValues(_indicatorKey, barIndex, length + _periods)
	 ' Use for the average value of the indicator values.
	Define averageValue As Number
	 ' Use for the sum of the deviations.
	Define sumDev As Number
	 ' Use for the calculated Average Deviation values, indexed by bar index.
	Define results(length - 1) As Number
	 ' Calculate the Average Deviation values for the specified bar range.
	For i As Integer = length - 1 To 0 Step -1
		 ' Calculate the Average Deviation value for the current bar.
		averageValue = 0
		For j As Integer = i + _periods - 1 To i Step -1
			averageValue += values(j)
		Next
		averageValue = averageValue / length
		sumDev = 0
		For j As Integer = i + _periods - 1 To i Step -1
			sumDev += MathAbs(values(j) - averageValue)
		Next
		 ' Assign the Average Deviation value for the current bar.
		results(i) = sumDev / length
	Next
	Return results

Copyright © 2010 IQBroker, LLC. All rights reserved.