Master this essential momentum oscillator in TradingView's Pine Script for identifying overbought/oversold conditions, trend strength, and reversals.
The Stochastic Oscillator, developed by George C. Lane, is a momentum indicator that shows the location of the closing price relative to the high-low range over a set number of periods. Unlike RSI, which measures the speed of price changes, Stochastic measures the *strength of a close* in relation to its price range. It consists of two lines: %K (the main line) and %D (the signal line, which is a smoothed version of %K).
Stochastic %K and %D oscillate between 0 and 100. They are particularly effective at identifying overbought and oversold conditions, as well as divergence from price, making them a crucial tool for anticipating reversals.
The Stochastic Oscillator's calculation involves a few key components:
%K = ((Current Close - Lowest Low) / (Highest High - Lowest Low)) * 100%D = Simple Moving Average of %K over a specified smoothing period (e.g., 3 periods).//@version=5
indicator("My Stochastic Oscillator", overlay=false)
// Inputs for Stochastic parameters
kLength = input.int(14, title="%K Length", minval=1)
dLength = input.int(3, title="%D Smoothing", minval=1)
smoothing = input.int(3, title="%K Smoothing", minval=1)
// Calculate Stochastic %K and %D values using the built-in function
[kValue, dValue] = ta.stoch(close, high, low, kLength, smoothing, dLength)
// Plot the %K line
plot(kValue, title="%K", color=color.blue, linewidth=2)
// Plot the %D line
plot(dValue, title="%D", color=color.orange, linewidth=2)
// Plot horizontal lines for overbought and oversold levels
h_overbought = hline(80, "Overbought", color=color.red)
h_oversold = hline(20, "Oversold", color=color.green)
// Fill background between Stochastic lines and levels for visual clarity
fill(h_overbought, h_oversold, color.new(color.gray, 90))
The conventional overbought level is 80 and the oversold level is 20 for Stochastic.
When %K and %D enter the overbought region (above 80) or oversold region (below 20), it signals potential turning points.
//@version=5
strategy("Stochastic Overbought/Oversold Strategy", overlay=true)
kLength = input.int(14, title="%K Length", minval=1)
dLength = input.int(3, title="%D Smoothing", minval=1)
smoothing = input.int(3, title="%K Smoothing", minval=1)
overboughtLevel = input.int(80, title="Overbought Level")
oversoldLevel = input.int(20, title="Oversold Level")
[kValue, dValue] = ta.stoch(close, high, low, kLength, smoothing, dLength)
longCondition = ta.crossover(kValue, dValue) and dValue < oversoldLevel
shortCondition = ta.crossunder(kValue, dValue) and dValue > overboughtLevel
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
Crossovers between the %K and %D lines are frequent signals, indicating shifts in momentum.
//@version=5
strategy("Stochastic Crossover Strategy", overlay=true)
kLength = input.int(14, title="%K Length", minval=1)
dLength = input.int(3, title="%D Smoothing", minval=1)
smoothing = input.int(3, title="%K Smoothing", minval=1)
[kValue, dValue] = ta.stoch(close, high, low, kLength, smoothing, dLength)
longSignal = ta.crossover(kValue, dValue)
shortSignal = ta.crossunder(kValue, dValue)
if (longSignal)
strategy.entry("Long", strategy.long)
if (shortSignal)
strategy.entry("Short", strategy.short)
Divergence occurs when price and Stochastic move in opposite directions, often signaling a strong potential trend reversal.
//@version=5
indicator("Stochastic Divergence Scanner", overlay=true)
kLength = input.int(14, title="%K Length", minval=1)
dLength = input.int(3, title="%D Smoothing", minval=1)
smoothing = input.int(3, title="%K Smoothing", minval=1)
overboughtLevel = input.int(80, title="Overbought Level")
oversoldLevel = input.int(20, title="Oversold Level")
[kValue, dValue] = ta.stoch(close, high, low, kLength, smoothing, dLength)
plot(kValue, "%K", color.blue)
plot(dValue, "%D", color.orange)
hline(overboughtLevel, "Overbought", color.red)
hline(oversoldLevel, "Oversold", color.green)
hline(50, "Centerline", color.gray)
bullishDivergence = close[2] > close[1] and close[1] > close and dValue[2] < dValue[1] and dValue[1] < dValue
bearishDivergence = close[2] < close[1] and close[1] < close and dValue[2] > dValue[1] and dValue[1] > dValue
plotshape(bullishDivergence, title="Bullish Divergence", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(bearishDivergence, title="Bearish Divergence", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
alertcondition(bullishDivergence, "Bullish Stochastic Divergence", "Potential bullish reversal based on Stochastic divergence.")
alertcondition(bearishDivergence, "Bearish Stochastic Divergence", "Potential bearish reversal based on Stochastic divergence.")
To get the most from the Stochastic Oscillator in Pine Script:
While both are momentum oscillators, Stochastic focuses on the price's position relative to its recent range, whereas RSI focuses on the speed of price changes. They can complement each other well.
The Stochastic Oscillator (%K and %D) is a fundamental and highly versatile momentum indicator in Pine Script for TradingView. Its ability to show the current closing price relative to its recent range makes it invaluable for identifying overbought and oversold conditions, assessing momentum, and spotting potential reversals through divergence. By understanding its calculation, thoughtfully tuning its parameters, and integrating it strategically with other technical analysis tools, you can leverage the Stochastic Oscillator to gain deeper insights into market dynamics and enhance your trading strategies.
Get a high-performance Pine Script analysis tool for actionable market insights, designed for traders on the move.
This strategy runs in live mode on TradingView, helping you identify potential opportunities.
Get Pine Script Strategy