Momentum

Stochastic Buy Sell Signals: K/D Crosses and Divergence

Understand Stochastic K/D crosses, overbought and oversold states, and price-pivot divergence with working Pine Script v6 and explicit confirmation rules.

Stochastic Signal Interpretation

Quick-reference guide to 8 signal types you will see on the chart.

Signal TypeIndicator ConditionMarket MeaningInterpretation limits
Oversold bullish crossK crosses above D, with both lines below the lower threshold at the confirmed close.K has moved above its signal average while both remain near the lower end of their scale.A cross that finishes outside the zone is excluded, even if it began inside.
Overbought bearish crossK crosses below D, with both lines above the upper threshold at the confirmed close.K has moved below its signal average while both remain high on their scale.No price reversal or divergence is required by this condition.
Overbought stateBoth K and D are above the upper threshold on a closed bar.The smoothed close position is high in its recent range.Red background is a continuing state, not an entry alert.
Oversold stateBoth K and D are below the lower threshold on a closed bar.The smoothed close position is low in its recent range.Green background can persist through a downtrend.
Regular bullish divergenceSuccessive confirmed price lows are lower while K sampled at those lows is higher.The price swing and oscillator reading disagree.No oversold filter or independently detected K pivot is required.
Regular bearish divergenceSuccessive confirmed price highs are higher while sampled K is lower.The higher price high has a lower oscillator reading.Reported only after the right-hand price bars confirm.
Hidden bullish divergenceSuccessive price lows are higher while sampled K is lower.A higher price low coincides with a deeper oscillator pullback.This names a common divergence convention; no wider uptrend or continuation is established.
Hidden bearish divergenceSuccessive price highs are lower while sampled K is higher.A lower price high coincides with a stronger oscillator reading.The label does not imply a profitable short or detect a complete trend setup.

Stochastic Pine Script Signal Code

Educational indicator example. Review its exact rules and compare its plots with your intended calculation in TradingView. It does not place orders or establish profitability.

Pine Script v6
//@version=6
indicator("Stochastic Cross and Pivot Signals", overlay=false)
kPeriod = input.int(14, "Range Length", minval=1)
kSmooth = input.int(3, "K Smoothing", minval=1)
dSmooth = input.int(3, "D Smoothing", minval=1)
ob = input.float(80, "Upper Threshold", minval=50.1, maxval=100)
os = input.float(20, "Lower Threshold", minval=0, maxval=49.9)
pivotBars = input.int(2, "Pivot Bars Each Side", minval=1, maxval=20)
rawK = ta.stoch(close, high, low, kPeriod)
k = ta.sma(rawK, kSmooth)
d = ta.sma(k, dSmooth)
crossUp = ta.crossover(k, d)
crossDown = ta.crossunder(k, d)
bullCross = barstate.isconfirmed and crossUp and k < os and d < os
bearCross = barstate.isconfirmed and crossDown and k > ob and d > ob
lowState = barstate.isconfirmed and k < os and d < os
highState = barstate.isconfirmed and k > ob and d > ob
priceLow = ta.pivotlow(low, pivotBars, pivotBars)
priceHigh = ta.pivothigh(high, pivotBars, pivotBars)
newLow = not na(priceLow)
newHigh = not na(priceHigh)
priorLow = ta.valuewhen(newLow, priceLow, 1)
priorLowK = ta.valuewhen(newLow, k[pivotBars], 1)
priorHigh = ta.valuewhen(newHigh, priceHigh, 1)
priorHighK = ta.valuewhen(newHigh, k[pivotBars], 1)
bullDiv = barstate.isconfirmed and newLow and priceLow < priorLow and k[pivotBars] > priorLowK
bearDiv = barstate.isconfirmed and newHigh and priceHigh > priorHigh and k[pivotBars] < priorHighK
hiddenBull = barstate.isconfirmed and newLow and priceLow > priorLow and k[pivotBars] < priorLowK
hiddenBear = barstate.isconfirmed and newHigh and priceHigh < priorHigh and k[pivotBars] > priorHighK
plot(k, "K", color.blue, 2)
plot(d, "D", color.orange)
hline(ob, "Upper", color.red)
hline(os, "Lower", color.green)
bgcolor(highState ? color.new(color.red, 90) : lowState ? color.new(color.green, 90) : na)
plotshape(bullCross, "Bull Cross", shape.triangleup, location.bottom, color.green)
plotshape(bearCross, "Bear Cross", shape.triangledown, location.top, color.red)
// Pivot markers appear on the confirmation bar, without backdating.
plotshape(bullDiv, "Regular Bull", shape.labelup, location.bottom, color.teal, text="REG")
plotshape(bearDiv, "Regular Bear", shape.labeldown, location.top, color.orange, text="REG")
plotshape(hiddenBull, "Hidden Bull", shape.labelup, location.bottom, color.lime, text="HID")
plotshape(hiddenBear, "Hidden Bear", shape.labeldown, location.top, color.fuchsia, text="HID")
alertcondition(bullCross, "Stochastic Oversold Cross", "K crossed above D with both below the lower threshold")
alertcondition(bearCross, "Stochastic Overbought Cross", "K crossed below D with both above the upper threshold")
alertcondition(bullDiv, "Stochastic Regular Bull", "Confirmed regular bullish price-pivot divergence")
alertcondition(bearDiv, "Stochastic Regular Bear", "Confirmed regular bearish price-pivot divergence")
alertcondition(hiddenBull, "Stochastic Hidden Bull", "Confirmed hidden bullish price-pivot divergence")
alertcondition(hiddenBear, "Stochastic Hidden Bear", "Confirmed hidden bearish price-pivot divergence")

