Skip to content
GetProfitable
Search
Momentum

Stochastic Crossover

Smoothed stochastic with the overbought and oversold bands drawn and shaded, marking only the crossovers that happen inside an extreme rather than every crossover in the middle of the range.

indicator
//@version=6
indicator("Stochastic Crossover", "Stoch X")

kLen = input.int(14, "K length", minval = 1, group = "Settings")
kSmooth = input.int(3, "K smoothing", minval = 1, group = "Settings")
dSmooth = input.int(3, "D smoothing", minval = 1, group = "Settings")
upper = input.int(80, "Overbought", minval = 51, maxval = 99, group = "Settings")
lower = input.int(20, "Oversold", minval = 1, maxval = 49, group = "Settings")
bull = input.color(#26A69A, "Bullish cross", group = "Style")
bear = input.color(#EF5350, "Bearish cross", group = "Style")
lineCol = input.color(#787B86, "Lines", group = "Style")

raw = ta.stoch(close, high, low, kLen)
k = ta.sma(raw, kSmooth)
d = ta.sma(k, dSmooth)

top = hline(upper, "Overbought", color = color.new(lineCol, 50))
bottom = hline(lower, "Oversold", color = color.new(lineCol, 50))
fill(top, bottom, color = color.new(lineCol, 93), title = "Band")
plot(k, "K", color = lineCol, linewidth = 2)
plot(d, "D", color = color.new(lineCol, 45))

bullCross = ta.crossover(k, d) and k < lower
bearCross = ta.crossunder(k, d) and k > upper
plotshape(bullCross, "Bull cross", location = location.bottom, style = shape.triangleup, color = bull, size = size.tiny)
plotshape(bearCross, "Bear cross", location = location.top, style = shape.triangledown, color = bear, size = size.tiny)
alertcondition(bullCross, "Crossed up while oversold", "The stochastic K line crossed above D while below the oversold level")
alertcondition(bearCross, "Crossed down while overbought", "The stochastic K line crossed below D while above the overbought level")
This runs in TradingView, not here
Pine Script only executes inside TradingView. Paste the source into the Pine Editor and add it to a chart to see it plotted.

What it will not do

  • The stochastic is bounded, so in a strong trend it pins near an extreme and keeps issuing crosses against the direction that is actually paying.
  • Restricting crosses to the extremes cuts noise but also drops the crosses that start a trend from the middle of the range.
  • Smoothing makes the two lines steadier and later. The settings decide how much lag you accept for how few false crosses.

Written from this description

Plot a stochastic with configurable K length and smoothing, shade the area between the 80 and 20 bands, and mark the bars where K crosses D while below 20 or above 80.

Educational only, not financial advice. The maths is simple arithmetic on the numbers you enter; it knows nothing about your broker, fees, slippage or the market. Something wrong with it? Say so in Site Feedback.