Calculating Rolling R-Squared for Price Data
Summary
This note explains R-squared as a measure of how much variation in a dependent series is explained by a fitted linear relationship. It presents an indicator that computes the squared correlation between a rolling sequence of prices and a time index, returning values from zero to one. A period setting determines the window, and the example uses the platform’s custom close series. The accompanying calculation accumulates sums of the time indices, prices, their squares, and their products, then combines these into the squared correlation coefficient.
The document is useful as a compact implementation reference for a trend-fit statistic, but it does not describe a trading rule, validation procedure, or empirical results. R-squared measures how closely the observations fit a linear pattern; by itself it does not identify direction, forecast returns, or establish that a strategy is profitable. The explanation also simplifies the interpretation as correlation, so users should distinguish this regression-fit statistic from a signed correlation measure. The supplied code is platform-specific and should be checked against the target platform’s built-in function and data conventions.
Key ideas
- R-squared describes the fraction of observed variation explained by a fitted relationship.
- The example calculates a rolling squared correlation between price observations and a time index.
- The window length controls how many recent observations enter the calculation.
- A high R-squared indicates a closer linear fit but does not reveal trend direction or guarantee predictive value.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.