Stochastic oscillator
Is the close near the top or the bottom of its recent range?
Where today's close sits between the highest high and lowest low of the last N days, from 0 (at the low) to 100 (at the high).
Stochastic on a chart of past prices
Settings: move a slider and the chart redraws
How many days of highs and lows the close is compared with.
How much the raw reading is smoothed into K. 1 means not at all.
How much K is smoothed again into the slower D line.
The rule only sells crossings above this line.
The rule only buys crossings below this line.
What it measures
Take the highest high and the lowest low of the last 14 days. If today's close is right at that high, the raw reading is 100; at the low, 0; halfway, 50.
That raw reading jumps around, so it is smoothed into the K line, and K is smoothed again into the slower D line. In this app (on the chart and in scripts alike) a smoothing of 3 means each new K is two-thirds of yesterday's K plus one-third of today's raw reading.
Above 80 is usually called “overbought” and below 20 “oversold”: the close has been near the top, or the bottom, of its recent range.
How traders read it
- Above 80: closes near the top of the recent range. Below 20: near the bottom.
- K crossing above D while below 20 is the classic buy (the close is lifting off the bottom of its range); K crossing below D while above 80 the classic sell. Marked on the chart.
- It is faster than RSI and reaches the extremes much more often.
The traps
Pinned at the top in a trend
In a steady uptrend every close is near the recent high, so the stochastic sits above 80 for long stretches.
Lots of crossings
K and D cross often; most crossings away from the extremes mean little, which is why the rule ignores them.
A narrow range exaggerates
When the highest high and lowest low are close together, a small move swings the reading from 0 to 100.
Test the classic rule: Stochastic crossover at the extremes
Waiting for prices…
Quick check
Waiting for prices…
Use it in a script
function run(bars, ta, helpers) {
const s = ta.stochasticKdj(bars, 14, 3, 3);
const k = s.map((x) => x.k), d = s.map((x) => x.d);
const up = helpers.crossover(k, d), down = helpers.crossunder(k, d);
const signals = [];
for (let i = 0; i < bars.length; i++) {
if (up[i] && d[i] < 20) signals.push(helpers.buy(i)); // K crosses above D near the bottom
else if (down[i] && d[i] > 80) signals.push(helpers.sell(i)); // K crosses below D near the top
}
return helpers.finalize(signals);
}This is the rule tested above, with the settings on your sliders, and it gives exactly the same buy and sell candles. Paste it into the Scripts drawer of the chart workbench, or save it and compare it across stocks in the Strategy Lab. New to scripts? The Writing strategies track starts from zero.
In the reference:ta.stochasticKdjhelpers.crossoverhelpers.crossunder