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

//@version=5
indicator(title="Multi-timeframe Stochastic RSI", shorttitle="sRSI", format=format.price, precision=0, timeframe="", timeframe_gaps=false)

//user options
source = input.source(defval=close, title="Source")
SRSI_bull_cutoff = input.int(defval=25, title="Stochastic RSI oversold cutoff", minval=0, maxval=100)
SRSI_bear_cutoff = input.int(defval=75, title="Stochastic RSI overbought cutoff", minval=0, maxval=100)
lowthreshold = input.int(defval=3, title="low bandpass", minval=0, maxval=22)
highthreshold = input.int(defval=19, title="high bandpass", minval=0, maxval=22)

// Use only time-frames available on the free tradingview sub
freecheck = input.bool(defval=false, title="I have free TV")

//select sRSI to view manually
sRSIoverride = input.bool(defval=false, title="Manual sRSI")
lowtime = input.timeframe('5', "sRSI 1", options = ['1','3','5','15','30','45','60','120','240','480','D','2D','W','2W','M','2M','4M'])
midlowtime = input.timeframe('15', "sRSI 2", options = ['1','3','5','15','30','45','60','120','240','480','D','2D','W','2W','M','2M','4M'])
midhightime = input.timeframe('30', "sRSI 3", options = ['1','3','5','15','30','45','60','120','240','480','D','2D','W','2W','M','2M','4M'])
hightime = input.timeframe('60', "sRSI 4", options = ['1','3','5','15','30','45','60','120','240','480','D','2D','W','2W','M','2M','4M'])

tp1 = 10
tp2 = 50
tp3 = 100
tp4 = 100
if (sRSIoverride == true)
    tp1 := 100
    tp2 := 100
    tp3 := 10
    tp4 := 50

// Color Selection from drop down
string colorscheme = input.string("Tron", "Color Scheme", options = ["Classic", "Rainbow", "Tron", "Blue"])

// functions
k(_res) =>
    request.security(syminfo.tickerid, _res,  ta.sma(ta.stoch(ta.rsi(source, 14), ta.rsi(source, 14), ta.rsi(source, 14), 14), 3)[barstate.isrealtime ? 1 : 0], gaps=barmerge.gaps_off)

d(_res) =>
    request.security(syminfo.tickerid, _res,  ta.sma(ta.sma(ta.stoch(ta.rsi(source, 14), ta.rsi(source, 14), ta.rsi(source, 14), 14), 3), 3)[barstate.isrealtime ? 1 : 0], gaps=barmerge.gaps_off)

//get Timeframe of current chart and convert to a score for timeframe detection based switches
timeframe = timeframe.period
framescore = 0
framescore := if (timeframe == "M")
    13
else if (timeframe == "W")
    12
else if (timeframe == "D")
    11
else if (timeframe == "480")
    10
else if (timeframe == "240")
    9
else if (timeframe == "180")
    8
else if (timeframe == "120")
    7
else if (timeframe == "60")
    6
else if (timeframe == "30")
    5
else if (timeframe == "15")
    4
else if (timeframe == "5")
    3
else if (timeframe == "3")
    2
else if (timeframe == "1")
    1
else
    0
    
//Initialize time-frames and adjust according to chart
time1 = "1"
time2 = "3"
time3 = "5"
time4 = "15"
time5 = "30"
time6 = "45"
time7 = "60"
time8 = "120"
time9 = "240"
time10 = "480"
time11 = "D"
time12 = "2D"
time13 = "W"
time14 = "2W"
time15 = "M"
time16 = "2M"
time17 = "4M"

//Multi time-frame Stochastic RSI k and d - auto adjust for different time-frame charts
k1 = k(time1)
k2 = k(time3)
k3 = k(time4)
k4 = k(time5)
k5 = k(time6)
k6 = k(time7)
k7 = k(time8)
k8 = k(time9)
k9 = k(time11)
k10 = k(time13)
k11 = k(time15)
d1 = d(time1)
d2 = d(time3)
d3 = d(time4)
d4 = d(time5)
d5 = d(time6)
d6 = d(time7)
d7 = d(time8)
d8 = d(time9)
d9 = d(time11)
d10 = d(time13)
d11 = d(time15)

if (framescore <= 3 and framescore > 1 and freecheck == false)
    k1 := k(time3)
    k2 := k(time4)
    k3 := k(time5)
    k4 := k(time6)
    k5 := k(time7)
    k6 := k(time8)
    k7 := k(time9)
    k8 := k(time11)
    k9 := k(time12)
    k10 := k(time13)
    k11 := k(time15)
    d1 := d(time3)
    d2 := d(time4)
    d3 := d(time5)
    d4 := d(time6)
    d5 := d(time7)
    d6 := d(time8)
    d7 := d(time9)
    d8 := d(time11)
    d9 := d(time12)
    d10 := d(time13)
    d11 := d(time15)

