Indicator · Pine Script v5 · Own pane

RSI & Stochastic

by tkittich

The script plots the Relative Strength Index and Stochastic oscillator indicators in separate panes.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code68
Settings10inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago20 Apr 2022

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

68 lines of Pine Script v5 from sto & rsi.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

sto & rsi.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// RSI & Stochastic. Same as the built-in scripts with changing color when overbought or oversold.

//@version=5

indicator(title="RSI & Stochastic", shorttitle="RSI & Sto", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

///////////////
// RSI
gRSI   = "RSI Settings"
gRSIMA = "RSI MA Settings"
showRSI = input(true, title="Show RSI", group=gRSI)

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

rsiLengthInput = input.int(14, minval=1, title="RSI Length", group=gRSI)
rsiSourceInput = input.source(close, "Source", group=gRSI)

maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group=gRSIMA)
maLengthInput = input.int(14, title="MA Length", group=gRSIMA)
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group=gRSIMA)

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands" and showRSI

rsiColor = rsi > 70 or rsi < 30 ? color.yellow : color.purple

plot(showRSI ? rsi : na, "RSI", color=rsiColor, linewidth=2)
plot(showRSI ? rsiMA : na, "RSI-based MA", color=color.gray)

rsiUpperBand = hline(showRSI ? 70 : na, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(showRSI ? 30 : na, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 95), title="RSI Background")

bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper Bollinger Band", color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 95) : na, title="Bollinger Bands Background")

///////////////
// Stochastic
gSto  = "Stochastic Settings"
showSto = input(true, title="Show Stochastic", group=gSto)

periodK = input.int(14, title="%K Length", minval=1, group=gSto)
smoothK = input.int(1, title="%K Smoothing", minval=1, group=gSto)
periodD = input.int(3, title="%D Smoothing", minval=1, group=gSto)

k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)

stoColor = k > 80 or k < 20 ? color.orange : color.blue

plot(showSto ? k : na, title="%K", color=stoColor)
plot(showSto ? d : na, title="%D", color=color.gray)
h0 = hline(showSto ? 80 : na, "Stochastic Upper Band", color=#787B86)
h1 = hline(showSto ? 20 : na, "Stochastic Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(33, 150, 243, 95), title="Stochastic Background")

Overview

RSI & Stochastic calculates the RSI with customizable length, source, and MA type including Bollinger Bands, and the Stochastic oscillator with %K and %D periods. It draws overbought and oversold levels at 70/30 for RSI and 80/20 for Stochastic, along with background fills. The main settings change the lengths and MA type for RSI as well as the smoothing periods for Stochastic. It signals overbought conditions above 70 or 80 and oversold below 30 or 20. No entry or exit rules exist since it is an indicator only. It draws on the price chart without repainting or lookahead.

Details

Published by

tkittich on GitHub

Type

Indicator, Pine Script v5

Draws

Separate pane below the chart

Category

Oscillators & momentum, Volatility & bands

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
Show RSIbooltrueshowRSI
RSI Lengthint14rsiLengthInput
SourcesourceclosersiSourceInput
MA TypestringSMAmaTypeInput
MA Lengthint14maLengthInput
BB StdDevfloat2.0bbMultInput
Show StochasticbooltrueshowSto
%K Lengthint14periodK
%K Smoothingint1smoothK
%D Smoothingint3periodD
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

TTM Squeeze

by Alorse
330 stars

This indicator plots a TTM Squeeze momentum oscillator in a separate pane. It compares a Bollinger Band offset to a Keltner Channel offset and marks the zero line white when the difference is non-negative and black when the bands are inside the channel. A line

Indicator v5 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of TTM Squeeze

Function - Logistic Equation

by ricardosantos79
261 stars

Calculates dynamic modeling values by applying a mathematical logistic differential equation to price change over a specified lookback length. It measures price momentum relative to the recent high-low price range. The script plots positive and negative logist

Indicator v5 Oscillators & momentumVolatility & bands
Updated 4 years ago · None stated GitHub repository of Function - Logistic Equation

MACD-V

by harryguiacorn
219 stars

MACD-V is a volatility-normalized momentum indicator based on research by Alex Spiroglou. It calculates the standard MACD histogram and normalizes it by dividing by the Average True Range (ATR). Plotted in a separate pane as a colored histogram, it includes us

Indicator v5 Oscillators & momentumVolatility & bands
Updated 1 year ago · None stated GitHub repository of MACD-V
218 stars

This indicator detects a Bollinger Band squeeze when the bands sit inside Keltner Channels, marking low volatility. It plots red dots during a squeeze and green dots when the bands are outside, in a separate pane. An optional histogram shows momentum as the cl

Indicator v6 Volatility & bandsOscillators & momentum
Updated 3 months ago · None stated GitHub repository of Bollinger Band Squeeze Detector
147 stars

This indicator generates trade entry signals and target levels optimized for 1-hour timeframes, designed for external signal extraction. It calculates 7-period and 16-period EMAs alongside 10-period RSI and ATR filters. Long signals occur when the fast EMA cro

Indicator v5 TrendOscillators & momentum
Updated 6 weeks ago · MIT GitHub repository of Signal Strategy 100 Candles of 1H Timeframe

Bubbles Indicator

by capissimo
114 stars

The Bubbles Indicator measures volatility squeezes and expansion bursts using Bollinger Bands combined with a sinusoid oscillator and RSI divergence. It processes price data through scaled Bollinger Bands to highlight volatility changes while confirming trend

Indicator v4 Volatility & bandsOscillators & momentum Repaints
Updated 1 year ago · None stated GitHub repository of Bubbles Indicator

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