TradingView indicator guides

Stochastic Oscillator: Raw K, Smoothing and Range Changes

The Stochastic Oscillator locates the close within a rolling high-low range. This example uses a 14-bar range, one-bar K smoothing and a three-bar SMA signal. Its 80 and 20 references describe range position, not reversal probabilities or automatic trade instructions.

Inspect the Pine Script

Keep the three calculation stages separate

TradingView’s Stochastic guide documents the range-position calculation and smoothing controls. In this implementation, R = 100 × (close − lowest low) / (highest high − lowest low), K = SMA(R, k_smoothing), and D = SMA(K, d_smoothing). Both extremes include the current bar. TradingView’s documented built-in defaults use K smoothing 3; this copied example uses 1. Match that setting when comparing the plots.

With valid OHLC data and a positive range, raw R is between 0 and 100. R = 100 means close equals the window high; R = 0 means close equals its low. R = 80 lies exactly four fifths of the way from low to high. It is not a percentile rank of the closes or an 80% probability.

Only unsmoothed K directly describes the current close’s range position. With three-bar K smoothing, raw observations 100, 100 and 50 produce K = 83.333333 even though the current close is at its window midpoint. If highest high equals lowest low, the displayed fraction is undefined; the code delegates that case to the built-in and supplies no custom neutral-value fallback.

Reproduce the smoothing without market assumptions

Use the hypothetical raw sequence 20, 80, 50, 100, 0. With K smoothing 3, the first three available K values are 50, 76.666667 and 50. A three-value D first becomes 58.888889 from their average. At that point the raw value is 0, K is 50, and D is 58.888889: the labels cannot be used interchangeably.

With this page’s K smoothing 1, K instead equals each raw value. D becomes 50, 76.666667 and 50 on raw observations 3, 4 and 5. Changing K smoothing changes both plotted lines; changing only D smoothing leaves K unchanged. Setting D smoothing to 1 makes D equal K on valid observations, so there are no strict K/D crossings.

These are arithmetic examples, not tested parameter recommendations. SMA smoothing uses available valid observations according to Pine’s documented functions; the snippet has no separate full-history or signal warmup gate. Compare the same loaded history and inspect missing values before judging two implementations.

An oscillator rise does not require a rising close

For a three-bar range, consider these four hypothetical high/low/close bars in order: 110/90/100, 104/95/100, 103/90/100, and 102/96/99. At the third bar the range is 90 to 110 and raw K is 50. At the fourth, the old high of 110 leaves the window; the range becomes 90 to 104 and raw K becomes 100 × 9/14 = 64.285714.

Price fell from 100 to 99 while raw K rose. The changing reference window explains the result; it is not evidence of buying pressure or a price acceleration. Likewise, a narrow nonzero range can produce a large oscillator move from a small price change.

A positive rescaling of every high, low and close preserves raw K. Higher absolute volatility does not, by itself, prove that crypto needs a particular length or threshold. Fourteen five-minute bars cover fourteen chart observations, not the same sampling interval as fourteen daily bars. No universal conversion of 7 hourly bars into 14 daily bars preserves this calculation.

Compare Williams %R, crossings and divergence precisely

Williams %R uses −100 × (highest high − close) / (highest high − lowest low). With identical valid ranges, %R = raw K − 100. Raw K at 80 maps to %R at −20; raw K at 20 maps to −80. They move in the same direction and cross corresponding thresholds on the same bars. Smoothing K can create differences, but a shifted scale alone creates no extra sensitivity.

RSI instead compares smoothed gains and losses of its source. That is a different calculation, not proof that either indicator has a higher reversal success rate or a fixed lead.

A bullish K/D crossing requires current K > D and previous K ≤ previous D. Being above 80, crossing into that zone, and crossing back out are separate events. On a daily chart, a specified closed-bar crossing condition yields at most one event per daily bar; intrabar fluctuations must not be presented as several independent confirmed daily signals.

Regular bullish divergence pairs a lower price low with a higher oscillator low. Hidden bullish divergence pairs a higher price low with a lower oscillator low. Both require an explicit pivot definition and confirmation timing. The supplied script does not detect either. Divergence or a crossing does not guarantee the next price move.

Use the copied code within its actual scope

The snippet calls p_ta_stoch(14, 1, 3), plots blue K and orange D, draws 80/50/20 reference lines and fills the 20–80 region. It contains no input declarations, crossing alerts, divergence markers, strategy orders or bar-close gate. Edit the source or request explicit inputs to change these values; they do not appear in an Inputs panel automatically.

The current high, low and close can change intrabar, so both lines may move. If adding alerts, specify whether a crossing must survive bar close. If building a strategy, define entry and exit timing, sizing, stops and costs separately, then evaluate independent data. Reference zones and additional EMA or MACD filters do not establish profitability.

Pine Script v6: implementation and limits

The full source retains fixed 14/1/3 parameters and the two SMA smoothing stages. It does not implement the trade, alert or divergence rules discussed as possible extensions.

Pine Script v6
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pineify

//@version=6
indicator(title="Stochastic Oscillator", overlay=false, max_labels_count=500)

// Stochastic Calculation
// %K = ta.stoch(close, high, low, k_length)
// %D = ta.sma(%K, d_smoothing)
p_ta_stoch(simple int k_length, simple int k_smoothing, simple int d_smoothing) =>
    k = ta.sma(ta.stoch(close, high, low, k_length), k_smoothing)
    d = ta.sma(k, d_smoothing)
    [k, d]

[p_ind_1_k, p_ind_1_d] = p_ta_stoch(14, 1, 3)

// Stochastic Lines
plot(p_ind_1_k, "STOCH - K", color.rgb(41, 98, 255, 0), 1)
plot(p_ind_1_d, "STOCH - D", color.rgb(255, 109, 0, 0), 1)

// Overbought, Middle, and Oversold Levels
p_ind_1_k_h0 = hline(80, "STOCH - Upper Band", color=#787B86)
hline(50, "STOCH - Middle Band", color=color.new(#787B86, 50))
p_ind_1_k_h1 = hline(20, "STOCH - Lower Band", color=#787B86)
fill(p_ind_1_k_h0, p_ind_1_k_h1, color=color.rgb(33, 150, 243, 90), title="STOCH - Background")
Existing indicator chart preview
Existing visual preview. Verify exact values against the source; the image is not a performance result.

Specify a checkable adaptation in Pineify

Ask the Pine Script AI Coding Agent for explicit K length, K smoothing and D smoothing inputs, plus any precisely defined confirmed-bar alerts. Review the generated source and compare intermediate raw K, smoothed K and D values in TradingView. Check current product access; code generation does not supply tested returns.

Open Pine Script AI Coding Agent

Educational indicator analysis. No measured trading performance is established here. Syntax validation does not prove formula equivalence, execution assumptions or profitability.

Implementation questions

That direct interpretation applies to raw K with no additional smoothing. A smoothed K can remain above 80 while the current raw value is lower.

Yes. The four-bar example removes an old window high, raising raw K from 50 to 64.285714 while close falls from 100 to 99.

With identical ranges and no K smoothing, Williams %R equals raw K minus 100. Corresponding threshold events occur on the same bars; a scale shift creates no lead.

No input declarations are present. Edit the fixed call or add inputs explicitly. The provided code only plots the indicator.

No. These are specified calculation events. A trading hypothesis requires independently evaluated rules, execution assumptions and costs.