Stochastic Oscillator
Where the close sits in the recent range: 100 at the top, 0 at the bottom.
What it measures
Section titled “What it measures”%K is the close’s position between the lowest low and the highest high of the last periods bars, as a percentage. %D is a simple moving average of %K over smaPeriods bars, so it is smoother and turns later.
How traders read it
Section titled “How traders read it”Traders read above 80 as overbought and below 20 as oversold, and %K crossing %D as a turn in momentum. Like RSI, it can stay at an extreme through a trend.
Formula
Section titled “Formula”%K = (close − lowest low) / (highest high − lowest low) × 100, over the last periods bars%D = SMA(%K, smaPeriods)This is the “fast” stochastic. A window with no range gives a %K of 0.
Settings and output
Section titled “Settings and output”Call: stochasticOscillator(periods, smaPeriods)
| Setting | Chart default |
|---|---|
periods: Periods |
14 |
smaPeriods: Signal Periods |
3 |
What you get: a list of 2, in this order: 1. a number (“Stochastic Oscillator”); 2. a number (“Stochastic Oscillator 3-Period Moving Average”).
Warm-up: with stochasticOscillator(14, 3), it asks for 14 bars of history and a run-up of 3, and its first value came on bar 17 of the example data.
The prices are made up, hourly, and the same on every page, so the chart shows how the indicator behaves, not how a market behaved.
In an algorithm
Section titled “In an algorithm”let wasBelow = false;
function onIntervalClose() { const lines = stochasticOscillator(14, 3); const k = lines[0]; const d = lines[1]; const below = k < d; if (OPEN_POSITIONS_COUNT === 0 && wasBelow && !below && d < 20) { openPosition("BUY", { quantity: 10000, stopLossPips: 25, takeProfitPips: 40 }); } wasBelow = below;}