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

//@version=5
indicator("Correlation Coefficient Table", shorttitle = "CCT", format = format.price, precision = 2, overlay = true)
tf = input.timeframe(defval="", title = "Timeframe")
symbolInput = input.symbol("BTCUSDT", "Source Symbol", confirm = true, tooltip = "The symbol which will be the source of correlation")

symbolInput1 = input.symbol("", title = "Symbol 1")
symbolInput2 = input.symbol("", title = "Symbol 2")
symbolInput3 = input.symbol("", title = "Symbol 3")
symbolInput4 = input.symbol("", title = "Symbol 4")
symbolInput5 = input.symbol("", title = "Symbol 5")

sourceInput = input.source(close, "Source")
lengthInput = input.int(20, "Length", tooltip = "Numbers of bars back to be used when calculating correlation")
t_VPosition = input.string("top", "Vertical Position", options = ["top", "middle", "bottom"], group = "Table Settings")
t_HPosition = input.string("right", "Horizontal Position", options = ["left", "center", "right"], group = "Table Settings")
t_bgcolor    = input.color(color.new(color.gray, 0), "Background Color", group = "Table Settings")
t_bordercolor  = input.color(color.new(color.white,0), "Border Color", group = "Table Settings")
t_tsize  = input.string("normal", "Text Size", options = ["tiny", "small", "normal", "large", "huge", "auto"], group = "Table Settings")
t_alignment = input.string("center","Table Alignment", options = ["center","left", "right"], group = "Table Settings")

calc_correlation(source, source2, tf, candlesc, length ) =>
    s1=request.security(source, tf, candlesc, barmerge.gaps_off)
    s2=request.security(source2,tf, candlesc, barmerge.gaps_off)
    corr = ta.correlation(s1, s2, length)
    
    string corr_status = if corr == 0.0
        "No correlation"
    else if corr > 0.0 and corr < 0.2
        "Very Weak Correlation"
    else if corr >= 0.2 and corr < 0.4
        "Weak Correlation"
    else if corr >= 0.4 and corr < 0.6
        "Moderately Strong Correlation"
    else if corr >= 0.6 and corr < 0.8
        "Strong Correlation"
    else if corr >= 0.8 and corr < 1.0
        "Very Strong Correlation"
    else if corr == 1.0
        "Perfect Correlation"

    [corr, corr_status]


[corr1, corr_status1] = calc_correlation(symbolInput, symbolInput1, tf, sourceInput, lengthInput)
[corr2, corr_status2] = calc_correlation(symbolInput, symbolInput2, tf, sourceInput, lengthInput)
[corr3, corr_status3] = calc_correlation(symbolInput, symbolInput3, tf, sourceInput, lengthInput)
[corr4, corr_status4] = calc_correlation(symbolInput, symbolInput4, tf, sourceInput, lengthInput)
[corr5, corr_status5] = calc_correlation(symbolInput, symbolInput5, tf, sourceInput, lengthInput)

if barstate.islast
    var table t = table.new(t_VPosition + "_" + t_HPosition, columns = 7, rows = 7, bgcolor = t_bgcolor, border_width=1, border_color = t_bordercolor)
    if symbolInput1 != ""
        table.cell(t, 0, 0, "Base Symbol")
        table.cell(t, 1, 0, "vs Symbol")
        table.cell(t, 3, 0, "Corr Value")
        table.cell(t, 4, 0, "Corr Status")
        table.cell(t, 0, 1, symbolInput)
        table.cell(t, 1, 1, symbolInput1)
        table.cell(t, 3, 1, str.tostring(corr1,"#.##"))
        table.cell(t, 4, 1, corr_status1)
    if symbolInput2 != ""
        table.cell(t, 0, 2, symbolInput)
        table.cell(t, 1, 2, symbolInput2)
        table.cell(t, 3, 2, str.tostring(corr2,"#.##"))
        table.cell(t, 4, 2, corr_status2)
        table.merge_cells(t, 0, 1, 0, 2)
    if symbolInput3 != ""
        table.cell(t, 0, 3, symbolInput)
        table.cell(t, 1, 3, symbolInput3)
        table.cell(t, 3, 3, str.tostring(corr3,"#.##"))
        table.cell(t, 4, 3, corr_status3)
        table.merge_cells(t, 0, 1, 0, 3)
    if symbolInput4 != ""
        table.cell(t, 0, 4, symbolInput)
        table.cell(t, 1, 4, symbolInput4)
        table.cell(t, 3, 4, str.tostring(corr4,"#.##"))
        table.cell(t, 4, 4, corr_status4)
        table.merge_cells(t, 0, 1, 0, 4)
    if symbolInput5 != ""
        table.cell(t, 0, 5, symbolInput)
        table.cell(t, 1, 5, symbolInput5)
        table.cell(t, 3, 5, str.tostring(corr5,"#.##"))
        table.cell(t, 4, 5, corr_status5)
        table.merge_cells(t, 0, 1, 0, 5)