Skip to content

Instantly share code, notes, and snippets.

@d0rsha
Last active April 24, 2023 14:36
Show Gist options
  • Save d0rsha/a0f76cdf7f7ca639e5d5837e282f560b to your computer and use it in GitHub Desktop.
Save d0rsha/a0f76cdf7f7ca639e5d5837e282f560b to your computer and use it in GitHub Desktop.
[Pinescript RSI + Stoch] A pinescript for tradingview indicator RSI + stoch #pinescript
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © d0rsha
// Set pine version
//@version=4
/////////////////
// RSI
study(title="RSI + stoch", shorttitle="RSI", format=format.price, precision=2, resolution="")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=color.white)
/////////////////
// Setup horizontal lines.
hline(70, "Bullish High", color=color.red, linestyle=hline.style_solid)
hline(40, "Bullish Low", color=color.red, linestyle=hline.style_solid)
hline(50, "Mid", color=color.green, linestyle=hline.style_solid)
hline(60, "Bearish High", color=color.blue, linestyle=hline.style_solid)
hline(30, "Bearish Low", color=color.blue, linestyle=hline.style_solid)
//////////////////
// Stochastic RSI
smoothK = input(5, "K", minval=1)
smoothD = input(3, "D", minval=1)
lengthRSI = input(14, "RSI Length", minval=1)
lengthStoch = input(14, "Stochastic Length", minval=1)
src2 = input(close, title="RSI Source")
rsi1 = rsi(src2, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
// Can't set opacity with color(<HEX_COLOR>, <Opacity>)... ;(
plot(k, "K", color=color(#0094FF))
plot(d, "D", color=color(#FF6A00))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment