RSI MFI MultiTimeframe Oversold/Overbought
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Seungdori_

//@version=5
indicator("RSI MFI MultiTimeframe Oversold/Overbought", shorttitle = 'RSI/MFI MTF', overlay =false)

//Region : Inputs//

main_osc = input.string(defval = 'RSI', title = '[ RSI / MFI / Sto RSI ] ', options = ['RSI', 'MFI', 'Sto RSI'], group = 'OSC')
overbought = input.int(defval = 70, group = 'OSC')
oversold = input.int(defval = 30, group = 'OSC')

mtf1= input.timeframe('15' , title = 'TimeFrame 1st', group = 'TimeFrame')
mtf2= input.timeframe('60' , title = 'TimeFrame 2nd', group = 'TimeFrame')
mtf3= input.timeframe('240', title = 'TimeFrame 3rd', group = 'TimeFrame')
mtf4= input.timeframe('D'  , title = 'TimeFrame 4th', group = 'TimeFrame')
mtf5= input.timeframe('W'  , title = 'TimeFrame 5th', group = 'TimeFrame')

grad = input.bool(defval = true, title = 'GRADIENT COLOR', group = 'COLOR')
up_color = input.color(defval = color.rgb(9, 156, 142), title = 'UP', group = 'COLOR')
down_color = input.color(defval = color.rgb(247, 30, 30), title = 'DOWN', group = 'COLOR')

//Region : Oscilators//

rsi = ta.rsi(close, 14)
mfi = ta.mfi(hlc3, 14)
smoothK = 3
smoothD = 3
lengthStoch = 14
k = ta.sma(ta.stoch(rsi, rsi, rsi, lengthStoch), smoothK)
d = ta.sma(k, smoothD)

var float osc = close
var int osc_state = 0

if main_osc == 'MFI'
    osc := mfi
if main_osc == 'RSI'
    osc := rsi
if main_osc == 'Sto RSI'
    osc := d

if osc > overbought
    osc_state := 1
if osc < oversold
    osc_state := -1


//Region : MTF//

OSC_MTF_STATE_1 = request.security(syminfo.tickerid, mtf1, osc_state)
OSC_MTF_STATE_2 = request.security(syminfo.tickerid, mtf2, osc_state)
OSC_MTF_STATE_3 = request.security(syminfo.tickerid, mtf3, osc_state)
OSC_MTF_STATE_4 = request.security(syminfo.tickerid, mtf4, osc_state)
OSC_MTF_STATE_5 = request.security(syminfo.tickerid, mtf5, osc_state)

OSC_MTF_1 = request.security(syminfo.tickerid, mtf1, osc, barmerge.gaps_off)
OSC_MTF_2 = request.security(syminfo.tickerid, mtf2, osc, barmerge.gaps_off)
OSC_MTF_3 = request.security(syminfo.tickerid, mtf3, osc, barmerge.gaps_off)
OSC_MTF_4 = request.security(syminfo.tickerid, mtf4, osc, barmerge.gaps_off)
OSC_MTF_5 = request.security(syminfo.tickerid, mtf5, osc, barmerge.gaps_off)

//Region : Colors//

color plot1_up_color = grad ? color.from_gradient(OSC_MTF_1, 60, overbought + 15, color.new(color.gray, 80),up_color) : color.green 
color plot2_up_color = grad ? color.from_gradient(OSC_MTF_2, 60, overbought + 15, color.new(color.gray, 80),up_color) : color.green 
color plot3_up_color = grad ? color.from_gradient(OSC_MTF_3, 60, overbought + 15, color.new(color.gray, 80),up_color) : color.green 
color plot4_up_color = grad ? color.from_gradient(OSC_MTF_4, 60, overbought + 15, color.new(color.gray, 80),up_color) : color.green 
color plot5_up_color = grad ? color.from_gradient(OSC_MTF_5, 60, overbought + 15, color.new(color.gray, 80),up_color) : color.green 

