Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.8k views
in Technique[技术] by (71.8m points)

pandas - How Can I switch from yahoo finance data to my excel spreadsheet?

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...