bt = get_backtest('598bb4d9b11f81540685ce78')
bt.create_full_tear_sheet()
Use simple algorithm, next cell, to find the performance of a simple asset allocation (could be just one asset) to use as benchmark. Then we can load in the performance that includes dividends.
def initialize(context):
"""
Called once at the start of the algorithm.
"""
set_commission(commission.PerShare(cost=0, min_trade_cost=0))
# Rebalance every day, 1 hour after market open.
schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=1))
def my_rebalance(context,data):
"""
Execute orders according to our schedule_function() timing.
"""
order_target_percent(sid(22972),1.0)
dynamic_algorithm = bt.cumulative_performance.returns
ex_us = get_backtest('598bc5ed59ab1554042ee706').cumulative_performance.returns
ex_us_bond = get_backtest('598bc5d88e33dd5412351555').cumulative_performance.returns
us = get_backtest('598bc5e44a58a153d4ce0262').cumulative_performance.returns
us_bond = get_backtest('598bc5cc59ab1554042ee703').cumulative_performance.returns
Then make a copy and add the algorithm result as well.
import pandas as pd
data = pd.DataFrame(us)
data.columns = ['US Stock']
data['US Bond'] = us_bond.values
data['ex-US Bond'] = ex_us_bond.values
data['ex-US Stock'] = ex_us.values
data2 = data.copy()
data2['Dynamic'] = dynamic_algorithm
data2.plot()
Define function that computes all possible combinations of an array of arrays that add to 1
import numpy as np
import matplotlib.pyplot as plt
import itertools
def create_portfolios(a):
"""
Create portfolio combinations
"""
b = list(itertools.product(*a))
x = [sum(i) for i in b]
port = pd.DataFrame(b)
port['Sum'] = x
print len(port)
port = port[port.Sum == 1.0]
del port['Sum']
print len(port)
return port
Allow increments of 12.5% allocation to each asset class, maximum of 62.5% to an asset in line with what Vanguard does with their target date funds
a = [0,.125,.25,.375,.5,.625]
portfolios = create_portfolios([a,a,a,a])
port_returns = pd.DataFrame(np.dot(data, portfolios.T), index=data.index)
port_returns.plot(legend=False)
port_returns.plot(legend=False)
dynamic_algorithm.plot(lw=10)
data2.plot(legend=True,lw=5)
The following is some of the code in the intialize function of the algorithm. This explicitely lists the ETFs to consider an allocation to at the start of every month.
context.us_style = [sid(25871), #ITOT Total US
sid(21512), #IVE Large Value
sid(21513), #IVV Large Blend
sid(21514), #IVW Large Growth
sid(21770), #IJJ Mid Value
sid(21507), #IJH Mid Blend
sid(21771), #IJK Mid Growth
sid(21772), #IJS Small Value
sid(21508), #IJR Small Blend
sid(21773)] #IJT Small Growth
context.us_sector = [sid(19662), #XLY Consumer Cyclical
sid(19656), #XLF Financials
sid(19659), #XLP Consumer Defensive
sid(19661), #XLV Healthcare
sid(19657), #XLI Industrials
sid(19655), #XLE Energy
sid(19654), #XLB Materials
sid(19660), #XLU Utilities
sid(21652), #IYR ISHARES Real Estate
sid(19658)] #XLK Tech
context.ex_us_stock = [sid(22972), #EFA Core EAFE
sid(27536), #EFV EAFE Value
sid(35248), #SCZ ex-US Small-Cap
sid(33072), #RWX ex-US REIT
sid(24705)] #EEM Emerging Markets
context.country = [sid(14516), #EWA Australia
sid(14519), #EWH Hong Kong
sid(12430), #EWD Sweden
sid(21757), #EWZ Brazil
sid(24611)] #EZA South Africa
""" sid(14520), #EWJ Japan
sid(14529), #EWU UK
sid(14527), #EWQ France
sid(14518), #EWG Germany
sid(14522), #EWL Switzerland
sid(25098), #EWI Italy
sid(21491), #EWY South Korea
sid(21619), #EWT Taiwan
sid(14524), #EWN Netherlands
sid(14526), #EWP Spain
"""
context.bonds = [sid(35323), #EMB Emerging Market Bond
sid(34793), #BWX Int'l Treasury
sid(33655), #HYG High Yield Bond
sid(23881), #LQD Quality Corporate Bond
sid(34648), #MUB Municipal Bonds
sid(25485), #AGG Total Bond
sid(23921), #TLT Long Term Government Bond
sid(25801), #TIP TIPS
sid(23870), #IEF Intermediate Treasury
sid(23911)] #SHY Short Term Government Bond
# sid(33154)] #SHV <1 Treasury Bonds
context.other = [sid(26807), #GLD Gold
sid(33127)] #DBA Agricultural Commodities