Hey guys, this is what I am running into a little issue. Im running on python 3.8.13. Im currently doing “algorithmic-trading-python” course. I have setup the env as specified.
"
Equal-Weight S&P 500 Index Fund
Introduction & Library Imports
The S&P 500 is the world's most popular stock market index. The largest fund that is benchmarked to this index is the SPDR® S&P 500® ETF Trust. It has more than US$250 billion of assets under management.
The goal of this section of the course is to create a Python script that will accept the value of your portfolio and tell you how many shares of each S&P 500 constituent you should purchase to get an equal-weight version of the index fund.
Library Imports
The first thing we need to do is import the open-source software libraries that we'll be using in this tutorial.
In [1]:
import numpy as np
import pandas as pd
import requests
import xlsxwriter
import math
Importing Our List of Stocks
The next thing we need to do is import the constituents of the S&P 500.
These constituents change over time, so in an ideal world you would connect directly to the index provider (Standard & Poor's) and pull their real-time constituents on a regular basis.
Paying for access to the index provider's API is outside of the scope of this course.
There's a static version of the S&P 500 constituents available here. Click this link to download them now. Move this file into the starter-files folder so it can be accessed by other files in that directory.
Now it's time to import these stocks to our Jupyter Notebook file.
In [2]:
stocks = pd.read_csv('sp_500_stocks.csv')
#type()
stocks
Out[2]:
Ticker
0 A
1 AAL
2 AAP
3 AAPL
4 ABBV
... ...
500 YUM
501 ZBH
502 ZBRA
503 ZION
504 ZTS
505 rows × 1 columns
Acquiring an API Token
Now it's time to import our IEX Cloud API token. This is the data provider that we will be using throughout this course.
API tokens (and other sensitive information) should be stored in a secrets.py file that doesn't get pushed to your local Git repository. We'll be using a sandbox API token in this course, which means that the data we'll use is randomly-generated and (more importantly) has no cost associated with it.
Click here to download your secrets.py file. Move the file into the same directory as this Jupyter Notebook before proceeding.
In [6]:
from secrets import IEX_CLOUD_API_TOKEN
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
In [7]:
symbol='AAPL'
api_url = f'https://sandbox.iexapis.com/stable/stock/{symbol}/quote?token={IEX_CLOUD_API_TOKEN}'
data = requests.get(api_url).json()
data
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
Input In [7], in <cell line: 3>()
1 symbol='AAPL'
2 api_url = f'https://sandbox.iexapis.com/stable/stock/{symbol}/quote?token={IEX_CLOUD_API_TOKEN}'
----> 3 data = requests.get(api_url).json()
4 data
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)
"