Get daily and minute level historical stock data using Yahoo! Finance & Tiingo APIs, Pandas and plot them using Matplotlib

Stock market data APIs offer real-time or historical data on financial assets that are being traded on the stock exchanges. See The ultimate guide to (free) stock market APIs for 2020 for a list recommended API providers.

This article assumes that you have some Python fundamentals, Pandas and Matplotlib knowledge. The focus is on the following packages to access stock data:

yfinance — Yahoo! Finance

yfinance package provides access to Yahoo! Financial market data despite unavailability of Yahoo! Finance APIs since 2017. Follow the package link for more information on installation and documentation.

pandas-datareader — Tiingo

Tiingo has its own API to access data, but we will be using pandas-datareader for this article. An advantage of using the pandas-datareader package is that it converts data into a Panda’s DataFrame object. Access to Tiingo data requires an API key which can be obtained free by signing up for the starter package.

Accessing the Data

The code below shows how to get historical stock price data for Stock symbol MSFT (Microsoft) from 2019 to May 30 2020 using yfinance package.

import yfinance as yf
df = yf.download('MSFT', start='2019-01-01', end='2020-05-30')['Adj Close']

pandas-datareader adopts a similar approach for Tiingo data:

import pandas_datareader as pdr

df = pdr.get_data_tiingo('MSFT', start='2019-01-01', end='2020-05-30', api_key='your API key')['adjClose']

DataFrame from yfinance is indexed on Date and the DataFrame from pandas-datareader is indexed on Stock Symbol and Date.

#python #api #stock-market #data-science

Historical Stock Price Data using Python APIs
14.70 GEEK