嘿,你必须使用熊猫数据阅读器吗?下面是我编写的一个函数,用于从 Alpha Vantage 轻松提取历史股票价格。您所要做的就是插入您的符号和令牌。有关提取 Alpha Vantage 数据的更多功能,请随时查看我的链接:https ://github.com/hklchung/StockPricePredictor/blob/master/2020/alphavantage_funcs.py
def request_stock_price_hist(symbol, token, sample = False):
if sample == False:
q_string = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol={}&outputsize=full&apikey={}'
else:
q_string = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol={}&apikey={}'
print("Retrieving stock price data from Alpha Vantage (This may take a while)...")
r = requests.get(q_string.format(symbol, token))
print("Data has been successfully downloaded...")
date = []
colnames = list(range(0, 7))
df = pd.DataFrame(columns = colnames)
print("Sorting the retrieved data into a dataframe...")
for i in tqdm(r.json()['Time Series (Daily)'].keys()):
date.append(i)
row = pd.DataFrame.from_dict(r.json()['Time Series (Daily)'][i], orient='index').reset_index().T[1:]
df = pd.concat([df, row], ignore_index=True)
df.columns = ["open", "high", "low", "close", "adjusted close", "volume", "dividend amount", "split cf"]
df['date'] = date
return df
使用上述函数的方式如下:
df = request_stock_price_hist('IBM', 'REPLACE_YOUR_TOKEN')
df.to_csv('output.csv')