Hi I have a problem with this piece of code that needs to get stock prices based on a defined time period and a ticker code. The program actually works when I use my IEX API KEY, but not when I use my TEST IEX API KEY, I get the following error message
Unable to read URL: https://cloud.iexapis.com/stable/stock/market/batch?symbols=AAPL&types=chart&range=1y&token=Tpk_157dbb6ac5914bb6b5e309b5eb1484f5
Response Text: b'Test tokens may only be used in the sandbox environment. Please use https://sandbox.iexapis.com' error
'''
How to download stock data
'''
import pandas as pd
import pandas_datareader.data as web
import datetime as dt
from datetime import datetime
import os
os.environ["IEX_API_KEY"] = "Tpk_157dbb6ac5914bb6b5e309b5eb1484f5"
def get_stock_data():
tickers = ['AAPL'] #capitalize tickers
start = dt.datetime(2019,1,1) # can import 5 years max with iex
end = dt.datetime.today()
if not os.path.exists('stockdata'):
os.makedirs('stockdata')
for ticker in tickers:
print(ticker)
try :
df = web.DataReader(ticker, "iex", start, end)
print(df.head())
df.to_csv('stockdata/{}.to_csv'.format(ticker))
print(ticker, 'downloaded')
except Exception as e:
print(e, 'error')
get_stock_data()
I probably should have told the API that this is the iexcloud-sandbox that I need to access,as described in the Error Message, but the description link dosent say anything about it: https://intercom.help/iexcloud/en/articles/2915433-testing-with-the-iex-cloud-sandbox and I don't know how get it to work, can anybody help?
Set your IEX_API_VERSION
environment variable to iexcloud-sandbox
:
os.environ['IEX_API_VERSION'] = 'iexcloud-sandbox'