Relative Strength vs SPY - real time & multi TF analysis
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © axg_
// This code uses the Multiple Indicators Screener written by QuantNomad, you can view the original code here: https://www.tradingview.com/script/yLwyZpOn-Multiple-Indicators-Screener/
// RS calculation also borrowed by modhelius, the original calculation can be found here: https://www.tradingview.com/script/A4WyMCKM-Relative-Strength/

//@version=5
indicator('Relative Strength Screener', overlay=true)

// Inputs

_10 = input(true, 'Calculation inputs')
spyId = input.symbol('SPY', title='Comparison Symbol (ex: SPY)')
user_period_id = input.timeframe('5', title='Relative Strength Timeframe')
spy_period = request.security(spyId, user_period_id, close)
Length = input(10, title='RS calculation period length')

var string LEFT = 'LEFT'
var string CENTER = 'CENTER'
var string RIGHT = 'RIGHT'
var string TRUE = 'TRUE'
var string FALSE = 'FALSE'

_20 = input(true, 'Table display settings')
string i_table_placement = input.string(RIGHT, 'Position of table? (Left, Center or Right)', options=[LEFT, CENTER, RIGHT])
string i_display_ticker_symb = input.string(TRUE, 'Display ticker symbol and price?', options=[TRUE, FALSE])

table_placement = i_table_placement == RIGHT ? position.top_right : i_table_placement == CENTER ? position.top_center : position.top_left

_30 = input(true, 'Stock selection')
// Symbol selection

u01 = input.bool(true,  title = "", group = 'Symbols', inline = 's01')
u02 = input.bool(true,  title = "", group = 'Symbols', inline = 's02')
u03 = input.bool(true,  title = "", group = 'Symbols', inline = 's03')
u04 = input.bool(true,  title = "", group = 'Symbols', inline = 's04')
u05 = input.bool(true,  title = "", group = 'Symbols', inline = 's05')
u06 = input.bool(true,  title = "", group = 'Symbols', inline = 's06')
u07 = input.bool(true,  title = "", group = 'Symbols', inline = 's07')
u08 = input.bool(true,  title = "", group = 'Symbols', inline = 's08')
u09 = input.bool(true,  title = "", group = 'Symbols', inline = 's09')
u10 = input.bool(true,  title = "", group = 'Symbols', inline = 's10')
u11 = input.bool(true,  title = "", group = 'Symbols', inline = 's11')
u12 = input.bool(true,  title = "", group = 'Symbols', inline = 's12')
u13 = input.bool(true,  title = "", group = 'Symbols', inline = 's13')
u14 = input.bool(true,  title = "", group = 'Symbols', inline = 's14')
u15 = input.bool(true,  title = "", group = 'Symbols', inline = 's15')
u16 = input.bool(true,  title = "", group = 'Symbols', inline = 's16')
u17 = input.bool(true,  title = "", group = 'Symbols', inline = 's17')
u18 = input.bool(true,  title = "", group = 'Symbols', inline = 's18')
u19 = input.bool(true,  title = "", group = 'Symbols', inline = 's19')
u20 = input.bool(true,  title = "", group = 'Symbols', inline = 's20')
u21 = input.bool(true,  title = "", group = 'Symbols', inline = 's21')
u22 = input.bool(true,  title = "", group = 'Symbols', inline = 's22')
u23 = input.bool(true,  title = "", group = 'Symbols', inline = 's23')
u24 = input.bool(true,  title = "", group = 'Symbols', inline = 's24')
u25 = input.bool(true,  title = "", group = 'Symbols', inline = 's25')
u26 = input.bool(true,  title = "", group = 'Symbols', inline = 's26')
u27 = input.bool(true,  title = "", group = 'Symbols', inline = 's27')
u28 = input.bool(true,  title = "", group = 'Symbols', inline = 's28')
u29 = input.bool(true,  title = "", group = 'Symbols', inline = 's29')
u30 = input.bool(true,  title = "", group = 'Symbols', inline = 's30')
u31 = input.bool(true,  title = "", group = 'Symbols', inline = 's31')
u32 = input.bool(true,  title = "", group = 'Symbols', inline = 's32')
u33 = input.bool(true,  title = "", group = 'Symbols', inline = 's33')
u34 = input.bool(true,  title = "", group = 'Symbols', inline = 's34')
u35 = input.bool(true,  title = "", group = 'Symbols', inline = 's35')
u36 = input.bool(true, title = "", group = 'Symbols', inline = 's36')
u37 = input.bool(true, title = "", group = 'Symbols', inline = 's37')
u38 = input.bool(true, title = "", group = 'Symbols', inline = 's38')
u39 = input.bool(true, title = "", group = 'Symbols', inline = 's39')

s01 = input.symbol('AAPL',  group = 'Symbols', inline = 's01')
s02 = input.symbol('ABNB',  group = 'Symbols', inline = 's02')
s03 = input.symbol('AFRM', group = 'Symbols', inline = 's03')
s04 = input.symbol('AMD',  group = 'Symbols', inline = 's04')
s05 = input.symbol('BA',  group = 'Symbols', inline = 's05')
s06 = input.symbol('BABA',  group = 'Symbols', inline = 's06')
s07 = input.symbol('BBY',   group = 'Symbols', inline = 's07')
s08 = input.symbol('COIN',  group = 'Symbols', inline = 's08')
s09 = input.symbol('DASH',  group = 'Symbols', inline = 's09')
s10 = input.symbol('DKS',  group = 'Symbols', inline = 's10')
s11 = input.symbol('DOCU',  group = 'Symbols', inline = 's11')
s12 = input.symbol('ETSY',  group = 'Symbols', inline = 's12')
s13 = input.symbol('FB',  group = 'Symbols', inline = 's13')
s14 = input.symbol('FUTU',   group = 'Symbols', inline = 's14')
s15 = input.symbol('HD',   group = 'Symbols', inline = 's15')
s16 = input.symbol('IONQ',  group = 'Symbols', inline = 's16')
s17 = input.symbol('LCID',  group = 'Symbols', inline = 's17')
s18 = input.symbol('MARA',  group = 'Symbols', inline = 's18')
s19 = input.symbol('MRNA',  group = 'Symbols', inline = 's19')
s20 = input.symbol('MSFT',  group = 'Symbols', inline = 's20')
s21 = input.symbol('MTTR',  group = 'Symbols', inline = 's21')
s22 = input.symbol('NFLX',  group = 'Symbols', inline = 's22')
s23 = input.symbol('NVAX',  group = 'Symbols', inline = 's23')
s24 = input.symbol('NVDA',   group = 'Symbols', inline = 's24')
s25 = input.symbol('PFE', group = 'Symbols', inline = 's25')
s26 = input.symbol('PLTR',  group = 'Symbols', inline = 's26')
s27 = input.symbol('PLUG', group = 'Symbols', inline = 's27')
s28 = input.symbol('PTON',   group = 'Symbols', inline = 's28')
s29 = input.symbol('PYPL', group = 'Symbols', inline = 's29')
s30 = input.symbol('QCOM',   group = 'Symbols', inline = 's30')
s31 = input.symbol('RBLX',  group = 'Symbols', inline = 's31')
s32 = input.symbol('RIOT',  group = 'Symbols', inline = 's32')
s33 = input.symbol('SOFI',  group = 'Symbols', inline = 's33')
s34 = input.symbol('TGT',  group = 'Symbols', inline = 's34')
s35 = input.symbol('TSLA',  group = 'Symbols', inline = 's35')
s36 = input.symbol('U', group = 'Symbols', inline = 's36')
s37 = input.symbol('UBER',   group = 'Symbols', inline = 's37')
s38 = input.symbol('V',   group = 'Symbols', inline = 's38')
s39 = input.symbol('XPEV',  group = 'Symbols', inline = 's39')


// CALCULATIONS //

// Get only symbol
only_symbol(s) => 
    array.get(str.split(s, ":"), 1)

// for Relative Strength to SPY
RelativeStrength(SPY, src, length) =>
    SPYPerc = ta.change(SPY, Length) / SPY[Length] * 100
    SymPerc = ta.change(src, Length) / src[Length] * 100
    Diff = SymPerc - SPYPerc

screener_func() =>
    
    // Last close price
    pc = ta.change(close)
    
    //Relative Strength to SPY
    RS = RelativeStrength(spy_period, close, Length)

    [math.round_to_mintick(close), RS]

// Security call
[cl01, rs5m01] = request.security(s01, user_period_id, screener_func())
[cl02, rs5m02] = request.security(s02, user_period_id, screener_func())
[cl03, rs5m03] = request.security(s03, user_period_id, screener_func())
[cl04, rs5m04] = request.security(s04, user_period_id, screener_func())
[cl05, rs5m05] = request.security(s05, user_period_id, screener_func())
[cl06, rs5m06] = request.security(s06, user_period_id, screener_func())
[cl07, rs5m07] = request.security(s07, user_period_id, screener_func())
[cl08, rs5m08] = request.security(s08, user_period_id, screener_func())
[cl09, rs5m09] = request.security(s09, user_period_id, screener_func())
[cl10, rs5m10] = request.security(s10, user_period_id, screener_func())
[cl11, rs5m11] = request.security(s11, user_period_id, screener_func())
[cl12, rs5m12] = request.security(s12, user_period_id, screener_func())
[cl13, rs5m13] = request.security(s13, user_period_id, screener_func())
[cl14, rs5m14] = request.security(s14, user_period_id, screener_func())
[cl15, rs5m15] = request.security(s15, user_period_id, screener_func())
[cl16, rs5m16] = request.security(s16, user_period_id, screener_func())
[cl17, rs5m17] = request.security(s17, user_period_id, screener_func())
[cl18, rs5m18] = request.security(s18, user_period_id, screener_func())
[cl19, rs5m19] = request.security(s19, user_period_id, screener_func())
[cl20, rs5m20] = request.security(s20, user_period_id, screener_func())
[cl21, rs5m21] = request.security(s21, user_period_id, screener_func())
[cl22, rs5m22] = request.security(s22, user_period_id, screener_func())
[cl23, rs5m23] = request.security(s23, user_period_id, screener_func())
[cl24, rs5m24] = request.security(s24, user_period_id, screener_func())
[cl25, rs5m25] = request.security(s25, user_period_id, screener_func())
[cl26, rs5m26] = request.security(s26, user_period_id, screener_func())
[cl27, rs5m27] = request.security(s27, user_period_id, screener_func())
[cl28, rs5m28] = request.security(s28, user_period_id, screener_func())
[cl29, rs5m29] = request.security(s29, user_period_id, screener_func())
[cl30, rs5m30] = request.security(s30, user_period_id, screener_func())
[cl31, rs5m31] = request.security(s31, user_period_id, screener_func())
[cl32, rs5m32] = request.security(s32, user_period_id, screener_func())
[cl33, rs5m33] = request.security(s33, user_period_id, screener_func())
[cl34, rs5m34] = request.security(s34, user_period_id, screener_func())
[cl35, rs5m35] = request.security(s35, user_period_id, screener_func())
[cl36, rs5m36] = request.security(s36, user_period_id, screener_func())
[cl37, rs5m37] = request.security(s37, user_period_id, screener_func())
[cl38, rs5m38] = request.security(s38, user_period_id, screener_func())
[cl39, rs5m39] = request.security(s39, user_period_id, screener_func())


// ARRAYS //

s_arr   = array.new_string(0)
u_arr   = array.new_bool(0)
cl_arr  = array.new_float(0)
RS_arr = array.new_float(0)

