Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Last active March 1, 2024 07:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quantra-go-algo/64b8fb7d219b0e288140ca38baf28e29 to your computer and use it in GitHub Desktop.
Save quantra-go-algo/64b8fb7d219b0e288140ca38baf28e29 to your computer and use it in GitHub Desktop.
# Create a data frame df to store the two-time series
data = yf.download(stock_list, start=start_date, end=end_date)['Adj Close']
# Perform the Johansen Cointegration Test with a specified number of zero
specified_number = 0 # Testing for zero cointegrating relationships
coint_test_result = coint_johansen(data, specified_number, 1)
# Extract the trace statistics and eigen statistics
trace_stats = coint_test_result.lr1
eigen_stats = coint_test_result.lr2
# Print the test results
print("Johansen Cointegration Test Results (Testing for Zero Cointegrating Relationships):")
print(f"Trace Statistics: {coint_test_result.lr1}")
print(f"Critical Values: {coint_test_result.cvt}")
# Define stock pairs
stock_pairs = [('AAPL', 'AMZN'), ('MSFT', 'AAPL'), ('AMZN', 'MSFT')]
# Separate the output sections
print("\n" + "-" * 50 + "\n")
# Interpret the results for each pair
for i, (stock1, stock2) in enumerate(stock_pairs):
trace_statistic = trace_stats[i]
eigen_statistic = eigen_stats[i]
print(f"Pair {i + 1} ({stock1} and {stock2}):")
print(f"Trace Statistic: {trace_statistic}")
print(f"Eigen Statistic: {eigen_statistic}")
print("\n" + "-" * 50 + "\n")
# Determine cointegration based on critical values or other criteria
# Add your cointegration assessment logic here
print("Cointegration Assessment: Testing for Zero Cointegrating Relationships (Null Hypothesis)\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment