Calculate the Autocorrelation Function (ACF) of each asset’s daily returns for a range of lags.

The ACF measures the correlation between a return series and a lagged version of itself. A significant ACF at a given lag indicates that returns are not fully independent over time, which is relevant for assessing return predictability and volatility clustering (as opposed to a trading-signal use case, which is why this lives in the Risk module rather than Technicals).

The formula is as follows:

\[\operatorname{ACF}(k) = \text{Σ} (r_{t} - \text{Mean}) \cdot (r_{t + k} - \text{Mean}) / \text{Σ} (r_{t} - \text{Mean}) ^{2}\]

Where r_t are the n daily returns (missing values dropped), the numerator runs over t = 1, …, n - k, the denominator over all n returns, and k = 1, …, lags (10 by default).

Autocorrelation (ACF) in Python or via the MCP

Install the Finance Toolkit with:

pip install financetoolkit -U

Then call get_autocorrelation as shown below.

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["SPY", "QQQ", "IWM"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
    benchmark_ticker=None,
)

toolkit.risk.get_autocorrelation()

Or, after setting up the Finance Toolkit MCP, ask:

Were the daily returns of SPY, QQQ and IWM autocorrelated over the first ten lags from 2021 to 2025?

Which returns:

Lag SPY QQQ IWM
1 -0.031 -0.0387 -0.0092
2 -0.0069 -0.001 -0.0254
3 -0.0626 -0.0629 -0.0055
4 -0.0344 -0.0216 -0.0437
5 -0.0075 -0.0071 -0.016
6 -0.0104 0 -0.0112
7 -0.0109 0.0031 -0.013
8 -0.0063 -0.0118 -0.0168
9 0.0399 0.0505 0.0285
10 -0.0111 -0.0214 -0.0059

Parameters

get_autocorrelation accepts the following parameters.

ParameterTypeDescriptionExample
lagsintThe number of lags to calculate the ACF for. Defaults to 10.20
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise).2
Share