Average Directional Index

Average Directional Index

All functions · nseries package

ADX

func (s Series) ADX(h, l, c Series, n int) Series

ADX calculates the Average Directional Index (ADX) based on the given high, low, and close price series and a period n. Returns a series representing the ADX values for the input data.

If a high, low or close is NaN or infinite, the result is NaN from that bar onwards, except on the first bar, whose result is always 0; earlier bars are unaffected. A close counts on its own bar, although the calculation first reads it on the next one, and any later bad bars change nothing. This is the library’s policy for recursive methods: ADX is built on DMIPlus and DMIMinus, and both its initial running sum over the first n bars and its later smoothing depend on every earlier bar, so the result does not recover. It is NaN rather than 0 because a near-zero ADX reads as “no trend” and, in a regime filter for example, would pass for a real reading instead of showing up as missing data. Ordered comparisons such as > and < are false for NaN, and != is true, so test the result with math.IsNaN before comparing it with a threshold. The rule is keyed on the input values, not on the arithmetic: finite inputs whose arithmetic overflows, such as magnitudes near 1e308, are outside it and keep whatever result that arithmetic gives, which can include infinities or NaN.

The result is a new series of the same length as the inputs. If n is less than 1, or the series are empty or differ in length, it is an empty, non-nil series and the values are not inspected. Any n of 1 or more is accepted, even one longer than the series. The receiver is not used and may be nil.

The warm-up reads 0, not NaN: bars 0 to n-1 are 0 and the first valid index is n, the mean of the directional index over bars 1 to n; Wilder’s smoothing follows. Releases before this change gave the running sum divided by n on bars 1 to n-1, so a directional index of 100 read 20, 40, 60 and 80 at n = 5, a rising ramp that an ADX-rising rule reads as a strengthening trend. A NaN or infinite input inside the warm-up still gives NaN from its own bar.