MTF StochRSI indicator by noop42
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © noop42

//@version=4
study("MTF StochRSI indicator by noop42", overlay=false)

// Input parameters
src = input(close, title="Source")
average_value = input(title="Use average value mode (K+D/2)", type=input.bool, defval=true)
repainting = input(title="Allow repaint ? (disable to get live values)", type=input.bool, defval=true)

tf1 = input(title="Timeframe 1", type=input.resolution, defval="1")
tf2 = input(title="Timeframe 2", type=input.resolution, defval="5")
tf3 = input(title="Timeframe 3", type=input.resolution, defval="15")

smoothKD = input(3, "K", minval=1, group="Stoch RSI")
lengthRSI = input(14, "RSI Length", minval=1, group="Stoch RSI")
lengthStoch = input(14, "Stochastic Length", minval=1, group="Stoch RSI")
stoch_low = input(20, title="Stoch Oversold Level", group="Stoch RSI")
stoch_high = input(80, title="Stoch Overbought Level", group="Stoch RSI")

// On/Off repainting function
rp_security(_symbol, _res, _src) => security(_symbol, _res, _src[barstate.isrealtime ? (repainting ? 0 : 1) : 0], gaps=false)

// MTF Stochastic RSI
rsi_tf1 = rp_security(syminfo.tickerid, tf1, rsi(src,lengthRSI))
stoch_k_tf1 = rp_security(syminfo.tickerid, tf1, sma(stoch(rsi_tf1, rsi_tf1, rsi_tf1, lengthStoch), smoothKD))
stoch_d_tf1 = rp_security(syminfo.tickerid, tf1, sma(stoch_k_tf1, smoothKD))

rsi_tf2 = rp_security(syminfo.tickerid, tf2, rsi(src,lengthRSI))
stoch_k_tf2 = rp_security(syminfo.tickerid, tf2, sma(stoch(rsi_tf2, rsi_tf2, rsi_tf2, lengthStoch), smoothKD))
stoch_d_tf2 = rp_security(syminfo.tickerid, tf2, sma(stoch_k_tf2, smoothKD))

rsi_tf3 = rp_security(syminfo.tickerid, tf3, rsi(src,lengthRSI))
stoch_k_tf3 = rp_security(syminfo.tickerid, tf3, sma(stoch(rsi_tf3, rsi_tf3, rsi_tf3, lengthStoch), smoothKD))
stoch_d_tf3 = rp_security(syminfo.tickerid, tf3, sma(stoch_k_tf3, smoothKD))

// Zones lines
h0 = hline(stoch_high, "Upper Band", color=#eb500e45)
hmid = hline(50, "Middle Band", color=#ffffff45)
h1 = hline(stoch_low, "Lower Band", color=#10a3de45)
fill(h0, h1, color=#1984d110)

// Average mode
avgtf1 = (stoch_k_tf1 + stoch_d_tf1) / 2
avgtf2 = (stoch_k_tf2 + stoch_d_tf2) / 2
avgtf3 = (stoch_k_tf3 + stoch_d_tf3) / 2

bColor1 = #00bfff
bColor2 = #0048ff
bColor3 = #1e1e8f

plot(average_value ? avgtf1: na, "SRSI tf1 (avg mode)", color = bColor1, linewidth=1, transp=25)
plot(average_value ? avgtf2: na, "SRSI tf2 (avg mode)", color = bColor2, linewidth=2, transp=25)
plot(average_value ? avgtf3: na, "SRSI tf3 (avg mode)", color = bColor3, linewidth=4, transp=25)

// Classic mode
plot(average_value ? na: stoch_k_tf1, "K #1", color=color.aqua, linewidth=1)
plot(average_value ? na: stoch_d_tf1, "D #1", color=color.blue, linewidth=1)
plot(average_value ? na: stoch_k_tf2, "K #2", color=color.lime, linewidth=2)
plot(average_value ? na: stoch_d_tf2, "D #2", color=color.green, linewidth=2)
plot(average_value ? na: stoch_k_tf3, "K #3", color=color.yellow, linewidth=3)
plot(average_value ? na: stoch_d_tf3, "D #3", color=color.orange, linewidth=3)
