RSI Multi Symbol/Time Frame Detector
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MirNader_

//@version=5
indicator("RSI Multi Symbol/Time Frame Detector" , overlay = true)
// Create a string variable "Gr1" to be used as a group name for the time frames input
string Gr1              = " RSI Time Frames "
// Create four string variables to be used as input options for the time frames
string tf1_1            = input.timeframe('5'   , "Time Frame 1️⃣"      , group = Gr1 )
string tf1_2            = input.timeframe('15'  , "Time Frame 2️⃣"      , group = Gr1 )
string tf1_3            = input.timeframe('60'  , "Time Frame 3️⃣"      , group = Gr1 )
string tf1_4            = input.timeframe('240' , "Time Frame 4️⃣"      , group = Gr1 )

// Create a string variable "Gr2" to be used as a group name for the symbols input
string Gr2              = "RSI Symbols"
// Create three string variables to be used as input options for the symbols
string sym1             = input.symbol("SP:SPX"       , "Symbol 1️⃣"   , group = Gr2 )
string sym2             = input.symbol("OANDA:EURUSD" , "Symbol 2️⃣"   , group = Gr2 )
string sym3             = input.symbol("OANDA:NZDUSD" , "Symbol 3️⃣"   , group = Gr2 )

// Create a string variable "Gr3" to be used as a group name for the RSI settings input
string Gr3              = "RSI Settings"
// Create two integer variables to be used as input options for the RSI settings
int rsiLengthInput      = input.int(14, minval=1, title="RSI Length"   , group=Gr3)
float rsiSourceInput    = input.source(close, "Source"                 , group=Gr3)

// Create a string variable "Gr4" to be used as a group name for the MA settings input
string Gr4              = "MA Settings"
// Create three variables to be used as input options for the MA settings
string maTypeInput      = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group=Gr4)
int maLengthInput       = input.int(14, title="MA Length"                                                                            , group=Gr4)
float bbMultInput       = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev"                                                , group=Gr4)


// Define a function for the moving average calculation
ma(source, length, type) =>
    switch type
        // If the type is "SMA", return the simple moving average calculation
        "SMA" => ta.sma(source, length)
        // If the type is "Bollinger Bands", return the simple moving average calculation
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        // If the type is "EMA", return the exponential moving average calculation
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

// Calculate relative strength index and its moving average
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)

// Request security data for multiple symbols and time frames
float rsi1_1 = request.security(sym1 , tf1_1 , rsi[1] , gaps = barmerge.gaps_off , lookahead = barmerge.lookahead_on)
float rsi1_2 = request.security(sym1 , tf1_2 , rsi[1] , gaps = barmerge.gaps_off , lookahead = barmerge.lookahead_on)
float rsi1_3 = request.security(sym1 , tf1_3 , rsi[1] , gaps = barmerge.gaps_off , lookahead = barmerge.lookahead_on)
float rsi1_4 = request.security(sym1 , tf1_4 , rsi[1] , gaps = barmerge.gaps_off , lookahead = barmerge.lookahead_on)

float rsi2_1 = request.security(sym2 , tf1_1 , rsi[1] , gaps = barmerge.gaps_off , lookahead = barmerge.lookahead_on)
float rsi2_2 = request.security(sym2 , tf1_2 , rsi[1] , gaps = barmerge.gaps_off , lookahead = barmerge.lookahead_on)
float rsi2_3 = request.security(sym2 , tf1_3 , rsi[1] , gaps = barmerge.gaps_off , lookahead = barmerge.lookahead_on)
float rsi2_4 = request.security(sym2 , tf1_4 , rsi[1] , gaps = barmerge.gaps_off , lookahead = barmerge.lookahead_on)

float rsi3_1 = request.security(sym3 , tf1_1 , rsi[1] , gaps = barmerge.gaps_off , lookahead = barmerge.lookahead_on)
float rsi3_2 = request.security(sym3 , tf1_2 , rsi[1] , gaps = barmerge.gaps_off , lookahead = barmerge.lookahead_on)
float rsi3_3 = request.security(sym3 , tf1_3 , rsi[1] , gaps = barmerge.gaps_off , lookahead = barmerge.lookahead_on)
float rsi3_4 = request.security(sym3 , tf1_4 , rsi[1] , gaps = barmerge.gaps_off , lookahead = barmerge.lookahead_on)


// Plot Table 
var RSI_table    = table.new(
     "middle" + "_" + 'right', columns=5, rows=24, bgcolor=color.rgb(255, 82, 82, 100), 
     frame_color=color.gray, frame_width = 1, border_color=color.new(color.green, 70), border_width = 1)
     
Cell = 0
// Header
table.cell       (RSI_table    , 0, 0, text = "Multi Time Frame / Symbol RSI Check",text_color = color.black , text_size = size.tiny)
table.merge_cells(RSI_table , 0, Cell, 4, Cell)
    
Cell := 02
table.cell       (RSI_table    , 0, Cell, text = "Symbol"                         , text_color = color.black , text_size = size.tiny)
table.cell       (RSI_table    , 1, Cell, text = str.tostring(tf1_1 ) + ' Minute' , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)
table.cell       (RSI_table    , 2, Cell, text = str.tostring(tf1_2 ) + ' Minute' , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)
table.cell       (RSI_table    , 3, Cell, text = str.tostring(tf1_3 ) + ' Minute' , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)
table.cell       (RSI_table    , 4, Cell, text = str.tostring(tf1_4 ) + ' Minute' , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)

Cell := 03
table.cell       (RSI_table    , 0, Cell, text = str.tostring(sym1 )              , text_color = color.black , text_size = size.tiny)
table.cell       (RSI_table    , 1, Cell, text = str.tostring(rsi1_1 , '#' )      , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)
table.cell       (RSI_table    , 2, Cell, text = str.tostring(rsi1_2 , '#' )      , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)
table.cell       (RSI_table    , 3, Cell, text = str.tostring(rsi1_3 , '#' )      , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)
table.cell       (RSI_table    , 4, Cell, text = str.tostring(rsi1_4 , '#' )      , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)

Cell := 04
table.cell       (RSI_table    , 0, Cell, text = str.tostring(sym2 )              , text_color = color.black , text_size = size.tiny)
table.cell       (RSI_table    , 1, Cell, text = str.tostring(rsi2_1 , '#' )      , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)
table.cell       (RSI_table    , 2, Cell, text = str.tostring(rsi2_2 , '#' )      , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)
table.cell       (RSI_table    , 3, Cell, text = str.tostring(rsi2_3 , '#' )      , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)
table.cell       (RSI_table    , 4, Cell, text = str.tostring(rsi2_4 , '#' )      , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)

Cell := 05
table.cell       (RSI_table    , 0, Cell, text = str.tostring(sym3 )              , text_color = color.black , text_size = size.tiny)
table.cell       (RSI_table    , 1, Cell, text = str.tostring(rsi3_1 , '#' )      , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)
table.cell       (RSI_table    , 2, Cell, text = str.tostring(rsi3_2 , '#' )      , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)
table.cell       (RSI_table    , 3, Cell, text = str.tostring(rsi3_3 , '#' )      , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)
table.cell       (RSI_table    , 4, Cell, text = str.tostring(rsi3_4 , '#' )      , text_color = color.black , bgcolor = color.rgb(54, 58, 69, 100), text_size = size.tiny)
//End