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.

Import NumPy and Pandas.

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.

Next, import the TiingoClient and initialize:

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

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):

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

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

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:

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:

(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:

(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.