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

//@version=5
indicator("Ultimate Correlation Coefficient", shorttitle = "UCC", format = format.price, precision = 2)

//symbolInput = input.symbol("INDEX:BTCUSD", "Main Comparison", confirm = true)
symbolInput1 = input.symbol("SP:SPX", "Symbol", confirm = true)
symbolInput2 = input.symbol("TVC:DXY", "Symbol", confirm = true)
symbolInput3 = input.symbol("CURRENCYCOM:GOLD", "Symbol", confirm = true)
symbolInput4 = input.symbol("TVC:US10Y", "Symbol", confirm = true)
symbolInput5 = input.symbol("TVC:VIX", "Symbol", confirm = true)
sourceInput = input.source(close, "Source")

//not implemented yet
//showSPX = input.bool(true, "show SPX", inline = "g", group = "r")
//showDXY = input.bool(true, "show DXY", inline = "g", group = "r")
//showGOLD = input.bool(true, "show GOLD", inline = "g", group = "r")
//showUS10Y = input.bool(true, "show US10Y", inline = "g", group = "r")
//showVIX = input.bool(true, "show VIX", inline = "g", group = "r")

//different lengths 
length15 = 15
length30 = 30
length60 = 60
length90 = 90
length120 = 120
length180 = 180
length360 = 360
length600= 600
length1000 = 1000
// This takes into account additional settings enabled on chart, e.g. divident adjustment or extended session 
adjustedSeries1 = ticker.modify(symbolInput1)
adjustedSeries2 = ticker.modify(symbolInput2)
adjustedSeries3 = ticker.modify(symbolInput3)
adjustedSeries4 = ticker.modify(symbolInput4)
adjustedSeries5 = ticker.modify(symbolInput5)


requestedData1 = request.security(adjustedSeries1, timeframe.period, sourceInput)
requestedData2 = request.security(adjustedSeries2, timeframe.period, sourceInput)
requestedData3 = request.security(adjustedSeries3, timeframe.period, sourceInput)
requestedData4 = request.security(adjustedSeries4, timeframe.period, sourceInput)
requestedData5 = request.security(adjustedSeries5, timeframe.period, sourceInput)


correlation151 = ta.correlation(sourceInput, requestedData1, length15)
correlation152 = ta.correlation(sourceInput, requestedData2, length15)
correlation153 = ta.correlation(sourceInput, requestedData3, length15)
correlation154 = ta.correlation(sourceInput, requestedData4, length15)
correlation155 = ta.correlation(sourceInput, requestedData5, length15)

correlation301 = ta.correlation(sourceInput, requestedData1, length30)
correlation302 = ta.correlation(sourceInput, requestedData2, length30)
correlation303 = ta.correlation(sourceInput, requestedData3, length30)
correlation304 = ta.correlation(sourceInput, requestedData4, length30)
correlation305 = ta.correlation(sourceInput, requestedData5, length30)

correlation601 = ta.correlation(sourceInput, requestedData1, length60)
correlation602 = ta.correlation(sourceInput, requestedData2, length60)
correlation603 = ta.correlation(sourceInput, requestedData3, length60)
correlation604 = ta.correlation(sourceInput, requestedData4, length60)
correlation605 = ta.correlation(sourceInput, requestedData5, length60)

correlation901 = ta.correlation(sourceInput, requestedData1, length90)
correlation902 = ta.correlation(sourceInput, requestedData2, length90)
correlation903 = ta.correlation(sourceInput, requestedData3, length90)
correlation904 = ta.correlation(sourceInput, requestedData4, length90)
correlation905 = ta.correlation(sourceInput, requestedData5, length90)

correlation1201 = ta.correlation(sourceInput, requestedData1, length120)
correlation1202 = ta.correlation(sourceInput, requestedData2, length120)
correlation1203 = ta.correlation(sourceInput, requestedData3, length120)
correlation1204 = ta.correlation(sourceInput, requestedData4, length120)
correlation1205 = ta.correlation(sourceInput, requestedData5, length120)

correlation1801 = ta.correlation(sourceInput, requestedData1, length180)
correlation1802 = ta.correlation(sourceInput, requestedData2, length180)
correlation1803 = ta.correlation(sourceInput, requestedData3, length180)
correlation1804 = ta.correlation(sourceInput, requestedData4, length180)
correlation1805 = ta.correlation(sourceInput, requestedData5, length180)

correlation3601 = ta.correlation(sourceInput, requestedData1, length360)
correlation3602 = ta.correlation(sourceInput, requestedData2, length360)
correlation3603 = ta.correlation(sourceInput, requestedData3, length360)
correlation3604 = ta.correlation(sourceInput, requestedData4, length360)
correlation3605 = ta.correlation(sourceInput, requestedData5, length360)

correlation6001 = ta.correlation(sourceInput, requestedData1, length600)
correlation6002 = ta.correlation(sourceInput, requestedData2, length600)
correlation6003 = ta.correlation(sourceInput, requestedData3, length600)
correlation6004 = ta.correlation(sourceInput, requestedData4, length600)
correlation6005 = ta.correlation(sourceInput, requestedData5, length600)

correlation10001 = ta.correlation(sourceInput, requestedData1, length1000)
correlation10002 = ta.correlation(sourceInput, requestedData2, length1000)
correlation10003 = ta.correlation(sourceInput, requestedData3, length1000)
correlation10004 = ta.correlation(sourceInput, requestedData4, length1000)
correlation10005 = ta.correlation(sourceInput, requestedData5, length1000)



//Plot
plot(0.000, color = color.new(color.black, 50), display = display.none)
plot(correlation151, "SPX", color = color.red, display = display.status_line)        
plot(correlation301, "SPX", color = color.red, display = display.status_line)     
plot(correlation601, "SPX", color = color.red, display = display.status_line)     
plot(correlation901, "SPX", color = color.red, display = display.status_line)     
plot(correlation1201, "SPX", color = color.red, display = display.status_line)     
plot(correlation1801, "SPX", color = color.red, display = display.status_line)     
plot(correlation3601, "SPX", color = color.red, display = display.status_line)     
plot(correlation6001, "SPX", color = color.red, display = display.status_line)     
plot(correlation10001, "SPX", color = color.red, display = display.status_line)     
plot(0.000, color = color.new(color.black, 50), display = display.none)

plot(correlation152, "DXY", color = color.green, display = display.status_line)      
plot(correlation302, "DXY", color = color.green, display = display.status_line)  
plot(correlation602, "DXY", color = color.green, display = display.status_line)  
plot(correlation902, "DXY", color = color.green, display = display.status_line)  
plot(correlation1202, "DXY", color = color.green, display = display.status_line)  
plot(correlation1802, "DXY", color = color.green, display = display.status_line)  
plot(correlation3602, "DXY", color = color.green, display = display.status_line)  
plot(correlation6002, "DXY", color = color.green, display = display.status_line)  
plot(correlation10002, "DXY", color = color.green, display = display.status_line)  
plot(0.000, color = color.new(color.black, 50), display = display.none)

plot(correlation153, "GOLD", color = color.yellow, display = display.status_line)
plot(correlation303, "GOLD", color = color.yellow, display = display.status_line)
plot(correlation603, "GOLD", color = color.yellow, display = display.status_line)
plot(correlation903, "GOLD", color = color.yellow, display = display.status_line)
plot(correlation1203, "GOLD", color = color.yellow, display = display.status_line)
plot(correlation1803, "GOLD", color = color.yellow, display = display.status_line)
plot(correlation3603, "GOLD", color = color.yellow, display = display.status_line)
plot(correlation6003, "GOLD", color = color.yellow, display = display.status_line)
plot(correlation10003, "GOLD", color = color.yellow, display = display.status_line)
plot(0.000, color = color.new(color.black, 50), display = display.none)

plot(correlation154, "US10Y", color = color.fuchsia, display = display.status_line)
plot(correlation304, "US10Y", color = color.fuchsia, display = display.status_line)
plot(correlation604, "US10Y", color = color.fuchsia, display = display.status_line)
plot(correlation904, "US10Y", color = color.fuchsia, display = display.status_line)
plot(correlation1204, "US10Y", color = color.fuchsia, display = display.status_line)
plot(correlation1804, "US10Y", color = color.fuchsia, display = display.status_line)
plot(correlation3604, "US10Y", color = color.fuchsia, display = display.status_line)
plot(correlation6004, "US10Y", color = color.fuchsia, display = display.status_line)
plot(correlation10004, "US10Y", color = color.fuchsia, display = display.status_line)
plot(0.000, color = color.new(color.black, 50), display = display.none)

plot(correlation155, "VIX", color = color.blue, display = display.status_line)
plot(correlation305, "VIX", color = color.blue, display = display.status_line)
plot(correlation605, "VIX", color = color.blue, display = display.status_line)
plot(correlation905, "VIX", color = color.blue, display = display.status_line)
plot(correlation1205, "VIX", color = color.blue, display = display.status_line)
plot(correlation1805, "VIX", color = color.blue, display = display.status_line)
plot(correlation3605, "VIX", color = color.blue, display = display.status_line)
plot(correlation6005, "VIX", color = color.blue, display = display.status_line)
plot(correlation10005, "VIX", color = color.blue, display = display.status_line)
plot(0.000, color = color.new(color.black, 50), display = display.none)

plot(math.avg(correlation151,correlation301,correlation601,correlation901,correlation1201,correlation1801,correlation3601,correlation6001,correlation10001),  "Average SPX", color = color.red, display = display.all - display.status_line)
plot(math.avg(correlation152,correlation302,correlation602,correlation902,correlation1202,correlation1802,correlation3602,correlation6002,correlation10002), "Average DXY", color = color.green, display = display.all - display.status_line)
plot(math.avg(correlation153,correlation303,correlation603,correlation903,correlation1203,correlation1803,correlation3603,correlation6003,correlation10003),  "Average GOLD", color = color.yellow, display = display.all - display.status_line)
plot(math.avg(correlation154,correlation304,correlation604,correlation904,correlation1204,correlation1804,correlation3604,correlation6004,correlation10004), "AverageUS10Y", color = color.fuchsia, display = display.all - display.status_line)
plot(math.avg(correlation155,correlation305,correlation605,correlation905,correlation1205,correlation1805,correlation3605,correlation6005,correlation10005), "Average VIX", color = color.blue, display = display.all - display.status_line)

hline(1)
hline(0, color = color.new(color.gray, 50))
hline(-1)