Efficiency Ratio
All functions · nseries package
EfficiencyRatio
func (s Series) EfficiencyRatio(n int) Series
EfficiencyRatio calculates the Kaufman Efficiency Ratio for a specified length n on a series: the absolute change over the last n bars divided by the sum of the absolute one-bar changes over those bars. Each computed ratio lies in [0, 1]; rounding can put the quotient fractionally above one, so it is clamped to exactly 1 (a NaN is left as NaN). A window with no movement at all (zero total absolute change) has an efficiency of zero. It returns a new Series of the same length without modifying s.
For n >= 1, bars 0 to n-1 (the first min(n, len(s)) bars) are 0. The first valid index is n, and the minimum length for one valid bar is n + 1. n >= len(s) gives all zeros, as does n < 1.
The warm-up reads 0, not NaN. A warm-up 0 is indistinguishable from a computed “no net movement” (perfectly choppy) window, so an untrimmed rule such as ER < 0.3 (chop) fires on every warm-up bar, while a rule such as ER >= 0.3 (trend) does not. Callers should mask or trim the first n bars: er.Window(-n) drops them; er.SetN(n, math.NaN()) returns a copy with them marked NaN. Window(m) returns the last m values for positive m and all but the first -m values for negative m.
Releases before this change gave 0.5 on the warm-up bars. KAMA is unaffected because it reads the ratio only from bar n on.