dev

Mean Absolute Deviation

dev(source, length)

Calculates the mean absolute deviation of a series from its simple moving average. This measures the average distance between each value and the mean, providing a measure of dispersion similar to standard deviation but using absolute differences.

Parameters

  • source (field): The data series to calculate deviation on
  • Valid inputs: close, open, high, low, or any indicator output
  • length (int): Number of periods for the calculation
  • Common values: 20 (standard), 14, 30
  • Range: 1 to any positive integer

Formula


DEV = AVG(|source[i] - SMA(source, length)|) for i in [0, length-1]

Where:
- SMA(source, length) = Simple Moving Average of source
- |x| = Absolute value of x
- AVG = Average over the period window

Examples


dev(close, 20);  # Standard 20-period deviation
dev(close, 14);  # 14-period deviation
dev(close, 20) > 2.0;  # High dispersion condition
x = dev(close, 20); x > dev(close, 50);  # Short-term vs long-term volatility

Returns

Mean absolute deviation value (always non-negative, same units as source)

Notes

  • Always returns non-negative values (uses absolute differences)
  • Higher values indicate greater dispersion/volatility
  • Lower values indicate tighter clustering around the mean
  • Used in CCI calculation with 0.015 scaling factor
  • Computationally simpler than standard deviation (no squaring)
  • Returns NULL when insufficient data (< length bars)