Automating everyday tasks with open source code.
In aprevious post, I gave an introduction to theyahoo_fin package. The most updated version of the package includes new functionality allowing you to scrape live stock prices fromYahoo Finance(real-time). In this article, well 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 thestock_infomodule fromyahoo_fin.
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 doesnt matter).
The live stock price has also been added to theget_quote_tablefunction, which pulls in additional information about the current trading days volume, bid / ask, 52-week range etc. effectively all the attributes available on Yahoosquote 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 thisyahoo_finupdate 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 theget_top_cryptosfunction:
The latest additions toyahoo_finthat well 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:
Click to share on Twitter (Opens in new window)
Click to share on Facebook (Opens in new window)
Click to share on LinkedIn (Opens in new window)
Click to share on Tumblr (Opens in new window)
Click to share on Reddit (Opens in new window)
Click to share on Skype (Opens in new window)
Enter your email address to subscribe to this blog and receive notifications of new posts by email.
How to hide a password in R with the keyring package
Web Browsing and Parsing with RoboBrowser and requests_html
Does Sell in May, Go Away really work?
3 ways to scrape tables from PDFs with Python