Skip to content

Instantly share code, notes, and snippets.

@TraderX0
Last active December 31, 2022 11:08
Show Gist options
  • Save TraderX0/aa67d67f2275cf1fbe28385f52d5a7f7 to your computer and use it in GitHub Desktop.
Save TraderX0/aa67d67f2275cf1fbe28385f52d5a7f7 to your computer and use it in GitHub Desktop.
Pinescript trading view multi time frame RSI - Allows you to plot upto 3 MTF RSI's on one panel
// All credits go to CMoody for the original Idea...
// THE MTFRSI is an extension of his original script to now encorporate 3RSI's on one panel
study(title="TraderX0 3-RSI MTF", shorttitle="X0_3_RSI_MTF", precision=0)
//candle source
src = close
//rsi setup
//first RSI inputs
firstRsiTimeframe = input(title="Select 1st RSI Timeframe", type=resolution, defval="60")
len = input(14, minval=1, title="1st RSI Length")
//second RSI inputs
isDisplaySecondRsi = input(false, title="Show 2nd RSI?")
secondRsiTimeframe = input(title="Select 2nd RSI Timeframe? Check Box Above", type=resolution, defval="240")
len2 = input(14, minval=1, title="2nd RSI Length")
//third RSI inputs
isDisplayThirdRsi = input(false, title="Show 3rd RSI?")
thirdRsiTimeframe = input(title="Select 3rd RSI Timeframe? Check Box Above", type=resolution, defval="D")
len3 = input(14, minval=1, title="3rd RSI Length")
//AREA FOR DISPLAYING RSI PROPERTIES
// oversold and overbought lines
upLine = input(70, minval=50, maxval=90, title="Upper Line Value?")
lowLine = input(30, minval=10, maxval=50, title="Lower Line Value?")
sml = input(false, title="Display Mid Line?")
sbh = input(false, title="Highlight When 1st RSI is Above/Below High/Low Lines?")
sch = input(false, title="Highlight When 1st RSI Cross?")
//calculate 1st rsi values
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))
outRSI = security(tickerid, firstRsiTimeframe, rsi)
//calculate 2nd rsi values
up2 = rma(max(change(src), 0), len2)
down2 = rma(-min(change(src), 0), len2)
rsi2 = down2 == 0 ? 100 : up2 == 0 ? 0 : 100 - (100 / (1 + up2 / down2))
outRSI2 = security(tickerid, secondRsiTimeframe, rsi2)
//calculate 3rd rsi values
up3 = rma(max(change(src), 0), len3)
down3 = rma(-min(change(src), 0), len3)
rsi3 = down3 == 0 ? 100 : up2 == 0 ? 0 : 100 - (100 / (1 + up3 / down3))
outRSI3 = security(tickerid, thirdRsiTimeframe, rsi3)
//TODO NEED TO RE-WRITE THIS SECTION FOR BETTER LOGIC - CHANGE THIS AS YOU WISH
//calculate rsi display logic - below is key to indicating buy or sell
aboveLine = outRSI > upLine ? 1 : 0
belowLine = outRSI < lowLine ? 1 : 0
crossUp = outRSI[1] < lowLine and outRSI > lowLine ? 1 : 0
crossDn = outRSI[1] > upLine and outRSI < upLine ? 1 : 0
//Get ui logic from calculated rsi condistions
bgcolor(sbh and aboveLine ? red : na, transp=70)
bgcolor(sbh and belowLine ? green : na, transp=70)
bgcolor(sch and crossUp ? lime : na, transp=40)
bgcolor(sch and crossDn ? red : na, transp=40)
//plot first rsi line
plot(outRSI, title="RSI", style=line, linewidth=2, color=blue)
//plot second rsi line if selected
plot(isDisplaySecondRsi and outRSI2 ? outRSI2 : na, title="2nd RSI - Different Time Frame?", style=line, linewidth=2, color=purple)
//plot third rsi line if selected
plot(isDisplayThirdRsi and outRSI3 ? outRSI3 : na, title="3rd RSI - Different Time Frame?", style=line, linewidth=2, color=green)
//plots the upper and lower lines and mid lines
p1 = plot(upLine, title= "Upper Line", style=solid, linewidth=2, color=black)
p2 = plot(lowLine, title= "Lower Line", style=solid, linewidth=2, color=black)
plot(sml and 50 ? 50 : na, title="Mid Line", style=line, linewidth=1, color=black)
//EOF
@webinger
Copy link

webinger commented Mar 9, 2020

I tried your code and when I select "Same as Symbol" for the first RSI it says Invalid value for "resolution". Do you know how to fix that?

@bugsbyteyou
Copy link

bugsbyteyou commented May 21, 2020

Hi TraderX0, I'm confused as to why you decided to use the same close timeseries for all the different timeframes? Is it so you use can use closer to realtime values for the higher time frames ? Thanks for the help.

@kkairu
Copy link

kkairu commented Dec 31, 2022

But why are you calculating RSI manually? Just wondering...

rp_security(_symbol, _res, _src) =>
request.security(_symbol, _res, _src[barstate.isrealtime ? repainting ? 0 : 1 : 0], barmerge.gaps_off, barmerge.lookahead_on)

rsi = rp_security(syminfo.tickerid, tf, ta.rsi(src, length))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment