我可以从Alpha Vantage获得4个股票信息,然后其余的DataFrames没有得到我要的股票信息。因此,我得到的串联df被解释为Nonetype(因为前四个df的格式与后两个的格式不同)。这不是我的问题。我只能收到4个请求的事实是...如果我可以解决-所连接的df将保持不变。
我的代码
import pandas as pd
import datetime
import requests
from alpha_vantage.timeseries import TimeSeries
import time
tickers = []
def alvan_csv(stocklist):
api_key = 'demo' # For use with Alpha Vantage stock-info retireval.
for ticker in stocklist:
#data=requests.get('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=%s&apikey={}'.format(api_key) %(ticker))
df = pd.read_csv('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&datatype=csv&symbol=%s&apikey={}'.format(api_key) %(ticker))#, index_col = 0) &outputsize=full
df['ticker'] = ticker
tickers.append(df)
# concatenate all the dfs
df = pd.concat(tickers)
print('\ndata before json parsing for === %s ===\n%s' %(ticker,df))
df['adj_close'] = df['adjusted_close']
del df['adjusted_close']
df['date'] = df['timestamp']
del df['timestamp']
df = df[['date','ticker','adj_close','volume','dividend_amount','split_coefficient','open','high','low']] #
df=df.sort_values(['ticker','date'], inplace=True)
time.sleep(20.3)
print('\ndata after col reshaping for === %s ===\n%s' %(ticker,df))
return df
if __name__ == '__main__':
stocklist = ['vws.co','nflx','mmm','abt','msft','aapl']
df = alvan_csv(stocklist)
NB。请注意,要使用Alpha Vantage API,您需要一个免费的API密钥,您可以在此处使用:https://www.alphavantage.co/support/#api-key 用您的API密钥替换演示API密钥,以使此代码起作用。
有什么想法可以使它起作用吗?
答案 0 :(得分:0)
很明显,Alpha Vantage的公平使用津贴很低,因为他们没有测量查询。分钟。因此,实际上,仅允许前四只股票全速运行。其余股票在下载前需要暂停,以免违反公平使用政策。
我现在在两次股票查询之间引入了暂停。现在,如果我停顿10秒钟,我将获得大约55%的股票。通话之间,如果我暂停15秒,则为100%。
我将精确测试可以将暂停设置为多低,以允许100%的股票通过。
我必须说,与我们在finance.yahoo.com上拥有的超高速火车相比,这让我印象深刻。下载速度真的很慢。要获得我500的股票行情,我需要2½小时。但是我猜乞g不能成为选择者。这是一项免费服务,我将对此进行管理。