Python code to aid in Exchange-Traded Funds (ETFs) portfolio rebalancing.
To install: with python 3, git clone
, cd
into it and run:
$ pip install -r requirements.txt
...
$ python setup.py install
Use it like this:
from decimal import *
from rebalance import Instrument, CASH, Portfolio, Plotter
# instruments (ETFs)
ZAG = Instrument('ZAG', 'BMO Aggregate Bond Index EFT')
VCN = Instrument('VCN', 'Vanguard FTSE Canada All Cap Index ETF')
XAW = Instrument('XAW', 'iShares Core MSCI All Country World ex Canada Index ETF')
my_portfolio = Portfolio({
CASH: Decimal(500),
ZAG: Decimal(1000),
VCN: Decimal(3000),
XAW: Decimal(5500)})
assert my_portfolio.total == Decimal(10000)
# canadian couch potato assertive model portfolio (2016)
model_portfolio = Portfolio({
ZAG: Decimal(25),
VCN: Decimal(25),
XAW: Decimal(50)})
assert model_portfolio.total == Decimal(100)
orders = my_portfolio.rebalance(model_portfolio)
print(orders)
# [Order(action=Sell, instrument=VCN, amount=Decimal('500.00')),
# Order(action=Sell, instrument=XAW, amount=Decimal('500.00')),
# Order(action=Buy, instrument=ZAG, amount=Decimal('1500.00'))]
The results are the necessary orders to rebalance my_portfolio
with the model_portfolio
as a model, where an order includes the desired action (Buy or Sell), the instrument (ETF) symbol and the dollar amount (with default precision and rounding rules, discrepancies may occur).
There's also a method to plot your portfolio:
my_portfolio.plot()
Plotter.show()
You can also plot the returns of your portfolio. This will use google finance and plot the daily value of the portfolio for a year until today, assuming the portfolio debuted one year ago with its recorded composition:
my_portfolio.plot_returns()
Plotter.show()
Note that this is for illustrative purposes only and does not account for fees or dividends. Also remember, past performance does not guarantee future results.