// Add Symbols 
array.push(s_arr, only_symbol(s01))
array.push(s_arr, only_symbol(s02))
array.push(s_arr, only_symbol(s03))
array.push(s_arr, only_symbol(s04))
array.push(s_arr, only_symbol(s05))
array.push(s_arr, only_symbol(s06))
array.push(s_arr, only_symbol(s07))
array.push(s_arr, only_symbol(s08))
array.push(s_arr, only_symbol(s09))
array.push(s_arr, only_symbol(s10))
array.push(s_arr, only_symbol(s11))
array.push(s_arr, only_symbol(s12))
array.push(s_arr, only_symbol(s13))
array.push(s_arr, only_symbol(s14))
array.push(s_arr, only_symbol(s15))
array.push(s_arr, only_symbol(s16))
array.push(s_arr, only_symbol(s17))
array.push(s_arr, only_symbol(s18))
array.push(s_arr, only_symbol(s19))
array.push(s_arr, only_symbol(s20))
array.push(s_arr, only_symbol(s21))
array.push(s_arr, only_symbol(s22))
array.push(s_arr, only_symbol(s23))
array.push(s_arr, only_symbol(s24))
array.push(s_arr, only_symbol(s25))
array.push(s_arr, only_symbol(s26))
array.push(s_arr, only_symbol(s27))
array.push(s_arr, only_symbol(s28))
array.push(s_arr, only_symbol(s29))
array.push(s_arr, only_symbol(s30))
array.push(s_arr, only_symbol(s31))
array.push(s_arr, only_symbol(s32))
array.push(s_arr, only_symbol(s33))
array.push(s_arr, only_symbol(s34))
array.push(s_arr, only_symbol(s35))
array.push(s_arr, only_symbol(s36))
array.push(s_arr, only_symbol(s37))
array.push(s_arr, only_symbol(s38))
array.push(s_arr, only_symbol(s39))

///////////
// FLAGS //

array.push(u_arr, u01)
array.push(u_arr, u02)
array.push(u_arr, u03)
array.push(u_arr, u04)
array.push(u_arr, u05)
array.push(u_arr, u06)
array.push(u_arr, u07)
array.push(u_arr, u08)
array.push(u_arr, u09)
array.push(u_arr, u10)
array.push(u_arr, u11)
array.push(u_arr, u12)
array.push(u_arr, u13)
array.push(u_arr, u14)
array.push(u_arr, u15)
array.push(u_arr, u16)
array.push(u_arr, u17)
array.push(u_arr, u18)
array.push(u_arr, u19)
array.push(u_arr, u20)
array.push(u_arr, u21)
array.push(u_arr, u22)
array.push(u_arr, u23)
array.push(u_arr, u24)
array.push(u_arr, u25)
array.push(u_arr, u26)
array.push(u_arr, u27)
array.push(u_arr, u28)
array.push(u_arr, u29)
array.push(u_arr, u30)
array.push(u_arr, u31)
array.push(u_arr, u32)
array.push(u_arr, u33)
array.push(u_arr, u34)
array.push(u_arr, u35)
array.push(u_arr, u36)
array.push(u_arr, u37)
array.push(u_arr, u38)
array.push(u_arr, u39)

///////////
// CLOSE //

array.push(cl_arr, cl01)
array.push(cl_arr, cl02)
array.push(cl_arr, cl03)
array.push(cl_arr, cl04)
array.push(cl_arr, cl05)
array.push(cl_arr, cl06)
array.push(cl_arr, cl07)
array.push(cl_arr, cl08)
array.push(cl_arr, cl09)
array.push(cl_arr, cl10)
array.push(cl_arr, cl11)
array.push(cl_arr, cl12)
array.push(cl_arr, cl13)
array.push(cl_arr, cl14)
array.push(cl_arr, cl15)
array.push(cl_arr, cl16)
array.push(cl_arr, cl17)
array.push(cl_arr, cl18)
array.push(cl_arr, cl19)
array.push(cl_arr, cl20)
array.push(cl_arr, cl21)
array.push(cl_arr, cl22)
array.push(cl_arr, cl23)
array.push(cl_arr, cl24)
array.push(cl_arr, cl25)
array.push(cl_arr, cl26)
array.push(cl_arr, cl27)
array.push(cl_arr, cl28)
array.push(cl_arr, cl29)
array.push(cl_arr, cl30)
array.push(cl_arr, cl31)
array.push(cl_arr, cl32)
array.push(cl_arr, cl33)
array.push(cl_arr, cl34)
array.push(cl_arr, cl35)
array.push(cl_arr, cl36)
array.push(cl_arr, cl37)
array.push(cl_arr, cl38)
array.push(cl_arr, cl39)

/////////
// Relative Strength

array.push(RS_arr, rs5m01)
array.push(RS_arr, rs5m02)
array.push(RS_arr, rs5m03)
array.push(RS_arr, rs5m04)
array.push(RS_arr, rs5m05)
array.push(RS_arr, rs5m06)
array.push(RS_arr, rs5m07)
array.push(RS_arr, rs5m08)
array.push(RS_arr, rs5m09)
array.push(RS_arr, rs5m10)
array.push(RS_arr, rs5m11)
array.push(RS_arr, rs5m12)
array.push(RS_arr, rs5m13)
array.push(RS_arr, rs5m14)
array.push(RS_arr, rs5m15)
array.push(RS_arr, rs5m16)
array.push(RS_arr, rs5m17)
array.push(RS_arr, rs5m18)
array.push(RS_arr, rs5m19)
array.push(RS_arr, rs5m20)
array.push(RS_arr, rs5m21)
array.push(RS_arr, rs5m22)
array.push(RS_arr, rs5m23)
array.push(RS_arr, rs5m24)
array.push(RS_arr, rs5m25)
array.push(RS_arr, rs5m26)
array.push(RS_arr, rs5m27)
array.push(RS_arr, rs5m28)
array.push(RS_arr, rs5m29)
array.push(RS_arr, rs5m30)
array.push(RS_arr, rs5m31)
array.push(RS_arr, rs5m32)
array.push(RS_arr, rs5m33)
array.push(RS_arr, rs5m34)
array.push(RS_arr, rs5m35)
array.push(RS_arr, rs5m36)
array.push(RS_arr, rs5m37)
array.push(RS_arr, rs5m38)
array.push(RS_arr, rs5m39)

// PLOTS //

var tbl = table.new(table_placement, 6, 41, frame_color=#151715, frame_width=1, border_width=2, border_color=color.new(color.white, 100))

if barstate.islast and i_display_ticker_symb == TRUE

    table.cell(tbl, 0, 0, 'Symbol',     text_halign = text.align_center, bgcolor = color.gray, text_color = color.white, text_size = size.normal)
    table.cell(tbl, 1, 0, 'Price',      text_halign = text.align_center, bgcolor = color.gray, text_color = color.white, text_size = size.normal)
    table.cell(tbl, 2, 0, 'RS '+ user_period_id,        text_halign = text.align_center, bgcolor = color.gray, text_color = color.white, text_size = size.normal)

    for i = 0 to 38
        if array.get(u_arr, i)
        
            rs5M_col  = array.get(RS_arr, i) > 0    ? color.green   : array.get(RS_arr, i) < 0 ? color.red : #aaaaaa 

            table.cell(tbl, 0, i + 1, array.get(s_arr, i),                                 text_halign = text.align_left,   bgcolor = color.gray, text_color = color.white, text_size = size.small)
            table.cell(tbl, 1, i + 1, str.tostring(array.get(cl_arr, i)),                  text_halign = text.align_center, bgcolor = #aaaaaa,    text_color = color.white, text_size = size.small)
            table.cell(tbl, 2, i + 1, str.tostring(array.get(RS_arr, i), "#.##"),         text_halign = text.align_center, bgcolor = rs5M_col,    text_color = color.white, text_size = size.small)
else
    table.cell(tbl, 0, 0, 'RS '+ user_period_id,        text_halign = text.align_center, bgcolor = color.gray, text_color = color.white, text_size = size.normal)

    for i = 0 to 38
        if array.get(u_arr, i)
        
            rs5M_col  = array.get(RS_arr, i) > 0    ? color.green   : array.get(RS_arr, i) < 0 ? color.red : #aaaaaa 
            table.cell(tbl, 0, i + 1, str.tostring(array.get(RS_arr, i), "#.##"),         text_halign = text.align_center, bgcolor = rs5M_col,    text_color = color.white, text_size = size.small)
