In [1]:
import numpy as np
import matplotlib
matplotlib.use('TKAgg')
%matplotlib inline
import pylab as py
py.ion()
import pandas as pd
import sys,os
import datetime

file_path = os.path.dirname(os.path.realpath('__file__'))

sys_path='%s/..'%file_path
sys.path.append(sys_path)
from investmenttools import PortfolioBuilder as PB
reload(PB)
Out[1]:
<module 'investmenttools.PortfolioBuilder' from '/home/sachin/Work/Python/Git_Folder/investmenttools/Python_Notebooks/../investmenttools/PortfolioBuilder.pyc'>
In [2]:
## Get Data for S&P Index Portfolio
sp=PB.get_historical_closes(['SPY','IVV','VOO'],datetime.datetime(2001,1,1),datetime.datetime(2017,8,17))
sp.dropna(inplace=True)
sp.head(5)
Out[2]:
Ticker IVV SPY VOO
Date
2010-09-09 111.30 110.92 101.32
2010-09-10 111.93 111.48 101.78
2010-09-13 113.16 112.72 103.06
2010-09-14 113.07 112.65 103.04
2010-09-15 113.49 113.08 103.30
In [3]:
## Get cumulative returns and plot
dp=sp.pct_change()
cp=(1+dp).cumprod()
cp.plot()
Out[3]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff6d0a40ed0>
In [4]:
## Get Data for Govt index funds
bp=PB.get_historical_closes(['SHY','VGSH','SCHO','TUZ'],datetime.datetime(2001,1,1),datetime.datetime(2017,8,17))
bp.dropna(inplace=True)
bp.head(5)
Out[4]:
Ticker SCHO SHY TUZ VGSH
Date
2010-08-05 50.01 84.21 50.89 60.73
2010-08-06 50.04 84.23 50.90 60.73
2010-08-09 50.01 84.18 50.88 60.71
2010-08-10 50.04 84.22 50.88 60.69
2010-08-11 50.04 84.24 50.89 60.76
In [5]:
bdp=bp.pct_change()
bcp=(1+bdp).cumprod()
bcp.plot()
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff692802dd0>
In [8]:
### Rebalance Buffet-Portfolio and plot cumulative returns since 2004
portfolio=PB.Backtest_Annual_Rebalance(['SPY','SHY'],[.9,.1],datetime.datetime(2004,1,1),datetime.datetime(2017,8,17)\
                                       ,initial=10000)
portfolio.dropna(inplace=True)
portfolio.head(5)
['SPY', 'SHY'] 2004-01-01 00:00:00 2017-08-17 00:00:00
******* 2005 ******
******* 2006 ******
******* 2007 ******
******* 2008 ******
******* 2009 ******
******* 2010 ******
******* 2011 ******
******* 2012 ******
******* 2013 ******
******* 2014 ******
******* 2015 ******
******* 2016 ******
******* 2017 ******
Out[8]:
Ticker SHY SPY Total N_SPY N_SHY Cash
Date
2004-01-02 82.27 111.23 9885.64 80.0 12.0 114.36
2004-01-05 82.28 112.44 9982.56 80.0 12.0 114.36
2004-01-06 82.38 112.55 9992.56 80.0 12.0 114.36
2004-01-07 82.44 112.93 10023.68 80.0 12.0 114.36
2004-01-08 82.45 113.38 10059.80 80.0 12.0 114.36
In [7]:
## Plot the cumulative returns as well as the number of equity units for eash of the two funds in the portfolio
py.figure(figsize=(15,4))
py.subplot(131);portfolio[['Total','Cash']].sum(axis=1).plot();
py.xticks(fontsize=15,fontweight='bold');
py.yticks(fontsize=15,fontweight='bold');
py.legend(['Buffet Portfolio'])

py.subplot(132);portfolio['N_SPY'].plot();
py.xticks(fontsize=15,fontweight='bold');
py.yticks(fontsize=15,fontweight='bold');
py.legend(['SPY stocks'])

py.subplot(133);portfolio['N_SHY'].plot();
py.xticks(fontsize=15,fontweight='bold');
py.yticks(fontsize=15,fontweight='bold');
py.legend(['SHY stocks'])
Out[7]:
<matplotlib.legend.Legend at 0x7ff68cc5eed0>