Integrate with Polygon API

Sean
SLTC — Sean Learns To Code
3 min readFeb 9, 2023

Recently an issue was reported on the Github page of yfinance, the Python library that I rely on for the stonk-bot. The tl;dr is that yfinanceis not an official API provided by Yahoo! Finance (YF). The official API was shut down by Yahoo! years ago and yfinance is an completely community driven alternative that relies on the data from YF. According to the maintainers of yfinance, YF sometimes changes the way the data is encrypted, which breaks yfinance until the maintainers figure out how to adjust the data decryption code.

Not wanting to disappoint my users, I started to look for an API to serve as the backup plan for yfinance. I found Polygon.

The stonk bot after being integrated with Polygon API

Integrate with Polygon API

I spent a few hours last night integrating the stonk bot with Polygon. The whole process could be summed up as follows

  • Register for an account on Polygon website and get the API token
  • Install the Python client
  • Create a Polygon client object in the code and fetch the data

This commit includes all the changes that I made. A few differences that one needs to pay attention to when consider using Polygon API in place of yfinance

  1. yfinance supports tickers of many (mostly US-based) mutual funds (e.g. VTSAX, FXAIX, etc.) while Polygon API doesn’t support mutual funds.
  2. Both support tickers for cryptos but with slightly different syntax. For example, yfinance uses BTC-USD for Bitcoin while Polygon uses X:BTDUSD.
  3. Polygon has some support for fetching crypto price in currencies other than US dollar (e.g. EUR) while VTSAX only supports USD.

Combine Polygon and yfinance

After successfully integrating my bot with Polygon, I realize that there are some issues

  1. I only have a free subscription with Polygon API so potentially I could be rate limited at some point.
  2. My users are already familiar with the ticker format supported yfinance. As yfinance and Polygon have different format for crypto tickers I need to make things backward compatible for the users
  3. How about mutual funds?

The solution for issue #1 above is to combine both APIs. First, the bot will attempt to fetch the data with yfinance and it will fall back to Polygon if yfinance doesn’t work. When yfinance fixes the issue, the number of requests the bot sends to Polygon will be significantly reduced.

For issue #2, I added a hack to check if a crypto ticker in the format supported by yfinance is used in the command and convert them to the format supported by Polygon.

For issue #3 unfortunately I don’t have a solution. I was not able to find any free API that provides pricing data of mutual funds so let’s hope that yfinance will resolve their issue soon.

The surprise after the bell

I wrote all the code and tested them last night. The market was closed at the time so I didn’t realize one limitation of Polygon API’s free tier: it only has end of data data!

A quick look at Polygon’s pricing info tells me that if I want to get real-time data I need to pay $200/month, which is a bit out of the budget for a side project.

Now it looks like my only option is to wait until the issue of yfinance is resolved!

--

--