Skip to content

Technical indicators

Stochastic Oscillator

Where the close sits in the recent range: 100 at the top, 0 at the bottom.

%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.

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.

%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.

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.

Stochastic Oscillator drawn by its own code on example prices

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.

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;
}