Investing - Correlation Table
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RrBc2

//@version=5
indicator("Investing - Correlation Table", overlay=true, max_bars_back=500)


correlation_TimeFrame       = timeframe.period
correlation_Source          = input.source(title="Correlation Source", defval=close)
correlation_Percentage      = input.float(title="Correlation Percentage (%)", defval=50, tooltip="How many (%) of correlation is considered not CORRELATED?")
barsToCheck                 = input.int(title="Amount of Bars to Check", defval=500, minval=0, maxval=500, tooltip="How many bars does it has to check for correlation percentage?")
correlated_Color            = input.color(title="Correlated Color", defval=color.rgb(255,204,204), inline="tableColor")
notCorrelated_Color         = input.color(title="", defval=color.rgb(204,255,204), inline="tableColor")

g_SymList                   = "Symbol List"
syminfo_1                   = input.symbol(title="", defval="FTX:BTCUSD", group=g_SymList, inline=g_SymList)
syminfo_2                   = input.symbol(title="", defval="FTX:ETHUSD", group=g_SymList, inline=g_SymList)
syminfo_3                   = input.symbol(title="", defval="FTX:BNBUSD", group=g_SymList, inline=g_SymList)
syminfo_4                   = input.symbol(title="", defval="FTX:XRPUSD", group=g_SymList, inline=g_SymList)
syminfo_5                   = input.symbol(title="", defval="FTX:LTCUSD", group=g_SymList, inline=g_SymList)
syminfo_6                   = input.symbol(title="", defval="FTX:SOLUSD", group=g_SymList, inline=g_SymList)
syminfo_7                   = input.symbol(title="", defval="FTX:DOTUSD", group=g_SymList, inline=g_SymList)
syminfo_8                   = input.symbol(title="", defval="FTX:DOGEUSD", group=g_SymList, inline=g_SymList)
syminfo_9                   = input.symbol(title="", defval="FTX:MATICUSD", group=g_SymList, inline=g_SymList)
syminfo_10                  = input.symbol(title="", defval="FTX:LINKUSD", group=g_SymList, inline=g_SymList)
syminfo_11                  = input.symbol(title="", defval="FTX:FTMUSD", group=g_SymList, inline=g_SymList)
syminfo_12                  = input.symbol(title="", defval="FTX:FTTUSD", group=g_SymList, inline=g_SymList)


security_0                  = request.security(syminfo.tickerid, correlation_TimeFrame, correlation_Source)
security_1                  = request.security(syminfo_1, correlation_TimeFrame, correlation_Source)
security_2                  = request.security(syminfo_2, correlation_TimeFrame, correlation_Source)
security_3                  = request.security(syminfo_3, correlation_TimeFrame, correlation_Source)
security_4                  = request.security(syminfo_4, correlation_TimeFrame, correlation_Source)
security_5                  = request.security(syminfo_5, correlation_TimeFrame, correlation_Source)
security_6                  = request.security(syminfo_6, correlation_TimeFrame, correlation_Source)
security_7                  = request.security(syminfo_7, correlation_TimeFrame, correlation_Source)
security_8                  = request.security(syminfo_8, correlation_TimeFrame, correlation_Source)
security_9                  = request.security(syminfo_9, correlation_TimeFrame, correlation_Source)
security_10                 = request.security(syminfo_10, correlation_TimeFrame, correlation_Source)
security_11                 = request.security(syminfo_11, correlation_TimeFrame, correlation_Source)
security_12                 = request.security(syminfo_12, correlation_TimeFrame, correlation_Source)


var table correlation_Table = table.new(position = position.top_right, columns = 2, rows = 15, border_width = 1)

var float[] correlation_Value   = array.new_float(20, 0)
array.set(correlation_Value, 0, math.round(ta.correlation(security_0, security_1, barsToCheck), 3) * 100)
array.set(correlation_Value, 1, math.round(ta.correlation(security_0, security_2, barsToCheck), 3)* 100)
array.set(correlation_Value, 2, math.round(ta.correlation(security_0, security_3, barsToCheck), 3)* 100)
array.set(correlation_Value, 3, math.round(ta.correlation(security_0, security_4, barsToCheck), 3)* 100)
array.set(correlation_Value, 4, math.round(ta.correlation(security_0, security_5, barsToCheck), 3)* 100)
array.set(correlation_Value, 5, math.round(ta.correlation(security_0, security_6, barsToCheck), 3)* 100)
array.set(correlation_Value, 6, math.round(ta.correlation(security_0, security_7, barsToCheck), 3)* 100)
array.set(correlation_Value, 7, math.round(ta.correlation(security_0, security_8, barsToCheck), 3)* 100)
array.set(correlation_Value, 8, math.round(ta.correlation(security_0, security_9, barsToCheck), 3)* 100)
array.set(correlation_Value, 9, math.round(ta.correlation(security_0, security_10, barsToCheck), 3)* 100)
array.set(correlation_Value, 10, math.round(ta.correlation(security_0, security_11, barsToCheck), 3)* 100)
array.set(correlation_Value, 11, math.round(ta.correlation(security_0, security_12, barsToCheck), 3)* 100)

var string[] symbol_list        = array.new_string(20, "")
array.set(symbol_list, 0, str.substring(syminfo_1, (str.pos(syminfo_1, ":")+1)))
array.set(symbol_list, 1, str.substring(syminfo_2, (str.pos(syminfo_2, ":")+1)))
array.set(symbol_list, 2, str.substring(syminfo_3, (str.pos(syminfo_3, ":")+1)))
array.set(symbol_list, 3, str.substring(syminfo_4, (str.pos(syminfo_4, ":")+1)))
array.set(symbol_list, 4, str.substring(syminfo_5, (str.pos(syminfo_5, ":")+1)))
array.set(symbol_list, 5, str.substring(syminfo_6, (str.pos(syminfo_6, ":")+1)))
array.set(symbol_list, 6, str.substring(syminfo_7, (str.pos(syminfo_7, ":")+1)))
array.set(symbol_list, 7, str.substring(syminfo_8, (str.pos(syminfo_8, ":")+1)))
array.set(symbol_list, 8, str.substring(syminfo_9, (str.pos(syminfo_9, ":")+1)))
array.set(symbol_list, 9, str.substring(syminfo_10, (str.pos(syminfo_10, ":")+1)))
array.set(symbol_list, 10, str.substring(syminfo_11, (str.pos(syminfo_10, ":")+1)))
array.set(symbol_list, 11, str.substring(syminfo_12, (str.pos(syminfo_10, ":")+1)))


table.cell(correlation_Table, 0, 0, bgcolor=color.rgb(175,175,175), width=10, text="Symbol")
table.cell(correlation_Table, 1, 0, bgcolor=color.rgb(175,175,175), text="Correlation")

index = 0 
while index != 12
    correlation = array.get(correlation_Value, index)
    tableBG_Color = correlation < correlation_Percentage ? notCorrelated_Color : correlated_Color
    table.cell(correlation_Table, 0, index+1, bgcolor=tableBG_Color, text=str.tostring(array.get(symbol_list, index)))
    table.cell(correlation_Table, 1, index+1, bgcolor=tableBG_Color, text=str.tostring(correlation) + "%")
    index += 1