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.
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).
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")
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()
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. To learn more about yahoo_fin, please view my tutorials on YouTube.
Also, if you’re interested in using machine learning for financial data, check out Advances in Financial Machine Learning
Very excited to announce the early-access preview (MEAP) of my upcoming book, Software Engineering for…
Ever had long-running code that you don't know when it's going to finish running? If…
Background If you've done any type of data analysis in Python, chances are you've probably…
In this post, we will investigate the pandas_profiling and sweetviz packages, which can be used…
In this post, we're going to cover how to plot XGBoost trees in R. XGBoost…
In this post, we'll discuss the underrated Python collections package, which is part of the…