Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import pandas_datareader.data as web
- import datetime
- def returns(prices):
- #Calulates the growth of 1 dollar invested in a stock with given prices
- return (1 + prices.pct_change(1)).cumprod()
- def drawdown(prices):
- #Calulates the drawdown of a stock with given prices
- rets = returns(prices)
- return (rets.div(rets.cummax()) - 1) * 100
- def cagr(prices):
- #Calculates the Compound Annual Growth Rate (CAGR) of a stock with given prices
- delta = (prices.index[-1] - prices.index[0]).days / 365.25
- return ((prices[-1] / prices[0]) ** (1 / delta) - 1) * 100
- def sim_leverage(proxy, leverage=1, expense_ratio = 0.0, initial_value=1.0):
- pct_change = proxy.pct_change(1)
- pct_change = pct_change * leverage - expense_ratio / 252
- sim = (1 + pct_change).cumprod() * initial_value
- sim[0] = initial_value
- return sim
- start = datetime.datetime(1980, 1, 1)
- end = datetime.datetime(2021, 1, 1)
- vfinx = web.DataReader("VFINX", "yahoo", start, end)["Adj Close"]
- #upro = web.DataReader("UPRO", "yahoo", start, end)["Adj Close"]
- letf_sim = sim_leverage(vfinx, leverage=3, expense_ratio=0.1).rename("LETF Sim")
- print("CAGRs")
- print(f"SPY: {cagr(vfinx):.2f}%")
- #print(f"UPRO: {cagr(upro):.2f}%")
- print(f"LETF_SIMULATED: {cagr(letf_sim):.2f}%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement