Advertisement
Guest User

Untitled

a guest
Mar 27th, 2021
238
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. def returns(prices):
  6.     """
  7.    Calulates the growth of 1 dollar invested in a stock with given prices
  8.    """
  9.     return (1 + prices.pct_change(1)).cumprod()
  10.  
  11. def drawdown(prices):
  12.     """
  13.    Calulates the drawdown of a stock with given prices
  14.    """
  15.     rets = returns(prices)
  16.     return (rets.div(rets.cummax()) - 1) * 100
  17.  
  18. def cagr(prices):
  19.     """
  20.    Calculates the Compound Annual Growth Rate (CAGR) of a stock with given prices
  21.    """
  22.     delta = (prices.index[-1] - prices.index[0]).days / 365.25
  23.     return ((prices[-1] / prices[0]) ** (1 / delta) - 1) * 100
  24.  
  25.  
  26. def sim_leverage(proxy, leverage=1, expense_ratio = 0.0, initial_value=1.0):
  27.     pct_change = proxy.pct_change(1)
  28.     pct_change = (pct_change - expense_ratio / 252) * leverage
  29.     sim = (1 + pct_change).cumprod() * initial_value
  30.     sim[0] = initial_value
  31.     return sim
  32.  
  33. start = datetime.datetime(1977, 1, 1)
  34. end = datetime.datetime(2020, 1, 1)
  35.  
  36. vfinx = web.DataReader("VFINX", "yahoo", start, end)["Adj Close"]
  37. #upro = web.DataReader("UPRO", "yahoo", start, end)["Adj Close"]
  38. upro_sim = sim_leverage(vfinx, leverage=3.0, expense_ratio=0.02).rename("UPRO Sim")
  39.  
  40. print("CAGRs")
  41. print(f"SPY: {cagr(vfinx):.2f}%")
  42. #print(f"UPRO: {cagr(upro):.2f}%")
  43. print(f"UPRO_SIMULATED: {cagr(upro_sim):.2f}%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement