'Key error python algorithmic trading using google collab with iex cloud api
I am following this code from a youtube video it uses iex cloud API. I have followed the video till now but I am stuck at this point
enter code here
# Function sourced from
# https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-
chunks
def chunks(lst, n):
"""Yield successive n-sized chunks from last."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
symbol_groups = list(chunks(stocks['Ticker'], 100))
symbol_strings = []
for i in range(0, len(symbol_groups)):
symbol_strings.append(','.join(symbol_groups[i]))
for symbol_string in symbol_strings:
batch_api_call_url = f'https://sandbox.iexapis.com/stable/stock/market/batch/?
types=quote&symbols={symbol_string}&token={IEX_CLOUD_API_TOKEN}'
data = requests.get(batch_api_call_url).json()
for symbol in symbol_string.split(','):
final_dataframe = final_dataframe.append(
pd.Series([symbol,
data[symbol]['quote']['latestPrice'],
data[symbol]['quote']['marketCap'],
'N/A'],
index = my_columns),
ignore_index = True)
final_dataframe
I am getting a key error saying:
enter code
hereKeyError Traceback (most recent call last)
<ipython-input-16-23716615b418> in <module>()
12 final_dataframe = final_dataframe.append(
13 pd.Series([symbol,
---> 14 data[symbol]['quote']
['latestPrice'],
15 data[symbol]['quote']
['marketCap'],
16 'N/A'],
KeyError: 'HFC'
Solution 1:[1]
I don't know the exact reason but, skip these three stocks by doing...
for symbol in symbol_string.split(','):
if symbol == 'HFC' or symbol == 'VIAC' or symbol == 'WLTW':
continue
final_dataframe = final_dataframe.append( .....
Solution 2:[2]
HFC is a ticker symbol, so I'm going to assume that the IEX api doesn't have relevant info on HFC, just remove it from 'stocks['ticker']' or follow the advice above to skip it.
Solution 3:[3]
for symbol in symbol_string.split(","):
if symbol == 'HFC' or symbol == 'VIAC' or symbol == 'WLTW' or symbol == 'DISCA':
continue
else:
final_dataframe=final_dataframe.append(
pd.Series([
symbol,
data[symbol]["quote"]["latestPrice"],
data[symbol]["quote"]["marketCap"],
'N/A'
],
index = columns),
ignore_index=True
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | DexteR |
Solution 2 | Monkeyonmyshoulder |
Solution 3 | rajkg1 |