ITMScript
Measure rolling correlation with SPY
Measure how closely the one-bar returns of a symbol move with those of SPY over a rolling window.
Measure how closely the returns of this symbol move with the returns of SPY over a rolling window.
//@itmscript 2
study("Rolling correlation", overlay=false)
len = input.int(60, "Window", min=10, max=500)
bench = series(symbol="SPY")
own = ta.ret(close, 1)
other = ta.ret(bench.close, 1)
corr = ta.correlation(own, other, len)
plot(corr, "Correlation", color=corr >= 0 ? color.blue : color.orange, width=2, style=line)
hline(0, "Zero", color=color.gray, style=dotted)
hline(1, "One", color=color.gray, style=dashed)
hline(-1, "Minus one", color=color.gray, style=dashed)What you see
A line in its own pane stays between minus one and one. It is blue above zero and orange below. Dashed lines mark one and minus one. A dotted line marks zero.
A value near one means the two move together. Near zero means no link. Near minus one means they move in opposite directions. The study takes returns and not prices. Two prices that both trend look related whether or not they are. The line starts once the window has filled.
Change these inputs
- Window. The number of bars in the rolling window, from 10 to 500. A longer window moves slowly and is steadier.
The benchmark is written in the script. Replace SPY in the series line to compare with another symbol.