[英]Error when trying to download stock price data from Morningstar
import math
import pandas_datareader.data as web
import matplotlib.pyplot as plot
from datetime import datetime
from matplotlib import style
style.use('ggplot')
import csv
with open('sp500const.csv', newline='') as File:
reader = csv.reader(File)
dataa=[]
for i in reader:
dataa.extend(i)
Dates=[]
Prices=[]
Tick=[]
for stock in dataa[2:100]:
for year in range(2010,2019):
if year==2012:
start = datetime(year,5,1)
end=datetime(year,5,1)
f = web.DataReader(stock, 'morningstar', start, end)
temp=f['Close']
Prices.append(float(temp))
Dates.append(end.strftime('%m/%d/%Y'))
Tick.append(stock)
elif year==2013:
start=datetime(year,5,3)
end=datetime(year,5,3)
f = web.DataReader(stock, 'morningstar', start, end)
temp=f['Close']
Prices.append(float(temp))
Dates.append(end.strftime('%m/%d/%Y'))
Tick.append(stock)
elif year==2014:
start=datetime(year,5,5)
end=datetime(year,5,5)
f = web.DataReader(stock, 'morningstar', start, end)
temp=f['Close']
Prices.append(float(temp))
Dates.append(end.strftime('%m/%d/%Y'))
Tick.append(stock)
else:
start=datetime(year,5,4)
end=datetime(year,5,4)
f = web.DataReader(stock, 'morningstar', start, end)
temp=f['Close']
Prices.append(float(temp))
Dates.append(end.strftime('%m/%d/%Y'))
Tick.append(stock)
#datetime(Year,Month,Days)
x=[Tick,Dates,Prices]
z=[list(i) for i in zip(*x)]
print(z)
I want to download data for the S&500 Constituents, whose tickers are in the sp500const.csv file[Tickers as reported from wikipedia.com] for the dates 2011-2018. 我想下载S&500成分股的数据,其股票代码在sp500const.csv文件中(从wikipedia.com报告的股票代号),日期为2011-2018。 When i change the number of tickers to be downloaded (for example in the code now, I want to download stock prices for tickers dataa[2:100]).
当我更改要下载的代码的数量时(例如,现在在代码中,我想下载代码dataa [2:100]的股票价格)。 When i choose for instance data[2:4] the code works perfectly.
当我选择实例数据[2:4]时,代码可以完美运行。 I get the following error:
我收到以下错误:
Traceback (most recent call last): 追溯(最近一次通话):
File "<ipython-input-5-5592627b16bf>", line 44, in <module>
f = web.DataReader(stock, 'morningstar', start, end)
File "C:\Users\coxs\Miniconda3\lib\site-packages\pandas_datareader\data.py", line 391, in DataReader
session=session, interval="d").read()
File "C:\Users\coxs\Miniconda3\lib\site-packages\pandas_datareader\mstar\daily.py", line 219, in read
df = self._dl_mult_symbols(symbols=symbols)
File "C:\Users\coxs\Miniconda3\lib\site-
packages\pandas_datareader\mstar\daily.py", line 126, in _dl_mult_symbols
jsondata=jsondata)
File "C:\Users\coxs\Miniconda3\lib\site-packages\pandas_datareader\mstar\daily.py", line 164, in _restruct_json
volumes = jsondata["VolumeList"]["Datapoints"]
TypeError: 'NoneType' object is not subscriptable
Use a "try" "except" statement. 使用“ try”“ except”语句。 This should avoid returning the error and stopping the loop.
这应该避免返回错误并停止循环。 The problem should be from the API output though so you will have missing data.
问题应该出在API输出上,因此您将缺少数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.