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.
' Assign the parameters to script variables. _indicatorKey = indicatorKey _periods = periods
' 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