Indicator · Pine Script v6 · Own pane

Stochastic RSI

by rubbermetal· code credited to claygrace74

Stochastic RSI plots smoothed K and D lines of a stochastic calculation in a separate pane, using smoothed moving average smoothing instead of a plain average.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code88
Settings8inputs
LicenceMPL-2.0
Last update4 months ago10 Jun 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

88 lines of Pine Script v6 from Stochastic RSI.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

Stochastic RSI.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © claygrace74

//@version=6
indicator("Stochastic RSI", shorttitle="StochRSI", format=format.percent, overlay=false)

// ==================== Settings ====================
var stoch_group = "Stochastic RSI Settings"
int periodK = input.int(14, "Stochastic %K Length", minval=1, group=stoch_group)
int smoothK = input.int(3, "%K Smoothing", minval=1, group=stoch_group)
int smoothD = input.int(3, "%D Smoothing", minval=1, group=stoch_group)

var disp_group = "Display Settings"
int rsiob   = input.int(80, "Overbought Level", minval=50, maxval=100, group=disp_group)
int rsios   = input.int(20, "Oversold Level", minval=0, maxval=50, group=disp_group)
bool i_showDiv     = input.bool(true, "Show K-D Divergence Histogram", group=disp_group)
bool i_showSignals = input.bool(true, "Show K/D Cross Signals", group=disp_group)
bool i_showZoneFill = input.bool(true, "Show OB/OS Zone Fills", group=disp_group)

// ==================================================================================
// SMMA Helper
// ==================================================================================
smma(src, length) =>
    sma = ta.sma(src, length)
    smma = 0.0
    smma := na(smma[1]) ? sma : (smma[1] * (length - 1) + src) / length
    smma

// ==================================================================================
// Stochastic RSI Calculation
// ==================================================================================
k = smma(ta.stoch(close, high, low, periodK), smoothK)
d = smma(k, smoothD)
divergence = (math.round(k) - math.round(d)) + 50

// Signals
bullCross = ta.crossover(k, d) and d < rsios
bearCross = ta.crossunder(k, d) and d > rsiob
obEntry   = ta.crossover(k, rsiob)
osEntry   = ta.crossunder(k, rsios)
obExit    = ta.crossunder(k, rsiob)
osExit    = ta.crossover(k, rsios)

// 4-color divergence histogram
divGrowBull  = divergence >= 50 and divergence > divergence[1]
divShrinkBull = divergence >= 50 and divergence <= divergence[1]
divGrowBear  = divergence < 50 and divergence < divergence[1]
divShrinkBear = divergence < 50 and divergence >= divergence[1]
divColor = divGrowBull ? color.rgb(0, 200, 80) : divShrinkBull ? color.rgb(0, 120, 50) : divGrowBear ? color.rgb(200, 30, 30) : color.rgb(120, 40, 40)

// K line color gradient
kColor = color.from_gradient(k, 10, 90, color.green, color.red)

// ==================================================================================
// Grid Layout
// ==================================================================================
h100 = hline(100, "100", color=color.new(color.gray, 80))
hOB  = hline(rsiob, "Overbought", color=color.new(color.red, 50))
hMid = hline(50, "Midline", color=color.new(color.gray, 60))
hOS  = hline(rsios, "Oversold", color=color.new(color.green, 50))
h0   = hline(0, "0", color=color.new(color.gray, 80))

// Zone fills
fill(hOB, h100, color=i_showZoneFill ? color.new(color.red, 90) : color(na), title="OB Zone")
fill(h0, hOS,   color=i_showZoneFill ? color.new(color.green, 90) : color(na), title="OS Zone")

// ==================================================================================
// Plots
// ==================================================================================
plot(i_showDiv ? divergence : na, title='K-D Divergence', linewidth=2, style=plot.style_columns, color=divColor, histbase=50)
plot(k, title='%K', linewidth=2, style=plot.style_line, color=kColor)
plot(d, title='%D', linewidth=2, style=plot.style_line, color=color.rgb(255, 174, 0))

// Cross signals
plotshape(i_showSignals and bullCross, style=shape.triangleup,   location=location.bottom, color=color.green, text="Buy",  textcolor=color.green, size=size.tiny, title="Bull K/D Cross")
plotshape(i_showSignals and bearCross, style=shape.triangledown, location=location.top,    color=color.red,   text="Sell", textcolor=color.red,   size=size.tiny, title="Bear K/D Cross")
plotshape(i_showSignals and osExit,    style=shape.circle, location=location.bottom, color=color.new(color.green, 30), size=size.tiny, title="OS Exit")
plotshape(i_showSignals and obExit,    style=shape.circle, location=location.top,    color=color.new(color.red, 30),   size=size.tiny, title="OB Exit")

// ==================================================================================
// Alert Conditions
// ==================================================================================
alertcondition(bullCross,  "Bullish K/D Cross",    "K crossed above D in oversold zone")
alertcondition(bearCross,  "Bearish K/D Cross",    "K crossed below D in overbought zone")
alertcondition(obEntry,    "Entered Overbought",   "StochRSI K entered overbought territory")
alertcondition(osEntry,    "Entered Oversold",     "StochRSI K entered oversold territory")
alertcondition(osExit,     "Exited Oversold",      "StochRSI K crossed back above oversold")
alertcondition(obExit,     "Exited Overbought",    "StochRSI K crossed back below overbought")

Overview

The K line shifts color from green to red by level. Overbought and oversold zones are filled, K/D cross markers appear when D is in an extreme zone, and an optional histogram shows the K minus D gap. Length, smoothing and levels are adjustable.

Details

Published by

rubbermetal on GitHub

Code credit

claygrace74, as written in the script's header

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Oscillators & momentum

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Stochastic %K Lengthint14periodK
%K Smoothingint3smoothK
%D Smoothingint3smoothD
Overbought Levelint80rsiob
Oversold Levelint20rsios
Show K-D Divergence Histogrambooltruei_showDiv
Show K/D Cross Signalsbooltruei_showSignals
Show OB/OS Zone Fillsbooltruei_showZoneFill
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Asymmetrical RSI

by everget
948 stars

Asymmetrical RSI is an indicator that computes a custom relative strength index from the rate of change of a user-selected source. It plots the resulting ARSI line in a separate pane with color coding that turns green above the overbought level and red below t

Indicator v4 Oscillators & momentum
Updated 11 days ago · GPL-3.0 GitHub repository of Asymmetrical RSI

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop
261 stars

This indicator demonstrates an experimental graph-style scatter visualization within a separate pane using Pine Script arrays. It tracks two features across recent bars: the single-bar price change and a scaled 14-period Relative Strength Index. Values are loa

Indicator v4 Oscillators & momentum
Updated 4 years ago · None stated GitHub repository of Example: Graph Style - 4th Dimension RSI

KD指標

by TaichiS
219 stars

This script calculates and plots a Stochastic Oscillator (KD indicator) in a separate chart pane. It determines the fast stochastic value over a 9-period lookback, then applies a 3-period Simple Moving Average (SMA) to create the %K line, and a second 3-period

Indicator v4 Oscillators & momentum
Updated 5 years ago · None stated GitHub repository of KD指標

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers