Skip to content

Latest commit

 

History

12 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Stock Cointegration Analysis

This Python script analyzes the cointegration of two selected stocks using historical price data from Yahoo Finance. It applies statistical methods, including linear regression and the Engle-Granger cointegration test, to determine if the chosen stocks share a long-term equilibrium relationship. In the future, I will continue working on this code so it can automatically scan indexes find statistically significant cointegration, and perform statistical arbitrage.

Features

  • Retrieves monthly stock price data from Yahoo Finance
  • Calculates log prices for normalization
  • Performs linear regression to model the relationship between the two stocks
  • Computes residuals and tests for cointegration
  • Displays test statistics, p-values, and critical values for interpretation

Here is an example output of regressing MSFT on Apple:

  • Test statistic: -3.2633338723501963
  • P-value: 0.05995929898588575
  • Critical values: [-3.90080167 -3.33856151 -3.04613746] The stocks are cointegrated at a 90% confidence level
image

Requirements

Ensure you have the following Python libraries installed:

pip install pandas numpy matplotlib yfinance scikit-learn statsmodels
![Uploading image.png…]()

About

This Python script analyzes the cointegration of two stocks by performing a statistical test on their historical log prices. It retrieves ten years of monthly stock price data from Yahoo Finance using the yfinance library, then processes and analyzes the data.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages