This Quantopian Research based notebook compares SPY to the following portfolios:
In [1]:
import pandas as pd
import matplotlib.pyplot as pyplot
import seaborn as sns
In [8]:
def stats(backtest):
return backtest.risk.tail(1)
In [6]:
swensen = get_backtest('54fa0196e9787f0f115152aa')
pop = get_backtest('54fa094ca1094d0f12b31520')
spy = get_backtest('54fa0435f3bb8b0f0e66bb4f')
In [10]:
pop = get_backtest('54fa094ca1094d0f12b31520')
In [11]:
#swensen.risk.treasury_period_return.plot(label="Tresuries")
swensen.risk.benchmark_period_return.plot(label='SPY')
swensen.risk.period_return.plot(label="Swensen")
pop.risk.period_return.plot(label="POP")
pyplot.title("Portfolio Returns")
pyplot.legend(loc='best')
pyplot.ylabel("Portoflio % return")
pyplot.xlabel("Time")
Out[11]:
In [12]:
df_stats = stats(spy)
df_stats['Portfolio'] = 'SPY'
sw_stats = stats(swensen)
sw_stats['Portfolio'] = 'Swensen'
df_stats = df_stats.append(sw_stats)
pop_stats = stats(pop)
pop_stats['Portfolio'] = 'POP'
df_stats = df_stats.append(pop_stats)
df_stats = df_stats.set_index('Portfolio')
df_stats.transpose()
Out[12]:
In [11]:
# this no longer works: swensen.benchmark_security.symbol
In [350]:
((result.risk.benchmark_period_return + 1) * result.capital_base).plot(label=result.benchmark_security.symbol)
((result.risk.period_return + 1) * result.capital_base).plot(label="Portfolio")
pyplot.title("Portfolio Returns")
pyplot.legend(loc='best')
pyplot.ylabel("Portoflio % return")
pyplot.xlabel("Time")
Out[350]:
In [328]:
benchmark_daily_close = get_pricing(swensen.benchmark_security.symbol, fields='close_price',
start_date= swensen.start_date,
end_date = swensen.end_date,
frequency='daily')
base_mult = swensen.capital_base / benchmark_daily_close[0]
benchmark_ending_portfolio_value = benchmark_daily_close * base_mult
benchmark_ending_portfolio_value[:5]
Out[328]:
In [329]:
base_mult * 138
Out[329]:
In [330]:
benchmark_daily_close[:5]
Out[330]:
In [ ]:
100
In [37]:
spy.cumulative_performance.ending_portfolio_value.plot(label="SPY",colormap=None)
swensen.cumulative_performance.ending_portfolio_value.plot(label="Swensen")
#benchmark_ending_portfolio_value.plot(label=swensen.benchmark_security.symbol)
pyplot.title("Portfolio Returns")
pyplot.legend(loc='best')
pyplot.ylabel("Portoflio Value in $")
pyplot.xlabel("Time")
Out[37]:
In [175]:
swensen.cumulative_performance.ending_portfolio_value[:5]
Out[175]:
In [176]:
spy.cumulative_performance.ending_portfolio_value[:5]
Out[176]:
In [197]:
# days with rebalancing transactions
transactions = [str(t.date()) for t in list(swensen.transactions.index.unique())]
transactions
Out[197]:
In [178]:
# how many transactions ?
len(transactions)
Out[178]:
In [ ]:
stats(spy)
In [ ]:
In [297]:
#csv_VTHRX = fetch_url('http://ichart.yahoo.com/table.csv?s=VTHRX&a=0&b=0&c=2007&d=1&e=6&f=2015&g=d&ignore=.csv')
df_VTHRX = local_csv('VTHRX.csv')
df_VTHRX[:3]
Out[297]:
In [298]:
df_VTHRX.set_index('Date',inplace=True)
df_VTHRX.index = pd.to_datetime(df_VTHRX.index) # pd.DatetimeIndex(df_VTHRX.Date) # df_VTHRX.set_index('Date')
df_VTHRX.sort(inplace=True)
df_VTHRX = df_VTHRX[pd.datetime(2007, 3, 2):]
VTHRX_base_mult = swensen.capital_base / df_VTHRX['Adj Close'][0]
df_VTHRX['ending_portfolio_value'] = df_VTHRX['Adj Close'] * VTHRX_base_mult
df_VTHRX[:5]
Out[298]:
In [306]:
df_VTHRX.ending_portfolio_value.plot(label="VTHRX")
swensen.cumulative_performance.ending_portfolio_value.plot(label="Swensen")
#spy.cumulative_performance.ending_portfolio_value.plot(label="SPY", style=[':'])
pyplot.title("Portfolio Returns")
pyplot.legend(loc='best')
pyplot.ylabel("Portoflio Value in $")
pyplot.xlabel("Time")
Out[306]:
In [72]:
M5050 = get_backtest('54ea9bd57c6f390f1639a55f')
M6040 = get_backtest('54ea9beba77a8b0f24e91f8c')
M7030 = get_backtest('54ea9bff52d9f50f07c03985')
In [48]:
M5050.risk.period_return.plot(label="50/50")
M6040.risk.period_return.plot(label="60/40")
M7030.risk.period_return.plot(label="70/30")
M5050.risk.benchmark_period_return.plot(label='SPY')
pyplot.title("VTI / AGG Portfolio Returns")
pyplot.legend(loc='best')
pyplot.ylabel("Portoflio % return")
pyplot.xlabel("Time")
Out[48]:
In [74]:
df_stats = stats(spy)
df_stats['Portfolio'] = 'SPY'
M5050_stats = stats(M5050)
M5050_stats['Portfolio'] = '50/50'
df_stats = df_stats.append(M5050_stats)
M6040_stats = stats(M6040)
M6040_stats['Portfolio'] = '60/40'
df_stats = df_stats.append(M6040_stats)
M7030_stats = stats(M7030)
M7030_stats['Portfolio'] = '70/30'
df_stats = df_stats.append(M7030_stats)
df_stats = df_stats.set_index('Portfolio')
df_stats.transpose()
Out[74]:
In [ ]: