Correlation Analysis
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Created With passion by © LucasVivien
//@version=5
// V2 = v24


indicator('Correlation Analysis', overlay=false)

// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
//                                                    INPUTS                              
// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼

var g_1 = "Correlation Display"
plotcorrelation = input.bool  (true       , group=g_1, inline="a", title='Correlation')
i_highcorr      = input.color (#FF0000  , group=g_1, inline="a", title="")
i_lowcorr       = input.color (#00bcd4  , group=g_1, inline="a", title=""        , tooltip='Plots correlation between current chart and the "Reference Market" (input) below.\n\nCorrelation is calculated over the "Lookback" (input) number of periods.')
useScreener     = input.bool  (false      , group=g_1, inline="b", title="Screener")
boxLoc          = input.string("Top right", group=g_1, inline="b", title=""        , options=["Top center", "Top left", "Top right", "Middle center", "Middle left", "Middle right", "Bottom center", "Bottom left", "Bottom right"], tooltip='PLOTS CROSS-CORRELATION TABLE\nAdjust the number of lines/columns by turning on/off symbol switches below.\n\n - WARNING -\nDisplaying many symbols is memory/calculation expensive!\nDepending on you TradingView subscription plan, you might not be able to display all 20 at once.\nAlso, the more bars displayed on the chart (eg: on lower timeframes), the longer it will take for the script to execute.\nIf the script does not compile, simply turn off some symbols until it does.')
lookback        = input.int   (20         , group=g_1            , title='Lookback', tooltip="Correlation Lookback period")
source          = input.source(close      , group=g_1            , title='Source'  , tooltip="Price data source")
hiColGrad       = input.int   (80         , group=g_1, inline="c", title="Gradient: High/Low", tooltip="")
loColGrad       = input.int   (0          , group=g_1, inline="c", title="", tooltip="Adjust high correlation color gradient max threshold")

var g_2 = "Other Display"
i_maCol         = input.color (#e2df21  , group=g_2, inline="d", title="")
plotsma         = input.bool  (false      , group=g_2, inline="d", title='Correlation EMA')
corr_ma_len     = input.int   (100        , group=g_2, inline="d", title="")
i_covCol        = input.color (#f87b27  , group=g_2, inline="e", title="")
plotcovariance  = input.bool  (false      , group=g_2, inline="e", title='Covariance')
i_varCurCol     = input.color (#6331eb  , group=g_2, inline="f", title="")
i_plotVarCur    = input.bool  (false      , group=g_2, inline="f", title='Variation')

var g_symb = "Symbol"
u01 = input.bool(true , title = "01 - Reference Market", group=g_symb, inline='s01')
u02 = input.bool(true , title = "02", group=g_symb, inline='s02')
u03 = input.bool(true , title = "03", group=g_symb, inline='s03')
u04 = input.bool(true , title = "04", group=g_symb, inline='s04')
u05 = input.bool(true , title = "05", group=g_symb, inline='s05')
u06 = input.bool(true , title = "06", group=g_symb, inline='s06')
u07 = input.bool(false, title = "07", group=g_symb, inline='s07')
u08 = input.bool(false, title = "08", group=g_symb, inline='s08')
u09 = input.bool(false, title = "09", group=g_symb, inline='s09')
u10 = input.bool(false, title = "10", group=g_symb, inline='s10')
u11 = input.bool(false, title = "11", group=g_symb, inline='s11')
u12 = input.bool(false, title = "12", group=g_symb, inline='s12')
u13 = input.bool(false, title = "13", group=g_symb, inline='s13')
u14 = input.bool(false, title = "14", group=g_symb, inline='s14')
u15 = input.bool(false, title = "15", group=g_symb, inline='s15')
u16 = input.bool(false, title = "16", group=g_symb, inline='s16')
u17 = input.bool(false, title = "17", group=g_symb, inline='s17')
u18 = input.bool(false, title = "18", group=g_symb, inline='s18')
u19 = input.bool(false, title = "19", group=g_symb, inline='s19')
u20 = input.bool(false, title = "20", group=g_symb, inline='s20')

s01 = input.symbol('BTCUSDT'  , title="", group=g_symb, inline='s01')
s02 = input.symbol('AAVEUSDT' , title="", group=g_symb, inline='s02')
s03 = input.symbol('AVAXUSDT' , title="", group=g_symb, inline='s03')
s04 = input.symbol('BNBUSDT'  , title="", group=g_symb, inline='s04')
s05 = input.symbol('DASHUSDT' , title="", group=g_symb, inline='s05')
s06 = input.symbol('DOGEUSDT' , title="", group=g_symb, inline='s06')
s07 = input.symbol('ENSUSDT'  , title="", group=g_symb, inline='s07')
s08 = input.symbol('ETHUSDT'  , title="", group=g_symb, inline='s08')
s09 = input.symbol('FILUSDT'  , title="", group=g_symb, inline='s09')
s10 = input.symbol('FTMUSDT'  , title="", group=g_symb, inline='s10')
s11 = input.symbol('LUNCUSDT' , title="", group=g_symb, inline='s11')
s12 = input.symbol('MATICUSDT', title="", group=g_symb, inline='s12')
s13 = input.symbol('NEARUSDT' , title="", group=g_symb, inline='s13')
s14 = input.symbol('RUNEUSDT' , title="", group=g_symb, inline='s14')
s15 = input.symbol('SHIBUSDT' , title="", group=g_symb, inline='s15')
s16 = input.symbol('SOLUSDT'  , title="", group=g_symb, inline='s16')
s17 = input.symbol('THETAUSDT', title="", group=g_symb, inline='s17')
s18 = input.symbol('UNIUSDT'  , title="", group=g_symb, inline='s18')
s19 = input.symbol('WAVESUSDT', title="", group=g_symb, inline='s19')
s20 = input.symbol('XRPUSDT'  , title="", group=g_symb, inline='s20')

symCount =
 (u01 ? 1 : 0 ) + (u02 ? 1 : 0 ) + (u03 ? 1 : 0 ) + (u04 ? 1 : 0 ) + (u05 ? 1 : 0 ) + (u06 ? 1 : 0 ) + (u07 ? 1 : 0 ) + (u08 ? 1 : 0 ) + (u09 ? 1 : 0 ) + (u10 ? 1 : 0 ) + 
 (u11 ? 1 : 0 ) + (u12 ? 1 : 0 ) + (u13 ? 1 : 0 ) + (u14 ? 1 : 0 ) + (u15 ? 1 : 0 ) + (u16 ? 1 : 0 ) + (u17 ? 1 : 0 ) + (u18 ? 1 : 0 ) + (u19 ? 1 : 0 ) + (u20 ? 1 : 0 )



// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
//                                              REQUEST DATA - 20 SYMBOLS                                     
// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼

c = barstate.isconfirmed ? 0 : 1
[c_01, name_01, namef_01, type_01] = request.security(s01, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_02, name_02, namef_02, type_02] = request.security(s02, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_03, name_03, namef_03, type_03] = request.security(s03, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_04, name_04, namef_04, type_04] = request.security(s04, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_05, name_05, namef_05, type_05] = request.security(s05, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_06, name_06, namef_06, type_06] = request.security(s06, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_07, name_07, namef_07, type_07] = request.security(s07, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_08, name_08, namef_08, type_08] = request.security(s08, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_09, name_09, namef_09, type_09] = request.security(s09, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_10, name_10, namef_10, type_10] = request.security(s10, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_11, name_11, namef_11, type_11] = request.security(s11, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_12, name_12, namef_12, type_12] = request.security(s12, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_13, name_13, namef_13, type_13] = request.security(s13, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_14, name_14, namef_14, type_14] = request.security(s14, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_15, name_15, namef_15, type_15] = request.security(s15, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_16, name_16, namef_16, type_16] = request.security(s16, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_17, name_17, namef_17, type_17] = request.security(s17, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_18, name_18, namef_18, type_18] = request.security(s18, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_19, name_19, namef_19, type_19] = request.security(s19, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])
[c_20, name_20, namef_20, type_20] = request.security(s20, timeframe.period, [source[c], syminfo.basecurrency, syminfo.ticker, syminfo.type])



// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
//                                          CALCULATE STANDARD DEVIATIONS                                 
// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼

// St DEV FUNCTION: returns 
stDev(_Price) =>
    Var = math.round((_Price - _Price[1]) / _Price * 100, 2)
    var arr = array.new_float(lookback)
    array.shift(arr)
    array.push(arr, Var)
    StDev    = array.stdev(arr)
    [arr, StDev, Var]

[arr_cur, StDev_cur, Var_cur] = stDev(source)
[arr_01 , StDev_01 , Var_01 ] = stDev(c_01)
[arr_02 , StDev_02 , Var_02 ] = stDev(c_02)
[arr_03 , StDev_03 , Var_03 ] = stDev(c_03)
[arr_04 , StDev_04 , Var_04 ] = stDev(c_04)
[arr_05 , StDev_05 , Var_05 ] = stDev(c_05)
[arr_06 , StDev_06 , Var_06 ] = stDev(c_06)
[arr_07 , StDev_07 , Var_07 ] = stDev(c_07)
[arr_08 , StDev_08 , Var_08 ] = stDev(c_08)
[arr_09 , StDev_09 , Var_09 ] = stDev(c_09)
[arr_10 , StDev_10 , Var_10 ] = stDev(c_10)
[arr_11 , StDev_11 , Var_11 ] = stDev(c_11)
[arr_12 , StDev_12 , Var_12 ] = stDev(c_12)
[arr_13 , StDev_13 , Var_13 ] = stDev(c_13)
[arr_14 , StDev_14 , Var_14 ] = stDev(c_14)
[arr_15 , StDev_15 , Var_15 ] = stDev(c_15)
[arr_16 , StDev_16 , Var_16 ] = stDev(c_16)
[arr_17 , StDev_17 , Var_17 ] = stDev(c_17)
[arr_18 , StDev_18 , Var_18 ] = stDev(c_18)
[arr_19 , StDev_19 , Var_19 ] = stDev(c_19)
[arr_20 , StDev_20 , Var_20 ] = stDev(c_20)

// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
//                                                      ARRAYS                                    
// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼

// ARRAY: Turn On/Off Markets
u_arr = array.new_bool  (0)
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: Symbol Types
type_arr = array.new_string(0)
array.push(type_arr, type_01)
array.push(type_arr, type_02)
array.push(type_arr, type_03)
array.push(type_arr, type_04)
array.push(type_arr, type_05)
array.push(type_arr, type_06)
array.push(type_arr, type_07)
array.push(type_arr, type_08)
array.push(type_arr, type_09)
array.push(type_arr, type_10)
array.push(type_arr, type_11)
array.push(type_arr, type_12)
array.push(type_arr, type_13)
array.push(type_arr, type_14)
array.push(type_arr, type_15)
array.push(type_arr, type_16)
array.push(type_arr, type_17)
array.push(type_arr, type_18)
array.push(type_arr, type_19)
array.push(type_arr, type_20)
// ARRAY: Symbol Names
name_arr  = array.new_string(0)
array.push(name_arr, name_01)
array.push(name_arr, name_02)
array.push(name_arr, name_03)
array.push(name_arr, name_04)
array.push(name_arr, name_05)
array.push(name_arr, name_06)
array.push(name_arr, name_07)
array.push(name_arr, name_08)
array.push(name_arr, name_09)
array.push(name_arr, name_10)
array.push(name_arr, name_11)
array.push(name_arr, name_12)
array.push(name_arr, name_13)
array.push(name_arr, name_14)
array.push(name_arr, name_15)
array.push(name_arr, name_16)
array.push(name_arr, name_17)
array.push(name_arr, name_18)
array.push(name_arr, name_19)
array.push(name_arr, name_20)
// ARRAY: tandard Deviation
StDev_arr = array.new_float (0)
array.push(StDev_arr, StDev_01)
array.push(StDev_arr, StDev_02)
array.push(StDev_arr, StDev_03)
array.push(StDev_arr, StDev_04)
array.push(StDev_arr, StDev_05)
array.push(StDev_arr, StDev_06)
array.push(StDev_arr, StDev_07)
array.push(StDev_arr, StDev_08)
array.push(StDev_arr, StDev_09)
array.push(StDev_arr, StDev_10)
array.push(StDev_arr, StDev_11)
array.push(StDev_arr, StDev_12)
array.push(StDev_arr, StDev_13)
array.push(StDev_arr, StDev_14)
array.push(StDev_arr, StDev_15)
array.push(StDev_arr, StDev_16)
array.push(StDev_arr, StDev_17)
array.push(StDev_arr, StDev_18)
array.push(StDev_arr, StDev_19)
array.push(StDev_arr, StDev_20)
// ARRAY: SYMBOLS NAMES
namef_arr  = array.new_string(0)
array.push(namef_arr, namef_01)
array.push(namef_arr, namef_02)
array.push(namef_arr, namef_03)
array.push(namef_arr, namef_04)
array.push(namef_arr, namef_05)
array.push(namef_arr, namef_06)
array.push(namef_arr, namef_07)
array.push(namef_arr, namef_08)
array.push(namef_arr, namef_09)
array.push(namef_arr, namef_10)
array.push(namef_arr, namef_11)
array.push(namef_arr, namef_12)
array.push(namef_arr, namef_13)
array.push(namef_arr, namef_14)
array.push(namef_arr, namef_15)
array.push(namef_arr, namef_16)
array.push(namef_arr, namef_17)
array.push(namef_arr, namef_18)
array.push(namef_arr, namef_19)
array.push(namef_arr, namef_20)



// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
//                                              CALCULATE CORRELATIONS                                    
// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼

// MATRIX: Variation Rates
m = matrix.new<float>(20,lookback)
for i = 0 to lookback-1
    matrix.set(m,01-1,i, array.get(arr_01, i))
    matrix.set(m,02-1,i, array.get(arr_02, i))
    matrix.set(m,03-1,i, array.get(arr_03, i))
    matrix.set(m,04-1,i, array.get(arr_04, i))
    matrix.set(m,05-1,i, array.get(arr_05, i))
    matrix.set(m,06-1,i, array.get(arr_06, i))
    matrix.set(m,07-1,i, array.get(arr_07, i))
    matrix.set(m,08-1,i, array.get(arr_08, i))
    matrix.set(m,09-1,i, array.get(arr_09, i))
    matrix.set(m,10-1,i, array.get(arr_10, i))
    matrix.set(m,11-1,i, array.get(arr_11, i))
    matrix.set(m,12-1,i, array.get(arr_12, i))
    matrix.set(m,13-1,i, array.get(arr_13, i))
    matrix.set(m,14-1,i, array.get(arr_14, i))
    matrix.set(m,15-1,i, array.get(arr_15, i))
    matrix.set(m,16-1,i, array.get(arr_16, i))
    matrix.set(m,17-1,i, array.get(arr_17, i))
    matrix.set(m,18-1,i, array.get(arr_18, i))
    matrix.set(m,19-1,i, array.get(arr_19, i))
    matrix.set(m,20-1,i, array.get(arr_20, i))

// MATRIX: Covariance & Correlation 
s = matrix.new<float>(symCount,symCount)
for i = 0 to symCount-1
    for ii = 0 to symCount-1
        matrix.set(s,i,ii,math.round(array.covariance(matrix.row(m,ii),matrix.row(m,i))/(array.get(StDev_arr,ii)*array.get(StDev_arr,i))*100,2))
  
covariance  = array.covariance(arr_cur, arr_01)
correlation = covariance / (StDev_cur * StDev_01)
corr_ma     = ta.sma(correlation, corr_ma_len)



// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
//                                                       PLOTS                                     
// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼

// SCREENER TABLE
boxLocation = switch boxLoc
    "Bottom center" => position.bottom_center
    "Bottom left"   => position.bottom_left  
    "Bottom right"  => position.bottom_right 
    "Middle center" => position.middle_center
    "Middle left"   => position.middle_left  
    "Middle right"  => position.middle_right 
    "Top center"    => position.top_center   
    "Top left"      => position.top_left     
    "Top right"     => position.top_right 

var tbl = table.new(boxLocation, symCount+1, symCount+1)
if barstate.islast and useScreener
    for i = 0 to symCount-1
        if array.get(u_arr, i)
            table.cell(tbl, i+1, 0, array.get(type_arr, i) == "forex" or array.get(type_arr, i) ==  "cfd" ? array.get(namef_arr, i) : array.get(name_arr, i), text_halign=text.align_center, text_color=color.gray, text_size=size.small)
            table.cell(tbl, 0, i+1, array.get(type_arr, i) == "forex" or array.get(type_arr, i) ==  "cfd" ? array.get(namef_arr, i) : array.get(name_arr, i), text_halign=text.align_left  , text_color=color.gray, text_size=size.small)
            for ii = 0 to symCount-1
                if array.get(u_arr, ii)
                    table.cell(tbl,i+1,ii+1, str.tostring(math.round(matrix.get(s,i,ii)))+"%", text_color=
                     matrix.get(s,i,ii) > 0 and matrix.get(s,i,ii) <  99 ? color.from_gradient(matrix.get(s,i,ii),loColGrad   ,hiColGrad   , i_lowcorr , i_highcorr) : 
                     matrix.get(s,i,ii) < 0 and matrix.get(s,i,ii) > -99 ? color.from_gradient(matrix.get(s,i,ii),hiColGrad*-1,loColGrad*-1, i_highcorr, i_lowcorr ) : 
                     color.new(#999999,90))
 
// VARIATION
plot(i_plotVarCur ? Var_cur : na, color=i_varCurCol, style=plot.style_columns, title='Current Variation'  , linewidth=1)

// COVARIANCE
plot(plotcovariance ? covariance : na, color=color.new(i_covCol, 50), style=plot.style_area, title='Covariance')

// CORRELATION
p_corr = plot(plotcorrelation ? correlation : na, color=color.new(color.white, 100), style=plot.style_line, title='Correlation')

// CORRELATION SMA
plot(plotsma ? corr_ma : na, title = "SMA (Corr.)", color=i_maCol)

// LINES
p_midline = plot (0 , title="0" , color=color.white)
p_topline = hline(1 , title="1" , color=color.new(color.white, 50))
p_botline = hline(-1, title="-1", color=color.new(color.white, 50))

// CORRELATION COLOR GRADIENT
fill(p_midline, p_corr, top_value=hiColGrad/100, bottom_value=loColGrad/100            , top_color = color.new(i_highcorr, correlation > 0 ? 40  : 100), bottom_color = color.new(i_lowcorr , correlation > 0 ? 60 : 100))
fill(p_midline, p_corr, top_value=loColGrad/100*-1          , bottom_value=hiColGrad/100*-1, top_color = color.new(i_lowcorr , correlation < 0 ? 60 : 100), bottom_color = color.new(i_highcorr, correlation < 0 ? 40  : 100))

// LABEL
l = label.new(plotsma ? bar_index : na, plotsma ? corr_ma : na, color=i_maCol, text=str.tostring(math.round(corr_ma * 100, 0)) + '%', style=label.style_label_left, tooltip="Simple Average Correlation over the last " + str.tostring(corr_ma_len) + " Bars")
label.delete(l[1])



// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
//                                                   ALERTS                                     
// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼

// END