Calculate the Engle-Granger test for cointegration between every ordered pair of tickers in the Toolkit instance.

Two individually non-stationary series (e.g. two stock prices, each following a random walk) are cointegrated if some linear combination of them is stationary, i.e. they share a long-run equilibrium relationship even though each wanders on its own in the short run. This is the classic statistical foundation for pairs-trading: if two assets are cointegrated, deviations of the spread from its equilibrium level tend to revert, making the spread itself tradeable.

For more information about the method, see the following paper:

  • Engle, R.F. and Granger, C.W.J. (1987). “Co-integration and Error Correction: Representation, Estimation, and Testing.” Econometrica, 55(2), 251-276.

Also known as: EG test, residual-based cointegration test, pairs-trading test.

No programming experience? With the Finance Toolkit MCP server, AI assistants such as Claude and ChatGPT can calculate the Engle-Granger Cointegration for you. Just ask in plain English.

Calculate the Engle-Granger Cointegration in Python

The Engle-Granger Cointegration is available in the Econometrics module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_engle_granger_cointegration as shown below.

from financetoolkit import Toolkit

toolkit = Toolkit(["AAPL", "MSFT"], api_key="FINANCIAL_MODELING_PREP_KEY")

toolkit.econometrics.get_engle_granger_cointegration(period="quarterly")

Which returns:

(the 1%/10% critical value columns follow the same pattern as 5%, omitted here for width)

Dependent Independent EG Statistic P-Value Crit. 5% Cointegrated (5%)
AAPL MSFT -1.4334 0.7858 -3.8927 False
MSFT AAPL -2.8297 0.1564 -3.8927 False

Parameters

get_engle_granger_cointegration accepts the following parameters:

  • period (str, optional): The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”.
  • column (str, optional): The historical data column to test. Defaults to “Adj Close”.
  • max_lag (int, optional): The maximum number of lagged differences to consider in the underlying ADF test on the residuals. Defaults to statsmodels’ automatic selection.
  • include_benchmark (bool, optional): Whether to include “Benchmark” among the tickers paired up. Defaults to False.
  • rounding (int | None, optional): The number of decimals to round the results to. Defaults to None.

The Econometrics module page introduces the module, and the sidebar lists all of its functions.

Share