The defaults { kPeriod: 14, slowing: 3, dPeriod: 3 } are the slow
stochastic every charting platform ships as "Stochastic". slowing: 1
gives the fast stochastic — the same study, one knob, rather than a
second function whose only difference is a 1.
Appends two columns. %K is undefined for the first
kPeriod + slowing − 2 rows and %D for the first
kPeriod + slowing + dPeriod − 3 — at the defaults, bars 0–14 and 0–16.
Reads high, low and close, each named by an option defaulting to its
DEFAULT_OHLCV column — the atr shape.
Definition
TA-Lib's STOCH with SMA smoothing (slowk_matype = slowd_matype = 0), and STOCHF at slowing: 1. Verified in the oracle fixture: %D
agrees bar-for-bar with an identical warm-up, and %K agrees on every bar
TA-Lib emits, both to 5.7e-14 — the ulp of a value near 100.
Two deliberate deltas from TA-Lib
%K starts when it can. TA-Lib masks %K back to %D's first
valid bar, discarding dPeriod − 1 real values (bars 15 and 16 at the
defaults). This emits them — the same per-column warm-up macd
chose over TA-Lib's masking, for the same reason: a value that is
defined by the definition is a value. The oracle generator asserts the
two warm-ups are identical from TA-Lib's first bar on, and that ours
starts exactly dPeriod − 1 bars earlier.
A flat window is undefined. When HH === LL the ratio is 0/0;
TA-Lib reports 0, which is also the value for "close at the very
bottom of a real range". pond reports no value, as rsi does for
its flat window, and the smoothing carries it: a %K window containing
such a bar has no average (and so no %D for dPeriod bars after) —
an average of three values one of which is unknown is unknown. The rule
itself lives in the kernel (percentOfRangeValues) so
williamsR makes the same call.
Edges
Bounded 0..100 whenever low ≤ close ≤ high — which is a property
of the input, not something enforced here. Redirect close at a
column the range does not bound (a smoothed close, say) and %K reads
outside 0..100, honestly, rather than clamped.
Scale-invariant: a ratio of price differences, so scaling every
price leaves it unchanged (pinned by a property test alongside RSI).
A leading gap shifts the start. Running with close redirected at
another study's output starts that many bars later.
An interior gap in close costs that bar's fast %K, then
slowing bars of %K and dPeriod − 1 more of %D — the window
recovers once the gap has left it, unlike a Wilder recursion. A gap in
high or low is skipped by the range (core's reducer policy: the
extreme over the cells the window does hold), so it costs nothing unless
the window is entirely empty.
Williams %R is fast %K − 100: williamsR equals
stochastic({ slowing: 1 })'s %K shifted down by 100, bar for bar,
and a test pins that identity.
Stochastic oscillator (George Lane) — where the close sits in the trailing
kPeriod-bar range, as a percentage, smoothed twice:The defaults
{ kPeriod: 14, slowing: 3, dPeriod: 3 }are the slow stochastic every charting platform ships as "Stochastic".slowing: 1gives the fast stochastic — the same study, one knob, rather than a second function whose only difference is a1.Appends two columns.
%Kisundefinedfor the firstkPeriod + slowing − 2rows and%Dfor the firstkPeriod + slowing + dPeriod − 3— at the defaults, bars 0–14 and 0–16.Reads high, low and close, each named by an option defaulting to its
DEFAULT_OHLCVcolumn — the atr shape.Definition
TA-Lib's
STOCHwith SMA smoothing (slowk_matype = slowd_matype = 0), andSTOCHFatslowing: 1. Verified in the oracle fixture:%Dagrees bar-for-bar with an identical warm-up, and%Kagrees on every bar TA-Lib emits, both to5.7e-14— the ulp of a value near 100.Two deliberate deltas from TA-Lib
%Kstarts when it can. TA-Lib masks%Kback to%D's first valid bar, discardingdPeriod − 1real values (bars 15 and 16 at the defaults). This emits them — the same per-column warm-up macd chose over TA-Lib's masking, for the same reason: a value that is defined by the definition is a value. The oracle generator asserts the two warm-ups are identical from TA-Lib's first bar on, and that ours starts exactlydPeriod − 1bars earlier.undefined. WhenHH === LLthe ratio is0/0; TA-Lib reports0, which is also the value for "close at the very bottom of a real range". pond reports no value, as rsi does for its flat window, and the smoothing carries it: a%Kwindow containing such a bar has no average (and so no%DfordPeriodbars after) — an average of three values one of which is unknown is unknown. The rule itself lives in the kernel (percentOfRangeValues) so williamsR makes the same call.Edges
0..100wheneverlow ≤ close ≤ high— which is a property of the input, not something enforced here. Redirectcloseat a column the range does not bound (a smoothed close, say) and%Kreads outside0..100, honestly, rather than clamped.closeredirected at another study's output starts that many bars later.closecosts that bar's fast%K, thenslowingbars of%KanddPeriod − 1more of%D— the window recovers once the gap has left it, unlike a Wilder recursion. A gap inhighorlowis skipped by the range (core's reducer policy: the extreme over the cells the window does hold), so it costs nothing unless the window is entirely empty.%Risfast %K − 100: williamsR equalsstochastic({ slowing: 1 })'s%Kshifted down by 100, bar for bar, and a test pins that identity.