In this exercise, we are going to play around with the stock market API iexcloud.io.
The goal here is to get comfortable reading API documentation.
Let's find the API documentation page of iexcloud.io
Solution
Documentation pages are often hidden in the footer or in some menu.Typing 'the_website_name API documentation' in the google search bar is a quick way to find it too.
solution: https://iexcloud.io/docs/api/
The endpoints of the API we want to use are protected behind a paywall.
As Le Wagon, we kindly provide you with a proxy to that API for you to use only during the challenge today. We trust you!
Here's how it works:
- API would say: use
https://cloud.iexapis.com/stable/stock/aapl/stats?token=...
- Copy this URL, and replace
cloud.iexapis.com
withiex.lewagon.com
- You can try it now
Now let's find in the IEXCloud API the URL for the last 3 months of Apple stock prices.
When you find the URL copy and paste it in a new tab and look at the data you get from the API. It should be a JSON looking like this:
Show example
[
{
date: "2014-04-17",
open: 68.2926,
high: 69.3117,
low: 68.1875,
close: 68.9414,
volume: 71106721,
unadjustedVolume: 10158103,
change: 0.778798,
changePercent: 1.143,
vwap: 68.8375,
label: "Apr 17, 14",
changeOverTime: 0
},
{
date: "2014-04-21",
open: 68.9939,
high: 69.8869,
low: 68.8127,
close: 69.7596,
volume: 45668931,
unadjustedVolume: 6524133,
change: 0.8182,
changePercent: 1.187,
vwap: 69.5143,
label: "Apr 21, 14",
changeOverTime: 0.011868050257174998
},
// [...]
]
Solution
You can find this information here in the documentation: https://iexcloud.io/docs/api/#historical-pricesThe URL is:
https://iex.lewagon.com/stable/stock/aapl/chart/3m
For this exercise we will work in a Notebook.
jupyter notebook
Go ahead and create a new Python Notebook named stocks.ipynb
in the ~/code/<user.github_nickname>/{{local_path_to("02-Data-Toolkit/02-Data-Sourcing/01-Stock-Market-API")}} folder.
Start with the usual imports in the first cell:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
We will reuse the API URL for the last 3 months of Apple stock prices here.
To make a call to the API you can use the following code:
import requests
url = "YOUR_API_URL"
api_data = requests.get(url).json()
You can now create a dataframe apple_df
from this data.
Solution
apple_df = pd.DataFrame(api_data)
With this dataframe we can plot the evolution of the stock price. But before doing that, we need to do 2 things:
- Convert the
date
column to a datetime object - Set the
date
column as the index
To do this, you can use Pandas.to_datetime()
- pd.to_datetime documentation: http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html#pandas.to_datetime
- Format documentation: http://strftime.org/
Solution
apple_df['date'] = pd.to_datetime(apple_df['date'], format="%Y-%m-%d")
To do this you can use the DataFrame method set_index
- documentation: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html
Solution
apple_df = apple_df.set_index('date')
First let's plot with only the values in the close
column
Solution
apple_df['close'].plot()
Now we can make a plot with the values in the open
, close
, high
, and low
columns
Solution
apple_df[['open', 'close', 'high', 'low']].plot()
As we can see, our plot is really hard to read. We can improve its readability with the figsize
argument of the plot()
method.
- documentation: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html
Solution
apple_df[['open', 'close', 'high', 'low']].plot(figsize=(12,4))
Add and run the following cell to your notebook:
from nbresult import ChallengeResult
result = ChallengeResult('apple',
index_name=apple_df.index.name,
index_type=apple_df.index.dtype,
columns=apple_df.columns
)
result.write()
print(result.check())
You can commit
and push
your code 🚀
Let's find out what kind of data we can get from this API 🕵️♂️
- Amazon stock prices since last year?
- Meta (Facebook) market cap?
- Apple research and development spendings quarterly?
- Most recent news about Tesla?
- Performance of the 'Energy' sector?
All Solutions
https://iex.lewagon.com/stable/stock/amzn/chart/1y
https://iex.lewagon.com/stable/stock/meta/stats
https://iex.lewagon.com/stable/stock/aapl/financials
https://iex.lewagon.com/stable/stock/tsla/news/last/1
https://iex.lewagon.com/stable/stock/market/sector-performance
💡 Don't forget to push your code to GitHub
We'd like to compare the evolution of the GAFA stocks (Google, Apple, Meta, Amazon) by plotting them on the same chart.
Reuse the code from above to build a dataframe with one column per stock and keeping the dates as the index.
Maybe you can use some normalization technique at t = 0
to better compare the relative performance of each stock!