Stochastic Oscillator
Where the close sits inside the recent high-low range, on a 0-100 scale, with %K, a %D signal line and 80/20 bands.
- Stoch
- Built into Spectral
- Updates every tick
- stochastic
- oscillator
- overbought
- oversold
- momentum
- %k %d
- slow stochastic
MIT-0: free to use, change and share, no attribution needed. What you build from it is yours.
Already in Spectral: add it from the Indicators toolbox. The file here is the same indicator written as a custom indicator, to change or learn from. Installed, it appears next to the built-in one, marked Custom.

Overview
The Stochastic Oscillator shows where the close sits inside the high-low range of the last %K length bars: 100 is a close at the highest high, 0 a close at the lowest low. %K is that position, optionally smoothed; %D is a simple average of %K that acts as a signal line. Bands at 80 and 20 mark the upper and lower part of the range.
How to read it
Above 80 the market is closing near the top of its recent range, below 20 near the bottom. In a strong trend the oscillator can stay above 80 or below 20 for a long time, so a band touch is not a reversal by itself.
%K crossing above %D shows closes moving up within the range, crossing below shows them moving down; such crosses mean more near the bands. A higher low in the oscillator while price makes a lower low (or the reverse at the top) is a divergence worth comparing with price structure.
Settings and limitations
Defaults are 14 bars for %K, no %K smoothing and 3 bars for %D, as on TradingView. Setting %K smoothing to 3 gives the widely used slow stochastic (14, 3, 3); shorter %K lengths such as 5 react faster but give more crosses.
%K first appears on bar %K length + %K smoothing - 1 (bar 14 with the defaults), %D another %D smoothing - 1 bars later. A range with no width (every bar at the same price) has no value.
Notes
Matches the Stochastic on TradingView and most platforms: raw %K = 100 x (close - lowest low) / (highest high - lowest low), %K = simple average of raw %K, %D = simple average of %K.
Settings
Each setting appears in the indicator's settings panel in Spectral, next to a color setting for every output.
| Setting | Type | Default | Range or options | What it does |
|---|---|---|---|---|
| %K length | Whole number | 14 | 1 – 500 | Bars in the high-low range the close is compared with. |
| %K smoothing | Whole number | 1 | 1 – 100 | Bars in the simple average applied to %K. 1 draws the raw (fast) %K; 3 gives the common slow stochastic. |
| %D smoothing | Whole number | 3 | 1 – 100 | Bars in the simple average of %K that forms the %D signal line. |
| Upper band | Number | 80 | 0 – 100, step 1 | Level of the overbought reference band. |
| Middle band | Number | 50 | 0 – 100, step 1 | Level of the middle reference band. |
| Lower band | Number | 20 | 0 – 100, step 1 | Level of the oversold reference band. |
| Fill between bands | On / off | On | — | Shade the area between the upper and lower band. |
Presets
Each preset is its own entry in the Indicators toolbox, with these settings changed.
| Toolbox entry | Short name | Settings |
|---|---|---|
| Slow Stochastic (14, 3, 3) | Stoch 14 3 3 | %K smoothing: 3 |
| Stochastic (5, 3, 3) | Stoch 5 3 3 | %K length: 5%K smoothing: 3 |
What it draws
Lines
- %Ksolid · 1.5 px · primary
#8AA9C3 - %Dsolid · 1 px
#C4A46A
Levels
- Upper banddashed · 1 px
#788493 - Middle banddotted · 1 px
#788493 - Lower banddashed · 1 px
#788493
Zones
- Band fillfilled at 10% opacity
#8AA9C3
Source
Save it as stochastic.js and add it with Indicators → Custom indicators → Add file…. The format is explained in the custom indicators guide.
// Copyright (c) 2026 Spectral
// SPDX-License-Identifier: MIT-0
// Free to use, copy, modify and share: https://spectral.trade/docs/custom-indicators/LICENSE.txt
/** @type {import('./spectral-indicator').IndicatorV2} */
export default {
schemaVersion: 2,
id: 'stochastic',
name: 'Stochastic Oscillator',
shortName: 'Stoch',
version: '1.0.0',
author: 'Spectral',
description: 'Where the close sits inside the recent high-low range, on a 0-100 scale, with %K, a %D signal line and 80/20 bands.',
pane: 'oscillator',
live: { lookback: 'kLength' },
inputs: {
kLength: { type: 'integer', label: '%K length', default: 14, min: 1, max: 500, description: 'Bars in the high-low range the close is compared with.' },
kSmoothing: { type: 'integer', label: '%K smoothing', default: 1, min: 1, max: 100, description: 'Bars in the simple average applied to %K. 1 draws the raw (fast) %K; 3 gives the common slow stochastic.' },
dSmoothing: { type: 'integer', label: '%D smoothing', default: 3, min: 1, max: 100, description: 'Bars in the simple average of %K that forms the %D signal line.' },
upperBand: { type: 'number', label: 'Upper band', default: 80, min: 0, max: 100, step: 1, description: 'Level of the overbought reference band.' },
middleBand: { type: 'number', label: 'Middle band', default: 50, min: 0, max: 100, step: 1, description: 'Level of the middle reference band.' },
lowerBand: { type: 'number', label: 'Lower band', default: 20, min: 0, max: 100, step: 1, description: 'Level of the oversold reference band.' },
showFill: { type: 'boolean', label: 'Fill between bands', default: true, description: 'Shade the area between the upper and lower band.' },
},
plots: {
k: { label: '%K', color: '#8AA9C3', lineWidth: 1.5, primary: true },
d: { label: '%D', color: '#C4A46A', lineWidth: 1 },
},
levels: {
upper: { label: 'Upper band', color: '#788493', lineStyle: 'dashed' },
middle: { label: 'Middle band', color: '#788493', lineStyle: 'dotted' },
lower: { label: 'Lower band', color: '#788493', lineStyle: 'dashed' },
},
zones: {
fill: { label: 'Band fill', color: '#8AA9C3', opacity: 0.1 },
},
presets: [
{ id: 'slow', name: 'Slow Stochastic (14, 3, 3)', shortName: 'Stoch 14 3 3', inputs: { kSmoothing: 3 } },
{ id: 'short', name: 'Stochastic (5, 3, 3)', shortName: 'Stoch 5 3 3', inputs: { kLength: 5, kSmoothing: 3 } },
],
docs: {
overview: 'The Stochastic Oscillator shows where the close sits inside the high-low range of the last %K length bars: 100 is a close at the highest high, 0 a close at the lowest low. %K is that position, optionally smoothed; %D is a simple average of %K that acts as a signal line. Bands at 80 and 20 mark the upper and lower part of the range.',
howToRead: 'Above 80 the market is closing near the top of its recent range, below 20 near the bottom. In a strong trend the oscillator can stay above 80 or below 20 for a long time, so a band touch is not a reversal by itself.\n\n%K crossing above %D shows closes moving up within the range, crossing below shows them moving down; such crosses mean more near the bands. A higher low in the oscillator while price makes a lower low (or the reverse at the top) is a divergence worth comparing with price structure.',
settings: 'Defaults are 14 bars for %K, no %K smoothing and 3 bars for %D, as on TradingView. Setting %K smoothing to 3 gives the widely used slow stochastic (14, 3, 3); shorter %K lengths such as 5 react faster but give more crosses.\n\n%K first appears on bar %K length + %K smoothing - 1 (bar 14 with the defaults), %D another %D smoothing - 1 bars later. A range with no width (every bar at the same price) has no value.',
notes: 'Matches the Stochastic on TradingView and most platforms: raw %K = 100 x (close - lowest low) / (highest high - lowest low), %K = simple average of raw %K, %D = simple average of %K.',
},
calculate(ctx) {
const { bars, inputs, ta } = ctx;
const highest = ta.highest(bars.map((bar) => bar.high), inputs.kLength);
const lowest = ta.lowest(bars.map((bar) => bar.low), inputs.kLength);
const raw = bars.map((bar, index) => {
const high = highest[index];
const low = lowest[index];
return high === null || low === null || high === low ? null : (100 * (bar.close - low)) / (high - low);
});
const k = ta.sma(raw, inputs.kSmoothing);
const d = ta.sma(k, inputs.dSmoothing);
const band = (value) => bars.map(() => (inputs.showFill ? value : null));
return {
plots: { k, d },
levels: { upper: inputs.upperBand, middle: inputs.middleBand, lower: inputs.lowerBand },
zones: { fill: { upper: band(inputs.upperBand), lower: band(inputs.lowerBand) } },
};
},
};Adapt it with an LLM
Paste this into your assistant and replace the second-to-last line with the change you want.
Start from this Spectral custom indicator: https://spectral.trade/indicators/stochastic.js Format reference: https://spectral.trade/docs/custom-indicators.llm.md Keep the file format and every rule of the reference; give the result a new id, name, shortName and version, and update docs and input descriptions to match. Keep its live updates exact and fast (section "Fast live updates" of the reference): carry values from bar to bar only through ctx.ta, and raise live.lookback if the change reads further back; Spectral rejects a file whose live updates differ from a full recalculation or are too slow. Change I want: <describe the change> Answer with the complete file in one code block, ready to save as <id>.js.


