Python, project help

ok, having a issue with this Let me give you a rundown.

im running into issues with this. First off I have redone the API key so that works. However when I try to call to it it gives me this:

JSONDecodeError Traceback (most recent call last)
Input In [64], in <cell line: 25>()
25 for symbol in stocks[‘Ticker’]:
26 api_url = f’https://sandbox.iexapis.com/stable/stock/{symbol}/quote?token={IEX_CLOUD_API_TOKEN}
—> 27 data = requests.get(api_url).json()
28 final_dataframe = final_dataframe.append(
29 pd.Series([symbol,
30 data[‘latestPrice’],
(…)
33 index = my_columns),
34 ignore_index = True)

File /usr/local/Caskroom/miniconda/base/envs/algorithmic-trading-python/lib/python3.8/site-packages/requests/models.py:888, in Response.json(self, **kwargs)
886 if encoding is not None:
887 try:
→ 888 return complexjson.loads(
889 self.content.decode(encoding), **kwargs
890 )
891 except UnicodeDecodeError:
892 # Wrong UTF codec detected; usually because it’s not UTF-8
893 # but some other 8-bit codec. This is an RFC violation,
894 # and the server didn’t bother to tell us what codec was
895 # used.
896 pass

File /usr/local/Caskroom/miniconda/base/envs/algorithmic-trading-python/lib/python3.8/json/init.py:357, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352 del kw[‘encoding’]
354 if (cls is None and object_hook is None and
355 parse_int is None and parse_float is None and
356 parse_constant is None and object_pairs_hook is None and not kw):
→ 357 return _default_decoder.decode(s)
358 if cls is None:
359 cls = JSONDecoder

File /usr/local/Caskroom/miniconda/base/envs/algorithmic-trading-python/lib/python3.8/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)
332 def decode(self, s, _w=WHITESPACE.match):
333 “”“Return the Python representation of s (a str instance
334 containing a JSON document).
335
336 “””
→ 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):

File /usr/local/Caskroom/miniconda/base/envs/algorithmic-trading-python/lib/python3.8/json/decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
→ 355 raise JSONDecodeError(“Expecting value”, s, err.value) from None
356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I then tried to go back from the begging and fallow it threw.
final_dataframe = pd.DataFrame(columns = my_columns)
#for stock in stocks[‘Ticker’]:

api_url = f’https://sandbox.iexapis.com/stable/stock/{stock}/quote?token={IEX_CLOUD_API_TOKEN}

data = requests.get(api_url).json()

final_dataframe.append(

pd.Series(

[

stock,

data[‘latestPrice’],

data[‘marketCap’],

‘N/A’

],

index = my_columns),

ignore_index = True

)

for symbol in stocks[‘Ticker’]:
api_url = f’https://sandbox.iexapis.com/stable/stock/{symbol}/quote?token={IEX_CLOUD_API_TOKEN}
data = requests.get(api_url).json()
final_dataframe = final_dataframe.append(
pd.Series([symbol,
data[‘latestPrice’],
data[‘marketCap’],
‘N/A’],
index = my_columns),
ignore_index = True)

Still nothing.

I have tried everything I can think of. More then happy to pay, please give me a place to start. TY

(algorithmic-trading-python) ➜ algorithmic-trading-python git:(master) ✗ pip li
st
Package Version


appnope 0.1.3
argon2-cffi 21.3.0
argon2-cffi-bindings 21.2.0
asttokens 2.0.7
attrs 22.1.0
backcall 0.2.0
beautifulsoup4 4.11.1
bleach 5.0.1
certifi 2022.6.15
cffi 1.15.1
chardet 3.0.4
debugpy 1.6.2
decorator 5.1.1
defusedxml 0.7.1
entrypoints 0.4
executing 0.9.1
fastjsonschema 2.16.1
idna 2.8
importlib-resources 5.9.0
ipykernel 6.10.0
ipython 8.4.0
ipython-genutils 0.2.0
ipywidgets 7.7.1
jedi 0.18.1
Jinja2 3.1.2
jsonschema 4.9.1
jupyter 1.0.0
jupyter-client 7.2.2
jupyter-console 6.1.0
jupyter-core 4.10.0
jupyterlab-pygments 0.1.2
jupyterlab-widgets 1.1.1
MarkupSafe 2.1.1
matplotlib-inline 0.1.3
mistune 0.8.4
nbclient 0.5.13
nbconvert 6.4.4
nbformat 5.4.0
nest-asyncio 1.5.5
notebook 6.4.12
numpy 1.17.4
packaging 21.3
pandas 0.25.3
pandocfilters 1.5.0
parso 0.8.3
pexpect 4.8.0
pickleshare 0.7.5
pip 22.1.2
pkgutil_resolve_name 1.3.10
prometheus-client 0.14.1
prompt-toolkit 3.0.30
psutil 5.9.1
ptyprocess 0.7.0
pure-eval 0.2.2
pycparser 2.21
Pygments 2.12.0
pyparsing 3.0.9
pyrsistent 0.18.1
python-dateutil 2.8.2
pytz 2022.1
pyzmq 23.2.0
qtconsole 5.3.1
QtPy 2.1.0
requests 2.22.0
scipy 1.5.2
Send2Trash 1.8.0
setuptools 61.2.0
six 1.16.0
soupsieve 2.3.1
stack-data 0.3.0
terminado 0.15.0
testpath 0.6.0
tornado 6.2
traitlets 5.3.0
urllib3 1.25.11
wcwidth 0.2.5
webencodings 0.5.1
wheel 0.37.1
widgetsnbextension 3.6.1
XlsxWriter 1.2.2
zipp 3.8.1
(algorithmic-trading-python) ➜ algorithmic-trading-python git:(master) ✗ python
–version
Python 3.8.13

Yes I am making contact with API. I had to do some stuff but its working

{‘avgTotalVolume’: 69229836, ‘calculationPrice’: ‘close’, ‘change’: 3.64, ‘changePercent’: 0.02186, ‘close’: 173.3, ‘closeSource’: ‘ffcoiali’, ‘closeTime’: 1702190371433, ‘companyName’: ‘Apple Inc’, ‘currency’: ‘USD’, ‘delayedPrice’: 172.89, ‘delayedPriceTime’: 1702718565638, ‘extendedChange’: -0.22, ‘extendedChangePercent’: -0.00126, ‘extendedPrice’: 173.3, ‘extendedPriceTime’: 1678961592409, ‘high’: 175.37, ‘highSource’: ‘yaprc1 l ededenuii5 tme’, ‘highTime’: 1709853402297, ‘iexAskPrice’: None, ‘iexAskSize’: None, ‘iexBidPrice’: None, ‘iexBidSize’: None, ‘iexClose’: 178.31, ‘iexCloseTime’: 1686049225683, ‘iexLastUpdated’: None, ‘iexMarketPercent’: None, ‘iexOpen’: 177.62, ‘iexOpenTime’: 1691149051737, ‘iexRealtimePrice’: None, ‘iexRealtimeSize’: None, ‘iexVolume’: None, ‘lastTradeTime’: 1712409069377, ‘latestPrice’: 178.1, ‘latestSource’: ‘Close’, ‘latestTime’: ‘August 12, 2022’, ‘latestUpdate’: 1701819845361, ‘latestVolume’: 70245510, ‘low’: 170.3, ‘lowSource’: 'ra1idimn5epeuyc let de ', ‘lowTime’: 1740415244904, ‘marketCap’: 2853752482657, ‘oddLotDelayedPrice’: 179.04, ‘oddLotDelayedPriceTime’: 1717698975554, ‘open’: 175.37, ‘openTime’: 1700230634707, ‘openSource’: ‘flaiicof’, ‘peRatio’: 29.59, ‘previousClose’: 172.7, ‘previousVolume’: 57735518, ‘primaryExchange’: ‘DNASQA’, ‘symbol’: ‘AAPL’, ‘volume’: 70994271, ‘week52High’: 182.63, ‘week52Low’: 128.92, ‘ytdChange’: -0.005488444357282392, ‘isUSMarketOpen’: False}

Hi @lac_ekim - could you please reformat your message? It’s hard to understand what you are asking and I almost flagged it as SPAM. Help others help you. Thanks!

I am having a issue with this line of code:

final_dataframe = pd.DataFrame(columns = my_columns)

for symbol in stocks['Ticker']:
    api_url = f'https://sandbox.iexapis.com/stable/stock/{symbol}/quote?token={IEX_CLOUD_API_TOKEN}'
    data = requests.get(api_url).json()
    final_dataframe = final_dataframe.append(
                                        pd.Series([symbol, 
                                                   data['latestPrice'], 
                                                   data['marketCap'], 
                                                   'N/A'], 
                                                  index = my_columns), 
                                        ignore_index = True)



The API is working however:

symbol='AAPL'
api_url = f'https://sandbox.iexapis.com/stable/stock/{symbol}/quote?token={IEX_CLOUD_API_TOKEN}'
data = requests.get(api_url).json()
print(data)
# data

This is what it returns:
“{‘avgTotalVolume’: 69229836, ‘calculationPrice’: ‘close’, ‘change’: 3.64, ‘changePercent’: 0.02186, ‘close’: 173.3, ‘closeSource’: ‘ffcoiali’, ‘closeTime’: 1702190371433, ‘companyName’: ‘Apple Inc’, ‘currency’: ‘USD’, ‘delayedPrice’: 172.89, ‘delayedPriceTime’: 1702718565638, ‘extendedChange’: -0.22, ‘extendedChangePercent’: -0.00126, ‘extendedPrice’: 173.3, ‘extendedPriceTime’: 1678961592409, ‘high’: 175.37, ‘highSource’: ‘yaprc1 l ededenuii5 tme’, ‘highTime’: 1709853402297, ‘iexAskPrice’: None, ‘iexAskSize’: None, ‘iexBidPrice’: None, ‘iexBidSize’: None, ‘iexClose’: 178.31, ‘iexCloseTime’: 1686049225683, ‘iexLastUpdated’: None, ‘iexMarketPercent’: None, ‘iexOpen’: 177.62, ‘iexOpenTime’: 1691149051737, ‘iexRealtimePrice’: None, ‘iexRealtimeSize’: None, ‘iexVolume’: None, ‘lastTradeTime’: 1712409069377, ‘latestPrice’: 178.1, ‘latestSource’: ‘Close’, ‘latestTime’: ‘August 12, 2022’, ‘latestUpdate’: 1701819845361, ‘latestVolume’: 70245510, ‘low’: 170.3, ‘lowSource’: 'ra1idimn5epeuyc let de ', ‘lowTime’: 1740415244904, ‘marketCap’: 2853752482657, ‘oddLotDelayedPrice’: 179.04, ‘oddLotDelayedPriceTime’: 1717698975554, ‘open’: 175.37, ‘openTime’: 1700230634707, ‘openSource’: ‘flaiicof’, ‘peRatio’: 29.59, ‘previousClose’: 172.7, ‘previousVolume’: 57735518, ‘primaryExchange’: ‘DNASQA’, ‘symbol’: ‘AAPL’, ‘volume’: 70994271, ‘week52High’: 182.63, ‘week52Low’: 128.92, ‘ytdChange’: -0.005488444357282392, ‘isUSMarketOpen’: False}”

Why do I have the disconnect from the " Making Our First API Call

Now it’s time to structure our API calls to IEX cloud.

We need the following information from the API:

  • Market capitalization for each stock
  • Price of each stock"

to the
" Looping Through The Tickers in Our List of Stocks

Using the same logic that we outlined above, we can pull data for all S&P 500 stocks and store their data in the DataFrame using a for loop."

This is what I had to update to get the API to work, secrets.py: IEX_CLOUD_API_TOKEN = ‘Tsk_8613a5b7e6414cf89f2819ca7d948fbd’

Sorry for your confusing @robertomachorro

which tutorial does this come from? or is it just a general Python question?

its a general question. Im wondering if anyone can see what’s going on. I have the API working now but on the next line it throws a error when I loop threw the API.

Are you talking about the following line?

Is that a call to the Pandas library?

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html

I have to say that you aren’t providing much information, so only so much help can be given. At this point, I’m actually just guessing.