powered by
Computes rolling correlations between each symbol and a benchmark series (e.g., SPY) using simple returns over a fixed lookback window.
SPY
calc_rolling_correlation( data, benchmark_symbol = "SPY", lookback = 60, min_periods = NULL, method = c("pearson", "spearman") )
A data.table with Date and one column per non-benchmark symbol, containing rolling correlations. Insufficient data yields NAs.
data.table
Date
NA
A data.frame or data.table with a Date column and one column per symbol containing prices. Must include benchmark_symbol.
data.frame
benchmark_symbol
Character, the benchmark column name (default "SPY").
"SPY"
Integer window size (>= 2) for rolling correlations.
Minimum number of valid observations within the window to compute a correlation. Default is ceiling(lookback * 0.67).
ceiling(lookback * 0.67)
Correlation method, "pearson" (default) or "spearman".
"pearson"
"spearman"
Returns are computed as simple returns \((P_t - P_{t-1})/P_{t-1}\). Windows with fewer than min_periods valid pairs are marked NA.
min_periods
calc_momentum(), calc_rolling_volatility()
calc_momentum()
calc_rolling_volatility()
data(sample_prices_weekly) corr <- calc_rolling_correlation( data = sample_prices_weekly, benchmark_symbol = "SPY", lookback = 20 ) head(corr)
Run the code above in your browser using DataLab