Using Tiingo and Pandas DataReader to Access Financial Data

(Image link)

In searching for ways to obtain financial data for a data science project, I stumbled across two tools which turned out to be very handy: the Tiingo API and the Pandas DataReader sub package. In this blog post, I will describe these tools a bit, and then walk through some code examples of how to access data using these tools.

Tiingo is an enterprise-grade API that enables users to download financial data from the Internet. It is possible to access end-of-day (EOD) stock price data, historical intraday stock price data, news feeds with curated and tagged content. Additionally, there is a cryptocurrencies API (if you’re into that sort of thing), and two other features that were in beta as of this writing: Forex currency exchange data, and a Search function. Tiingo enables users to access data from a number of markets: the US NYSE, NASDAQ, NYSE American (formerly the AMEX), NYSE Arca (Exchange Traded Funds market), US equities exchanges, IEF, and mutual funds. Also, data from the Chinese markets Shenzhen and Shanghai are available via Tiingo. Data fields available include Open, High, Low, Close, Volume, Dividend, Splits (raw and adjusted). Fifty+ years of data is accessible, depending of course on how long the company in question has been in existence. Tiingo is enterprise-grade, owing to the quality of the feeds and the interface, but accessible; you get an impressive amount of functionality with a free account — 500 requests/hour, 20,000 requests/day, and 5 GB of data transfer/month. Spending a mere $10/month for a Power account gets you 20,000 requests/hour, 150,000 requests/day, and 100 GB of data transfer/month!

Pandas DataReader is a Python sub-package that used to be part of Pandas. This package enables users to extract financial data from a wide range of Internet sources directly into a Pandas DataFrame! So convenient. In addition to its ability to access Tiingo, Pandas DataReader connects with many other financial data tools, such as AlphaVantage, Federal Reserve Economic Data (FRED), Enigma, Eurostat, Organisation for Economic Co-operation and Development (OECD), Quandl, and Stooq. Like Tiingo, Pandas DataReader can directly access data from several financial markets. As of this writing, this included IEX, Moscow Eschange (MOEX), NASDAQ, and Thrift Savings Plan.

The last week of January 2021 was interesting for Reddit and for the stock market. (Screenshot of a post from Reddit’s Twitter feed)

To illustrate the usefulness of these two tools, I will walk through some code.

First, install Tiingo. This can be done from the command line, if you prefer.

!pip install tiingo

Import NumPy and Pandas.

import numpy as np
import pandas as pd

For some reason, my code can never figure out where my pip packages are installed, so I have to point it to the relevant directory by including the following bit of code suggested to me by a classmate. YMMV.

import sys
sys.path.append("/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages")

Next, import the TiingoClient and initialize:

from tiingo import TiingoClient# Set TIINGO_API_KEY in your environment variables in your .bash_profile, OR
# pass a dictionary with 'api_key' as a key into the TiingoClient.
config = {}
# To reuse the same HTTP Session across API calls (and have better performance), include a session key.
config['session'] = True### You will need to get an API key from https://api.tiingo.com
### The API key is free and includes a max number of requests per hour and day, and data transfer.
### Paid account will give more requests and data transfer.
# If you don't have your API key as an environment variable,
# pass it in via a configuration dictionary.
config['api_key'] = "Your API key here"
#Initialize
client = TiingoClient(config)

Now install Pandas DataReader (here, or from the command line if you prefer).

!pip install pandas-datareaderimport os
import pandas_datareader as pdr

You can pass individual ticker symbols or a list of ticker symbols into the functions. The get_ticker_metadata command returns information such as: the company’s name, class of the stock, exchange in which it is traded, the date the company went public, and a description of the company and its business. Let’s get the metadata for GameStop (ticker symbol GME):

gamestop_metadata = client.get_ticker_metadata("GME")
print(gamestop_metadata)

This returns (as of 3–9–2021):

{'endDate': '2021-03-09', 'exchangeCode': 'NYSE', 'startDate': '2002-02-13', 'ticker': 'GME', 'name': 'Gamestop Corporation - Class A', 'description': "GameStop Corp., a Fortune 500 company headquartered in Grapevine, Texas, is a digital-first omni-channel retailer, offering games and entertainment products in its over 4,800 stores and comprehensive e-Commerce properties across 10 countries. GameStop, through its family of brands offers the best selection of new and pre-owned video gaming consoles, accessories and video game titles, in both physical and digital formats. GameStop also offers fans a wide variety of POP! vinyl figures, collectibles, board games and more. Through GameStop’s unique buy-sell-trade program, gamers can trade in video game consoles, games, and accessories, as well as consumer electronics for cash or in-store credit. The company's consumer product network also includes www.gamestop.com and Game Informer® magazine, the world's leading print and digital video game publication. General information about GameStop Corp. can be obtained at the Company’s corporate website. Follow @GameStop and @GameStopCorp on Twitter and find GameStop on Facebook at www.facebook.com/GameStop."}

Instead of single ticker symbols, you can pass a list of ticker symbols into client.get_ticker_metadata:

company_symbols = ['EXEL', 'MSFT', 'gme','AMGN', 'DNA']dict_list=[]
for symbol in company_symbols:
dict_companies=client.get_ticker_metadata(symbol)
dict_list.append(dict_companies)
df__company_symbols=pd.DataFrame(dict_list)
df__company_symbols.head()

This returns:

(Image by author)

Note a couple things: ticker symbols passed into get_ticker_metadata do not have to be uppercase, and also it will return info on companies that have been delisted, including the ending date — in the example above, Genentech (ticker symbol DNA) was delisted on 4–5–2010, subsequent to its acquisition by Roche.

We can use Pandas DataReader to fetch historical stock price data for our list of stocks, and put it directly into a Pandas DataFrame. To do this, pass the list of ticker symbols, a date range, and your Tiingo API key into pdr.get_data(). Let’s look at the last few days of January 2021, which were, shall we say, interesting for Gamestop:

#Pass in the list of ticker symbols, and pandas datareader will download 
#the stock data for the specified time period.
#Need to add error handling for KeyErrors, because some dates are out of range.
test_hist_data_list=[]for symbol in company_symbols:
try:
df_test_prices = pdr.get_data_tiingo(symbol, start='2021-01-25', end='2021-01-29',
pause=0.2, api_key='Your API Key here')
test_hist_data_list.append(df_test_prices)

except KeyError as ke:
print('KeyError ', ke)

I added error handling, because this date range is invalid for DNA, as it was acquired long before, and so passing DNA for this date range returns a KeyError.

Let’s take a look at the data we acquired, to see what happened with GameStop:

print(test_hist_data_list[2])
(Image by author)

In the red rectangle above, you can see the wild ride GameStop stock had at the end of January 2021, as a result of Redditors in the r/WallStreetBets subreddit (motto: ‘like 4chan found a Bloomberg terminal’) buying.

Tiingo can get news articles too! Let’s look up Washington Post news articles about GameStop:

##Use the Tiingo client to get the news. You can pass in tickers, tags, sources, and start
##and end dates.
gme_news = client.get_news(tickers=['gme'],
sources=['washingtonpost.com'],
startDate='2021-01-15',
endDate='2021-01-29')
df_gme_news=pd.DataFrame(gme_news)
df_gme_news.head()
(Image by author)

There were 5 articles about GameStop in the Washington Post between January 15–29, 2021. Tiingo Client returns titles, tags, the URL of the article, and a description.

I hope you will find some of this useful! Thank you for reading!

Metis Data Science Bootcamp student, soon to be alumna. Immunology, cell biology; oncology drug development. Hope to apply data science in the pharma industry.