DEV Community

Cover image for Live Yahoo! Finance stock prices in Python
mbnlc
mbnlc

Posted on

Live Yahoo! Finance stock prices in Python

Disclaimer: I am the author of the Python package presented in this article.

Python is and has been one of the best and most used programming languages for data science and is especially popular in the financial space, being used for quantitative analytics by even the biggest firms, combing through vast amounts of data, searching for new ways to make money.

However, this isn't and shouldn't be reserved for big companies. None the less, most data providers charge for their service, so you might ask where you could get your hands on some quality data for your own projects.

Enter, Yahoo! Finance...

Yahoo! Finance is one of the most popular sites for any kind of financial information and better yet, it's completely free.

There are multiple ways of obtaining data from Yahoo! Finance with Python. As with any webpage you can use web scraping tools like beautiful soup or selenium to retrieve data from the Yahoo! Finance webpage. Even though this approach works, use of the unofficial Yahoo! Finance API has proven to be more efficient. A lot of open source projects such as yfinance make this even easier by providing a pythonic and intuitive way for requesting data.

Both these methods work perfectly for one-time requests, are, however, not ideal for live updates as they would have to be constantly requesting data / reloading the page. Web sockets allow updates to be sent by the server itself, sparing you countless unnecessary requests, which might even get you banned.

Live Yahoo! Finance quotes

Live stock prices are not easy to come by for free, especially via web socket. This has lead me to develop a simple and free way to obtain real time quotes from Yahoo! Finance:

GitHub logo mbnlc / yflive

Live Data Streamer for Yahoo! Finance

yflive is a python package for connecting to the Yahoo! Finance web socket in a simple, pythonic way.

from yflive import QuoteStreamer

# Get QuoteStreamer singleton
qs = QuoteStreamer()

# Subscribe to AAPL, TSLA
qs.subscribe(["AAPL", "TSLA"])
Enter fullscreen mode Exit fullscreen mode

The QuoteStreamer is the main component of this package. The singleton object ensures that a connection is upheld and manages communications.

After retrieving the singleton, we subscribe to the tickers of interest (in this example AAPL and TSLA). Note, that we have not yet connected to Yahoo! Finance and thus won't receive any quotes for the time being.

# Override on_quote callback
qs.on_quote = lambda q: print(q)

# Start streaming
qs.start()
Enter fullscreen mode Exit fullscreen mode

After defining a simple callback function for printing the quotes we receive, we open a connection to Yahoo! Finance.

You should now start to receive quotes for the tickers you've previously subscribed to. If this isn't the case, you might be outside of trading hours...

Wrapping up...

yflive is a very young project and still needs a lot of work. If you're interested in contributing, feel free to open a pull request or issue on GitHub.

I hope you find this useful and thanks for reading.

Feedback is appreciated.

Top comments (0)