Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save quantra-go-algo/3a669d82b1cc9740404626b4c5cf432c to your computer and use it in GitHub Desktop.
Save quantra-go-algo/3a669d82b1cc9740404626b4c5cf432c to your computer and use it in GitHub Desktop.
In this code, you will check whether the non-stationary independent variables are cointegrated or not
S_3 = Df['S_3'].dropna()
S_9 = Df['S_9'].dropna()
# Since S_3 and S_9 are moving averages, we could check their cointegration with the original Close series
close_price = Df['next_day_price']
# Conduct Engle-Granger Cointegration Test between S_3 and Close price
coint_result_3 = coint(S_3, close_price)
# Conduct Engle-Granger Cointegration Test between S_9 and Close price
coint_result_9 = coint(S_9, close_price)
# Extract p-values
p_value_3 = coint_result_3[1]
p_value_9 = coint_result_9[1]
# Print the results
print(f"Cointegration p-value between S_3 and next_day_price: {p_value_3}")
print(f"Cointegration p-value between S_9 and next_day_price: {p_value_9}")
# Interpretation
alpha = 0.05
if p_value_3 < alpha:
print("S_3 and next_day_price are cointegrated.")
else:
print("S_3 and next_day_price are not cointegrated.")
if p_value_9 < alpha:
print("S_9 and next_day_price are cointegrated.")
else:
print("S_9 and next_day_price are not cointegrated.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment