Code Monkey home page Code Monkey logo

rebalance's Introduction

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.

You can either install the package and write a driver file as described below or use the web-based calculator hosted on AWS here.

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.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.