How to get live stock prices with Python

[This article was first published on Open Source Automation, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


live stock prices with python yahoo finance

In a previous post, I gave an introduction to the yahoo_fin package. The most updated version of the package includes new functionality allowing you to scrape live stock prices from Yahoo Finance (real-time). In this article, we’ll go through a couple ways of getting real-time data from Yahoo Finance for stocks, as well as how to pull cryptocurrency price information.

The get_live_price function

First, we just need to load the stock_info module from yahoo_fin.


# import stock_info module from yahoo_fin
from yahoo_fin import stock_info as si

Then, obtaining the current price of a stock is as simple as one line of code:


# get live price of Apple
si.get_live_price("aapl")

# or Amazon
si.get_live_price("amzn")

# or any other ticker
si.get_live_price(ticker)

Note: Passing tickers is not case-sensitive (upper / lower / mixed doesn’t matter).

Getting live price with other quote data

The live stock price has also been added to the get_quote_table function, which pulls in additional information about the current trading day’s volume, bid / ask, 52-week range etc. — effectively all the attributes available on Yahoo’s quote page.

Just replace “aapl” with any other ticker you need.


# get quote table back as a data frame
si.get_quote_table("aapl", dict_result = False)

# or get it back as a dictionary (default)
si.get_quote_table("aapl")


How to get cryptocurrency prices

Also new in this yahoo_fin update is a function for getting the top cryptocurrency prices. These are updated frequently by Yahoo Finance (see this link).

Get back the prices on the top 100 (by market cap) cryptocurrencies by calling the get_top_cryptos function:


si.get_top_crypto()

How to get most active, biggest gainers / losers

The latest additions to yahoo_fin that we’ll show here are about getting data on the most actively traded stocks, as well what stocks have gained or lost the most during the trading session. Each of the functions below returns a pandas data frame with the (at most) top 100 stocks falling in each category (active / gainer / loser).


# get most active stocks on the day
si.get_day_most_active()

# get biggest gainers
si.get_day_gainers()

# get worst performers
si.get_day_losers()

The data above is being pulled from these links:

Most Active: https://finance.yahoo.com/most-active

Top Gainers: https://finance.yahoo.com/gainers

Worst Performers: https://finance.yahoo.com/losers

Click here to check out other articles on scraping stock and financials information. Also, if you’re interested in using machine learning for financial data, check out Advances in Financial Machine Learningpython live stock prices.

The post How to get live stock prices with Python appeared first on Open Source Automation.

To leave a comment for the author, please follow the link and comment on their blog: Open Source Automation.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)