Financial Data for Python - Trying YahooFinancial and YFinance packages
Financial Data with Python
How to pull data from: https://finance.yahoo.com/
Yahoo Financials
MIT | A powerful financial data module used for pulling data from Yahoo Finance. This module can pull fundamental and technical data for stocks, indexes, currencies, cryptos, ETFs, Mutual Funds, U.S. Treasuries, and commodity futures.
pip install yahoofinancials==1.20
You can get started with a .ipynb
jupyter for exploration.
YFinance
- https://pypi.org/project/yfinance/
- https://rowzero.io/blog/yfinance
- https://github.com/ranaroussi/yfinance
Apache v2 | Download market data from Yahoo! Finance’s API
pip install yfinance==0.2.52
import yfinance as yf
import pandas as pd
def STOCK(ticker):
return yf.Ticker(ticker).history(period="max")
STOCK('KO')
Dividend Data - DGI vs Yield
When you put together few stocks with growing dividends, you might expect something like this:
Some years might have a decrease due to:
- Global financial circunstances
- Or maybe just one of the stocks gave you a special dividend last year
What this tries to illustrate its just the general upwards trend.
But what’s better, high yield or high dividend growth?
Ideally something that give us both, but, there is always a trade off.
And some people call high yield investments as divs traps.
What does the data and math tell us about it?
No Dividend Reinvestment
In this case, you just buy one time in the beginning, no debt, sit and wait to see how each of those 2 evolve:
You can see how long it takes for 2.5% yield growing at 12% to catch up with 5.5% which grows at 2.57%
With Dividend Reinvestment
How about keep buying by re-investing the dividends?
Assuming that the price of the stock and the initial yield is the same, we would get:
Stock Value Data
So that’s how dividends can behave over time.
How about the stock value?
Conclusions
Is it possible to life from dividends?
How much do I need to live from dividends?
Those are typical questions i frequently see over the internet.
In theory, you just need to know 2 things:
- How much you spend (the net + taxes and so on)
- What it is the avg return on your assets
And the math goes… $Needs = P \times \frac{Yearly_Gross_Expenses}{(Yearly_returns)}$.
People mention about different strategies to estimate the returns:
- The 4% rule, which apparently is an estimate of what you can take from a portfolio without killing your principal every year
- Others just go with a dividend investing approach, so they dont need to sell shares
- And some people have a balance between stocks funds and bonds, so that if stock market goes down they can live with those bonds, without selling really cheap the stocks
FAQ
YFinance with Google Sheets
You can have a simple, yet useful Google Sheets with Stocks info:
=GoogleFinance(S35;"eps") #S35 can reference some ticket, like NYSE:KO
=GoogleFinance(S35;"pe")
=GoogleFinance("INDEXSP:.INX") #sp500
=INDICE(GoogleFinance("INDEXSP:.INX"; "price"; HOY()-365);2;2) #get the price 365 ago
=SPARKLINE(GoogleFinance("CURRENCY:EURCHF"; "price"; HOY()-J$1; HOY()))
=SPARKLINE(GoogleFinance("CURRENCY:"&"USD" & DERECHA(A6;3); "price"; HOY()-J$1; HOY()))
And if you need, you can also import info from other website sources:
- Import from ycharts:
=VALOR(IZQUIERDA(importxml(CONCATENAR("https://ycharts.com/companies/";REGEXEXTRACT(S33;"[^:]*$");"/profit_margin");$AJ$28);3))/100
#S33 is a ticker, NASDAQ:PEP, for example and AJ28 the XPATH
# =importxml("https://ycharts.com/companies/"& REGEXEXTRACT("NASDAQ:PEP";"\:(.*)") & "/profit_margin";$AI$28)
with xpath being
/html/body/div[3]/div[2]/section[1]/div/div/div[1]/div[2]/ul/li[1]/span[2]
- Import from numbeo:
=IZQUIERDA(importxml(C36;C37);6)/IZQUIERDA(importxml(C36;C38);6)
With:
- C36
https://www.numbeo.com/cost-of-living/compare_cities.jsp?country1=Poland&country2=spain&city1=Warsaw&city2=barcelona&tracking=getDispatchComparison
- C37
/html/body/div[2]/aside[1]/div[2]/div/span[1]/text()
- C38
/html/body/div[2]/aside[1]/div[2]/div/span[3]
- Even from Etherscan for crypto related info!
=importxml("https://etherscan.io/address/some_address";"/html/body/main/section[3]/div[2]/div[1]/div/div/div[2]/div/div")