Hey instead of using that library, below is a function I wrote to easily extract historical stock prices from Alpha Vantage. All you have to do is plug in your symbol and token. For more functions on extracting Alpha Vantage data, feel free to check out my link: 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
The way you would use this function is like this:
df = request_stock_price_hist('IBM', 'REPLACE_YOUR_TOKEN')
df.to_csv('output.csv')