Skip to content

pavankumarhm/data-stock-market-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.


Apple stock the last 3 months

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/

API setup

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:

  1. API would say: use https://cloud.iexapis.com/stable/stock/aapl/stats?token=...
  2. Copy this URL, and replace cloud.iexapis.com with iex.lewagon.com
  3. You can try it now

Using the API

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
    },
// [...]
]

⚠️ Take your time before reading the solution, finding what we want in an API documentation can usually take 10 to 15 minutes of reading

Solution You can find this information here in the documentation: https://iexcloud.io/docs/api/#historical-prices
The URL is:
https://iex.lewagon.com/stable/stock/aapl/chart/3m

Using API data in pandas

Setup

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

Converting the date to a datetime object

To do this, you can use Pandas.to_datetime()

Solution apple_df['date'] = pd.to_datetime(apple_df['date'], format="%Y-%m-%d")

Set the date column as the index

To do this you can use the DataFrame method set_index

Solution apple_df = apple_df.set_index('date')

Now we can plot 🎉

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.

Solution apple_df[['open', 'close', 'high', 'low']].plot(figsize=(12,4))

Test your code!

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 🚀


Back to the API

Let's find out what kind of data we can get from this API 🕵️‍♂️

What is the URL for the:

  1. Amazon stock prices since last year?
  2. Meta (Facebook) market cap?
  3. Apple research and development spendings quarterly?
  4. Most recent news about Tesla?
  5. Performance of the 'Energy' sector?
All Solutions
  1. https://iex.lewagon.com/stable/stock/amzn/chart/1y
  2. https://iex.lewagon.com/stable/stock/meta/stats
  3. https://iex.lewagon.com/stable/stock/aapl/financials
  4. https://iex.lewagon.com/stable/stock/tsla/news/last/1
  5. https://iex.lewagon.com/stable/stock/market/sector-performance

💡 Don't forget to push your code to GitHub

(Optional) Plotting multiple line charts

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!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages