Advertisement
Guest User

Untitled

a guest
Apr 7th, 2021
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. import pandas as pd
  2. import pandas_datareader.data as web
  3. import datetime
  4.  
  5.  
  6. def returns(prices):
  7.    
  8.     #Calulates the growth of 1 dollar invested in a stock with given prices
  9.    
  10.     return (1 + prices.pct_change(1)).cumprod()
  11.  
  12. def drawdown(prices):
  13.    
  14.     #Calulates the drawdown of a stock with given prices
  15.    
  16.     rets = returns(prices)
  17.     return (rets.div(rets.cummax()) - 1) * 100
  18.  
  19. def cagr(prices):
  20.    
  21.     #Calculates the Compound Annual Growth Rate (CAGR) of a stock with given prices
  22.    
  23.     delta = (prices.index[-1] - prices.index[0]).days / 365.25
  24.     return ((prices[-1] / prices[0]) ** (1 / delta) - 1) * 100
  25.  
  26.  
  27. def sim_leverage(proxy, leverage=1, expense_ratio = 0.0, initial_value=1.0):
  28.     pct_change = proxy.pct_change(1)
  29.     pct_change = pct_change * leverage - expense_ratio / 252
  30.     sim = (1 + pct_change).cumprod() * initial_value
  31.     sim[0] = initial_value
  32.     return sim
  33.  
  34. start = datetime.datetime(1980, 1, 1)
  35. end = datetime.datetime(2021, 1, 1)
  36.  
  37. vfinx = web.DataReader("VFINX", "yahoo", start, end)["Adj Close"]
  38. #upro = web.DataReader("UPRO", "yahoo", start, end)["Adj Close"]
  39. letf_sim = sim_leverage(vfinx, leverage=3, expense_ratio=0.1).rename("LETF Sim")
  40.  
  41. print("CAGRs")
  42. print(f"SPY: {cagr(vfinx):.2f}%")
  43. #print(f"UPRO: {cagr(upro):.2f}%")
  44. print(f"LETF_SIMULATED: {cagr(letf_sim):.2f}%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement