//@version=5
indicator("Correlation Analysis", overlay=false)
// User inputs for asset symbols
asset1 = input.symbol("AAPL", title="Asset 1")
asset2 = input.symbol("USDJPY", title="Asset 2")
asset3 = input.symbol("US10Y", title="Asset 3")
asset4 = input.symbol("GOLD", title="Asset 4")
// User input for correlation period
corr_period = input.int(30, title="Correlation Period", minval=2)
// Fetch historical data for each asset
asset1_close = request.security(asset1, timeframe.period, close)
asset2_close = request.security(asset2, timeframe.period, close)
asset3_close = request.security(asset3, timeframe.period, close)
asset4_close = request.security(asset4, timeframe.period, close)
// Check for division by zero errors and calculate log returns
asset1_return = na(asset1_close[1])? na : log(asset1_close / asset1_close[1])
asset2_return = na(asset2_close[1])? na : log(asset2_close / asset2_close[1])
asset3_return = na(asset3_close[1])? na : log(asset3_close / asset3_close[1])
asset4_return = na(asset4_close[1])? na : log(asset4_close / asset4_close[1])
// Correlation calculations
correlation_1_2 = ta.correlation(asset1_return, asset2_return, corr_period)
correlation_1_3 = ta.correlation(asset1_return, asset3_return, corr_period)
correlation_1_4 = ta.correlation(asset1_return, asset4_return, corr_period)
correlation_2_3 = ta.correlation(asset2_return, asset3_return, corr_period)
correlation_2_4 = ta.correlation(asset2_return, asset4_return, corr_period)
correlation_3_4 = ta.correlation(asset3_return, asset4_return, corr_period)
// Plot correlations
plot(correlation_1_2, color=color.red, title="Corr Asset 1 & Asset 2")
plot(correlation_1_3, color=color.green, title="Corr Asset 1 & Asset 3")
plot(correlation_1_4, color=color.blue, title="Corr Asset 1 & Asset 4")
plot(correlation_2_3, color=color.orange, title="Corr Asset 2 & Asset 3")
plot(correlation_2_4, color=color.purple, title="Corr Asset 2 & Asset 4")
plot(correlation_3_4, color=color.yellow, title="Corr Asset 3 & Asset 4")
// Display correlation matrix with labels
var label corr_matrix = label.new(x=na, y=na, text="", style=label.style_label_down, size=size.normal, color=color.white)
maxBarIndex = ta.highest(bar_index, 1)
if (bar_index == maxBarIndex)
label.set_xy(corr_matrix, bar_index, high)
label.set_text(corr_matrix,
"Corr 1-2: " + str.tostring(correlation_1_2, format.percent) + "n" +
"Corr 1-3: " + str.tostring(correlation_1_3, format.percent) + "n" +
"Corr 1-4: " + str.tostring(correlation_1_4, format.percent) + "n" +
"Corr 2-3: " + str.tostring(correlation_2_3, format.percent) + "n" +
"Corr 2-4: " + str.tostring(correlation_2_4, format.percent) + "n" +
"Corr 3-4: " + str.tostring(correlation_3_4, format.percent)
)
On Line 46 “label.set_text(corr_matrix,” of the code it gives me a syntax error at input ‘end of line without line continuation’. The indentations don’t look wrong to me that’s why I’m not really sure where the issue is stemming from. This has me stumped, is there any way to fix this?
New contributor
Nesar Hemmat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.