color plot1_down_color = grad ? color.from_gradient(OSC_MTF_1, oversold - 15, 60, down_color,color.new(color.gray, 80)) : color.red 
color plot2_down_color = grad ? color.from_gradient(OSC_MTF_2, oversold - 15, 60, down_color,color.new(color.gray, 80)) : color.red 
color plot3_down_color = grad ? color.from_gradient(OSC_MTF_3, oversold - 15, 60, down_color,color.new(color.gray, 80)) : color.red 
color plot4_down_color = grad ? color.from_gradient(OSC_MTF_4, oversold - 15, 60, down_color,color.new(color.gray, 80)) : color.red 
color plot5_down_color = grad ? color.from_gradient(OSC_MTF_5, oversold - 15, 60, down_color,color.new(color.gray, 80)) : color.red 

//Region : Plots//

plot(1, style = plot.style_line, color= grad ? (OSC_MTF_1 >= 50 ? plot1_up_color : plot1_down_color) : OSC_MTF_STATE_1 == 1  ? color.new(up_color,15) : OSC_MTF_STATE_1 == -1 ? color.new(down_color,15) : na, linewidth =10)
plot(2, style = plot.style_line, color= grad ? (OSC_MTF_2 >= 50 ? plot2_up_color : plot2_down_color) : OSC_MTF_STATE_2 == 1  ? color.new(up_color,15) : OSC_MTF_STATE_2 == -1 ? color.new(down_color,15) : na, linewidth =10)
plot(3, style = plot.style_line, color= grad ? (OSC_MTF_3 >= 50 ? plot3_up_color : plot3_down_color) : OSC_MTF_STATE_3 == 1  ? color.new(up_color,15) : OSC_MTF_STATE_3 == -1 ? color.new(down_color,15) : na, linewidth =10)
plot(4, style = plot.style_line, color= grad ? (OSC_MTF_4 >= 50 ? plot4_up_color : plot4_down_color) : OSC_MTF_STATE_4 == 1  ? color.new(up_color,15) : OSC_MTF_STATE_4 == -1 ? color.new(down_color,15) : na, linewidth =10)
plot(5, style = plot.style_line, color= grad ? (OSC_MTF_5 >= 50 ? plot5_up_color : plot5_down_color) : OSC_MTF_STATE_5 == 1  ? color.new(up_color,15) : OSC_MTF_STATE_5 == -1 ? color.new(down_color,15) : na, linewidth =10)

//Region : Labels//

formatTimeframe(_tf) =>
    tfStr = ""
    if _tf == "D"
        tfStr := "1D"
    else if _tf == "W"
        tfStr := "1W"
    else
        tfNum = str.tonumber(_tf)
        if tfNum < 60
            tfStr := _tf + "m"
        else if tfNum == 60
            tfStr := "1H"
        else
            hours = tfNum / 60
            tfStr := str.tostring(hours) + "H"
    tfStr

var label osc_label_1 = label.new(x = bar_index+3, y = 1, text = "", color = color.new(color.black, 100), textcolor=color.white, style = label.style_label_left)
var label osc_label_2 = label.new(x = bar_index+3, y = 2, text = "", color = color.new(color.black, 100), textcolor=color.white, style = label.style_label_left)
var label osc_label_3 = label.new(x = bar_index+3, y = 3, text = "", color = color.new(color.black, 100), textcolor=color.white, style = label.style_label_left)
var label osc_label_4 = label.new(x = bar_index+3, y = 4, text = "", color = color.new(color.black, 100), textcolor=color.white, style = label.style_label_left)
var label osc_label_5 = label.new(x = bar_index+3, y = 5, text = "", color = color.new(color.black, 100), textcolor=color.white, style = label.style_label_left)


_label(_label, _timeframe, plot, _value) =>
    label.set_x(_label, bar_index+5)
    label.set_y(_label, plot)
    label.set_text(_label, formatTimeframe(_timeframe) +   ": " + str.tostring(math.round(_value,2)))

_label(osc_label_1, mtf1, 1,OSC_MTF_1)
_label(osc_label_2, mtf2, 2,OSC_MTF_2)
_label(osc_label_3, mtf3, 3,OSC_MTF_3)
_label(osc_label_4, mtf4, 4,OSC_MTF_4)
_label(osc_label_5, mtf5, 5,OSC_MTF_5)