Example settings for Stochastic

Record the inputs used by the example. These settings are not an optimized trading recommendation.

Feature comparison table: Default vs Description
ParameterDefaultDescription
Range Length14Bars used for the highest high and lowest low around the current close.
K Smoothing3SMA length applied to the single raw series returned by ta.stoch. A value of 1 leaves raw K unsmoothed.
D Smoothing3SMA length applied to the already smoothed K line, not independently to raw K.
Upper / Lower Threshold80 / 20Both lines must be strictly inside the selected zone for its cross signal. Equality does not qualify.
Pivot Bars Each Side2Price-pivot confirmation uses two bars on each side by default. Changing this changes swing selection and reporting delay.

Turn the condition into an explicit rule

Specify the calculation, confirmation time, entry and exit rules before asking Pineify to draft or revise Pine Script. Review the code and simulate realistic costs before evaluating a strategy.

Explore Pine Script coding

FAQ

Stochastic Signals FAQ

What does 14,3,3 mean?
Raw Stochastic is 100 times the close minus the 14-bar low, divided by the 14-bar high minus low. K averages raw Stochastic over three bars, and D averages that K over three bars. For a hypothetical high of 110, low of 100 and close of 108, raw Stochastic is 80; the displayed K and D still depend on preceding bars.
Why does the code assign ta.stoch to one variable?
ta.stoch returns one raw oscillator series. It does not return a K/D tuple. The code constructs K with an SMA of that result, then constructs D with another SMA of K. This ordering is necessary for the stated 14,3,3 calculation.
Why did a K/D cross produce no marker?
The zone filter is evaluated at the confirmed bar close. For example, previous K/D of 15/17 followed by 19/18 qualifies as an oversold bullish cross with a threshold of 20. A cross ending at 21/19 does not. Crosses outside the two selected zones are deliberately omitted.
How are divergence signals confirmed?
The script compares consecutive confirmed price pivots and samples K at each pivot bar. With two right-hand bars, the second swing becomes available two bars later. Markers remain on that later confirmation bar. Hidden divergence labels use higher-price-low/lower-K and lower-price-high/higher-K comparisons; they are not the separate bull/bear setup definitions used in some Stochastic literature.
Can Stochastic readings change before the close?
Yes. The current high, low and close can all change, so K and D can change during the bar. The example gates conditions at bar close. Feed corrections and setting changes can still alter historical values; a changed result is not by itself proof that a script uses future data.
Which settings or chart interval perform best?
This page provides no measured accuracy ranking. More smoothing averages over more observations, but it does not guarantee fewer losing trades. Compare complete entry and exit rules with costs on independent data. Trend filters also change which trades are selected and require their own evaluation.
Can a Stochastic cross determine an options trade?
The example describes the charted price series. It does not model an option contract, expiration, strike, implied volatility, spread or exercise terms. A directional event on an underlying asset cannot establish the return of a particular call or put.