This Python script identifies cointegrated stock pairs among S&P 500 companies β a common statistical arbitrage strategy used in pairs trading.
It automatically downloads S&P 500 tickers from Wikipedia, fetches their historical price data from Yahoo Finance, and performs the Engle-Granger cointegration test on all possible stock pairs.
- Scrapes S&P 500 tickers directly from Wikipedia
- Downloads historical price data using
yfinance - Computes cointegration tests for all stock combinations
- Outputs the top 10 pairs with the lowest p-values (most likely to be cointegrated)
- Fetch S&P 500 tickers from Wikipedia.
- Download adjusted closing prices for a chosen subset of tickers.
- Run cointegration tests (
statsmodels.tsa.stattools.coint) for every pair. - Sort by p-value to identify the strongest candidates.
Install dependencies using:
pip install requests pandas yfinance statsmodels lxml