Skip to content

siavashadpey/rebalance

Repository files navigation

Rebalance

Build Status Coverage Code Factor Documentation Status

A calculator which tells you how to split your investment amongst your portfolio's assets based on your target asset allocation.

To use it, install the package and write a driver file as described below.

Installation

Clone the repository:

git clone https://github.com/siavashadpey/rebalance.git

Install the package:

cd rebalance
pip3 install .

Example

Make a driver file:

The driver file is where we create our portfolio. We specify all of its assets and the available cash we have to invest.

Follow the steps below for a detailed description. Alternatively, you can simply modify the example driver file.

cd rebalance
touch driver_file.py

Import all necessary packages:

from rebalance import Portfolio

First we create our portfolio:

# My portfolio
p = Portfolio()

Then we add our assets:

We must specify the ticker symbol and the quantity of each asset we currently have in our portfolio.

The portfolio used in this example is one of Canadian Portfolio Manager's model portfolios. This blog along with Canadian Couch Potato advocate low-cost, globally diversified index funds for DIY investors.
# Assets in portfolio
# The price will be retrieved automatically
tickers = ["XBB.TO",   # iShares Core Canadian Universe Bond Index ETF
           "XIC.TO",   # iShares Core S&P/TSX Capped Composite Index ETF
           "ITOT",     # iShares Core S&P Total U.S. Stock Market ETF
           "IEFA",     # iShares Core MSCI EAFE ETF
           "IEMG"]     # iShares Core MSCI Emerging Markets ETF
quantities = [36, 64, 32, 8, 7]
p.easy_add_assets(tickers=tickers, quantities=quantities)

We also need to add cash to our portfolio:

This is the amount that we are investing. We can add cash in different currencies.

# Cash in portfolio
cash_amounts = [3000., 200.]
cash_currency = ["USD", "CAD"]
p.easy_add_cash(amounts=cash_amounts, currencies=cash_currency)

Finally, we need to specify our target asset allocation:

The target asset allocation used in this example is that of an aggressive portfolio with 80% equities and 20% bonds (XBB.TO).
# Target asset allocation (in %)
target_asset_alloc = {
"XBB.TO": 20,
"XIC.TO": 20,
"ITOT":   36,
"IEFA":   20,
"IEMG":    4
}

Let the optimizer rebalance our portfolio!

# rebalance
p.selling_allowed = False # We don't want to sell any of our assets for this case
p.rebalance(target_asset_alloc, verbose=True)

You should see something similar to this (the actual values might differ due to changes in prices and exchange rates).

 Ticker      Ask     Quantity      Amount    Currency     Old allocation   New allocation     Target allocation
                      to buy         ($)                      (%)              (%)                 (%)
---------------------------------------------------------------------------------------------------------------
  XBB.TO    33.43       30         1002.90      CAD          17.52            19.99               20.00
  XIC.TO    24.27       27          655.29      CAD          22.61            20.01               20.00
    ITOT    69.38       10          693.80      USD          43.93            35.88               36.00
    IEFA    57.65       20         1153.00      USD           9.13            19.88               20.00
    IEMG    49.14        0            0.00      USD           6.81             4.24                4.00

Largest discrepancy between the new and the target asset allocation is 0.24 %.

Before making the above purchases, the following currency conversion is required:
    1072.88 USD to 1458.19 CAD at a rate of 1.3591.

Remaining cash:
    80.32 USD.
    0.00 CAD.