Stochastic
All functions · nseries package
Stochastic
func (s Series) Stochastic(h, l Series, n int) Series
Stochastic calculates the percentage value of each input value across the range determined by looking back at the last n values. Mismatched input lengths return a zero-filled series of the receiver’s length.
The output is on a 0 to 100 scale, 100*(s - LL)/(HH - LL), with HH and LL the highest high and lowest low of the last n bars. Bars 0 to n-2 are exactly 50 (warm-up), not 0: the first valid index is n-1 and the minimum length is n, and n > len(s) gives all 50. 50 is mid-range, so neither an oversold (< 20) nor an overbought (> 80) rule fires on a warm-up bar, but a crossing of 50 or a comparison with a moving average of the output can. Trim the warm-up (Window(-(n-1))) before smoothing, because a NaN mask would poison a recursive average such as XAverage; before a threshold rule, trim or mask it (SetN(n-1, math.NaN())). A flat window (HH == LL) gives 0, not 50, so the warm-up and flat values differ. This warm-up was reviewed in GLE-254 (3 October 2026) and kept, because 0 would read as the oversold extreme and would change every later bar of an XAverage of the output. ChannelPosition(n, 1) gives the house form, exactly 0 before its first valid index n-1 and 50 on a flat channel, and equals s.Stochastic(s, s, n) on windows that are not flat, after that index.