Penny Wise, API Foolish

Aboli Marathe
4 min readAug 19, 2020

--

Using APIs to fetch historical stock market data in Python

This week at PISB, we explored the colourful world of APIs and tried scraping historical stock market data using the Yahoo Finance API. The best submissions were presented by Pushkar Jain and Tanuj Pancholi, from the team Data Wizards. The students were mentored by Aboli Marathe and Ayush Das, and we would like to showcase the work here in the form of a short guide to getting finance data through APIs.

We will begin by importing the necessary libraries.

Now let’s select our target companies. For this example, we have chosen 5 companies for getting data.

Apple — Technology
Tesla — Automobile
Johnson & Johnson — Healthcare
Facebook — Communication Service
EBAY — E-commerce

This dictionary will store the names of the tickers (an abbreviation used to uniquely identify publicly traded shares of a particular stock on a particular stock market), so that we can use these in our code in the future.

Let’s try getting the information about our companies and storing it in a csv first. We will pass the ticker symbols that we stored in the dictionary, to the API Ticker module.

We have successfully got the data in our csv now which looks like this. This particular code returns 121 features about the company, including the sector, location and currency.

We can also find the top institutional stakeholder for a company using the Yahoo Finance API.

The code returns the stakeholders with the shares, values and other relevant details in a data frame.

Now let’s analyse the quarter-wise results of a company.

We’ve visualized these results on a line plot using matplotlib.

We’ve heard a lot about the negative impact of the pandemic on the share prices. Let’s compare how the share price trends changed over time before and during the pandemic.

The results will show us the trends of the opening price of Johnson & Johnson in the form of a line plot.

Now let’s dive into examining the volatility of the stocks. More is the fluctuation between “High” and “Low” more volatile the stock is. We tried to compare the volatility of stocks in the Pre- and Mid-Covid periods of time, to understand how the epidemic affected the stocks’ volatility.

Insights:

  1. During March — April of 2020 the stock prices of companies were very low but most the companies had a V-shaped recovery after that.
  2. We see that Covid-19 affected businesses in a negative manner across all the sectors.
  3. Stocks of all companies became highly volatile during Covid period. Johnson & Johnson was the most volatile stock all of the companies
  4. In 2019 Q2 was bad for all companies and Q4 was the best for all, except Ebay.
  5. Vanguard Group, Inc. is the largest Institutional Stakeholder in most of the 5 selected companies.

References:

Thanks for reading this article. The complete code of all the contributors is available on Github, with even more visualizations. We hope that you liked our work!

--

--

Aboli Marathe

Machine Learning Engineer @ Omdena| AI for Social Good | Writer @ The Innovation