else if (framescore == 4 and freecheck == false)
    k1 := k(time4)
    k2 := k(time5)
    k3 := k(time6)
    k4 := k(time7)
    k5 := k(time8)
    k6 := k(time9)
    k7 := k(time11)
    k8 := k(time12)
    k9 := k(time13)
    k10 := k(time14)
    k11 := k(time15)
    d1 := d(time4)
    d2 := d(time5)
    d3 := d(time6)
    d4 := d(time7)
    d5 := d(time8)
    d6 := d(time9)
    d7 := d(time11)
    d8 := d(time12)
    d9 := d(time13)
    d10 := d(time14)
    d11 := d(time15)

else if (framescore > 4 and freecheck == false)
    k1 := k(time7)
    k2 := k(time8)
    k3 := k(time9)
    k4 := k(time10)
    k5 := k(time11)
    k6 := k(time12)
    k7 := k(time13)
    k8 := k(time14)
    k9 := k(time15)
    k10 := k(time16)
    k11 := k(time17)
    d1 := d(time7)
    d2 := d(time8)
    d3 := d(time9)
    d4 := d(time10)
    d5 := d(time11)
    d6 := d(time12)
    d7 := d(time13)
    d8 := d(time14)
    d9 := d(time15)
    d10 := d(time16)
    d11 := d(time17)

col1 = color.new(color.yellow, tp1)
col2 = color.new(color.blue, tp1)
col3 = color.new(color.lime, tp1)
col4 = color.new(color.red, tp1)
col5 = color.new(color.yellow, tp2)
col6 = color.new(color.blue, tp2)
col7 = color.new(color.lime, tp2)
col8 = color.new(color.red, tp2)

if (colorscheme == 'Classic')
//Classic Colors
    col1 := color.new(color.yellow, tp1)
    col2 := color.new(color.blue, tp1)
    col3 := color.new(color.lime, tp1)
    col4 := color.new(color.red, tp1)
    col5 := color.new(color.yellow, tp2)
    col6 := color.new(color.blue, tp2)
    col7 := color.new(color.lime, tp2)
    col8 := color.new(color.red, tp2)
else if (colorscheme == 'Rainbow')
//Rainbow Colors
    col1 := color.new(color.lime, tp1)
    col2 := color.new(color.yellow, tp1)
    col3 := color.new(color.orange, tp1)
    col4 := color.new(color.red, tp1)
    col5 := color.new(color.lime, tp2)
    col6 := color.new(color.yellow, tp2)
    col7 := color.new(color.orange, tp2)
    col8 := color.new(color.red, tp2)
else if (colorscheme == 'Tron')
//Tron Colors
    col1 := color.new(color.orange, tp1)
    col2 := color.new(color.red, tp1)
    col3 := color.new(color.purple, tp1)
    col4 := color.new(color.blue, tp1)
    col5 := color.new(color.orange, tp2)
    col6 := color.new(color.red, tp2)
    col7 := color.new(color.purple, tp2)
    col8 := color.new(color.blue, tp2)
else if (colorscheme == 'Blue')
//Blue Colors
    col1 := color.new(color.blue, tp1)
    col2 := color.new(color.blue, tp1)
    col3 := color.new(color.blue, tp1)
    col4 := color.new(color.blue, tp1)
    col5 := color.new(color.blue, tp2)
    col6 := color.new(color.blue, tp2)
    col7 := color.new(color.blue, tp2)
    col8 := color.new(color.blue, tp2)
    
    
// colors to make backward compatible.. there's a better way to do this
aco1 = color.new(color.yellow, tp3)
aco2 = color.new(color.blue, tp3)
aco3 = color.new(color.lime, tp3)
aco4 = color.new(color.red, tp3)
aco5 = color.new(color.yellow, tp4)
aco6 = color.new(color.blue, tp4)
aco7 = color.new(color.lime, tp4)
aco8 = color.new(color.red, tp4)

if (colorscheme == 'Classic')
//Classic Colors
    aco1 := color.new(color.yellow, tp3)
    aco2 := color.new(color.blue, tp3)
    aco3 := color.new(color.lime, tp3)
    aco4 := color.new(color.red, tp3)
    aco5 := color.new(color.yellow, tp4)
    aco6 := color.new(color.blue, tp4)
    aco7 := color.new(color.lime, tp4)
    aco8 := color.new(color.red, tp4)
else if (colorscheme == 'Rainbow')
//Rainbow Colors
    aco1 := color.new(color.lime, tp3)
    aco2 := color.new(color.yellow, tp3)
    aco3 := color.new(color.orange, tp3)
    aco4 := color.new(color.red, tp3)
    aco5 := color.new(color.lime, tp4)
    aco6 := color.new(color.yellow, tp4)
    aco7 := color.new(color.orange, tp4)
    aco8 := color.new(color.red, tp4)
else if (colorscheme == 'Tron')
//Tron Colors
    aco1 := color.new(color.orange, tp3)
    aco2 := color.new(color.red, tp3)
    aco3 := color.new(color.purple, tp3)
    aco4 := color.new(color.blue, tp3)
    aco5 := color.new(color.orange, tp4)
    aco6 := color.new(color.red, tp4)
    aco7 := color.new(color.purple, tp4)
    aco8 := color.new(color.blue, tp4)
else if(colorscheme == 'Blue')
//Blue Colors
    aco1 := color.new(color.blue, tp3)
    aco2 := color.new(color.blue, tp3)
    aco3 := color.new(color.blue, tp3)
    aco4 := color.new(color.blue, tp3)
    aco5 := color.new(color.blue, tp4)
    aco6 := color.new(color.blue, tp4)
    aco7 := color.new(color.blue, tp4)
    aco8 := color.new(color.blue, tp4)

//count number of sRSI that are in your favor
bullcount = 0
if (k1 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (k2 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (k3 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (k4 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (k5 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (k6 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (k7 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (k8 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (k9 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (k10 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (k11 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (d1 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (d2 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (d3 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (d4 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (d5 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (d6 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (d7 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (d8 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (d9 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (d10 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1
if (d11 <= SRSI_bull_cutoff)
    bullcount := bullcount + 1

bearcount = 0
if (k1 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (k2 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (k3 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (k4 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (k5 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (k6 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (k7 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (k8 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (k9 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (k10 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (k11 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (d1 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (d2 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (d3 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (d4 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (d5 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (d6 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (d7 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (d8 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (d9 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (d10 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1
if (d11 >= SRSI_bear_cutoff)
    bearcount := bearcount + 1

//Plot and colors
bulllow = color.new(color.green, 100)
bullhigh = color.new(color.green, 0)
bearlow = color.new(color.red, 100)
bearhigh = color.new(color.red, 0)
bullcolor = color.from_gradient(bullcount, lowthreshold, highthreshold, bulllow, bullhigh)
bearcolor = color.from_gradient(bearcount, lowthreshold, highthreshold, bearlow, bearhigh)

hbear = hline(SRSI_bear_cutoff, linestyle=hline.style_solid, color=color.new(color.gray,100), linewidth=1, editable=false)
hbull = hline(SRSI_bull_cutoff, linestyle=hline.style_solid, color=color.new(color.gray,100), linewidth=1, editable=false)
htop = hline(100, linestyle=hline.style_solid, color=color.new(color.gray,100), linewidth=1, editable=false)
hbottom = hline(0, linestyle=hline.style_solid, color=color.new(color.gray,100), linewidth=1, editable=false)
fill(hbear, htop, color=bearcolor, editable=false)
fill(hbull, hbottom, color=bullcolor, editable=false)

// Normal auto adjusting plots
plot1 = plot(k2, linewidth=1, color=col5)
plot2 = plot(d2, linewidth=1, color=col1)
plot3 = plot(k3, linewidth=1, color=col6)
plot4 = plot(d3, linewidth=1, color=col2)
plot5 = plot(k4, linewidth=1, color=col7)
plot6 = plot(d4, linewidth=1, color=col3)
plot7 = plot(k5, linewidth=1, color=col8)
plot8 = plot(d5, linewidth=1, color=col4)

// Alternative plots with manual override
plot9 = plot(k(lowtime), linewidth=1, color=aco5)
plot10 = plot(d(lowtime), linewidth=1, color=aco1)
plot11 = plot(k(midlowtime), linewidth=1, color=aco6)
plot12 = plot(d(midlowtime), linewidth=1, color=aco2)
plot13 = plot(k(midhightime), linewidth=1, color=aco7)
plot14 = plot(d(midhightime), linewidth=1, color=aco3)
plot15 = plot(k(hightime), linewidth=1, color=aco8)
plot16 = plot(d(hightime), linewidth=1, color=aco4)

// END