I have a code which calculates a cointegration matrix from 50 stocks but the data used is from yahoo finance. I want to change the data from yahoo to a personal spreadsheet which I have, the location of the spreadsheet is: 'C:/Users/Home/Desktop/resultado.xlsx'
import numpy as np
import pandas as pd
import openpyxl
import matplotlib.pyplot as plt
import statsmodels
from statsmodels.tsa.stattools import coint
import statsmodels.api as sm
import pytrends
import quandl
from pandas_datareader import data as pdr
import datetime
import yfinance as yf
from GetData import GetDataYahoo as gdy
from Cointegration import FindCointPairs as fcp
# Get tickers
d=pd.read_excel('C:/Users/Home/Desktop/tickers50.xlsx')
stocks=pd.DataFrame(d,columns=['Bloomberg','Yahoo'])
stocks=stocks['Yahoo']
print(stocks)
# Get Data
start_dt=datetime.datetime(2020, 1, 1)
end_dt=datetime.datetime(2021, 1, 11)
sdata=gdy(stocks,start_dt,end_dt)
#Fill NA's with zeros
sdata.fillna(0,inplace=True)
#Create cointegration matrix
scores, pvalues, pairs=fcp(sdata,0.05)
p2=pvalues.T
# Heatmap to show pvalues of cointegration test
import seaborn
fig,ax = plt.subplots(figsize=(80,80))
seaborn.heatmap(p2,ax=ax, annot=True, fmt='.3g', cbar=False, xticklabels=stocks,yticklabels=stocks,cmap='hot',mask=(p2>=0.95))
plt.savefig('coint_bovespa50.png')
plt.show
print(pairs)
# Export List of Pairs to Excel
p=pd.DataFrame(pairs)
p.to_excel("CointPairs_output.xlsx")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…