Optimal lazy portfolio rebalancing calculator (in Rust)
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
example flesh out Jan 7, 2019
.gitignore rm stray file Jun 1, 2017
.travis.yml add travis Jan 7, 2019
LICENSE.md add license and readme Jun 1, 2017
screenshot.png screenshot Jan 7, 2019



Build Status

Optimal lazy portfolio rebalancing calculator (in Rust)


$ rebalance-app --help
rebalance-app 1.1.0
Alberto Leal (github.com/dashed) <mailforalberto@gmail.com>
Optimal lazy portfolio rebalancing calculator

    rebalance-app [OPTIONS] <contribution> --portfolio <FILE> --targets <FILE>

    -h, --help       Prints help information
    -V, --version    Prints version information

    -m, --min-contribution <MIN_CONTRIBUTION>    Sets the minimum contribution for assets
    -p, --portfolio <FILE>                       Sets a portfolio file
    -i, --portfolio_value_index <INDEX>          Sets CSV index of the portfolio value
    -t, --targets <FILE>                         Sets a targets file

    <contribution>    Sets the contribution amount


  1. Set up targets.csv. Example: example/targets.csv

  2. Set up portfolio.csv. Usually you export this from your favourite broker. Example: example/portfolio.csv

  3. Run: rebalance-app --portfolio example/portfolio.csv --targets example/targets.csv 5000

By default, rebalance-app assumes the values of your assets in your portfolio CSV file is at the 2nd column (i.e. index 1). For example:

Bond fund,                 roi %, ticker symbol, $16500.00
TIPS fund,                 roi %, ticker symbol, $6500.00
Domestic Stock ETF,        roi %, ticker symbol, $43500.00
International Stock ETF,   roi %, ticker symbol, $33500.00

You can adjust this using the -i flag:

rebalance-app -i 3 --portfolio example/portfolio.csv --targets example/targets.csv 5000

Minimum Contributions

Certain brokers may have certain securities that have minimum investment contributions.

The flag -m/--min-contribution comes in handy for this usecase.

For example, the following command:

rebalance-app --portfolio example/portfolio.csv --targets example/targets.csv -m 300 5000

says that "I want to contribute $5000 with the requirement that my contributions to each asset is at least $300".


Rationale: Rather than rebalance your portfolio internally, add/remove money such that your asset targets % are achieved as close as possible.

See this article on rebalancing: https://www.bogleheads.org/wiki/Rebalancing

Based on:

How it works:

Source: http://optimalrebalancing.tk/explanation.html

Step 1: Calculate the fractional deviations

Define the fractional deviation f of an asset to be a/t − 1, where t is the asset's target allocation and a is its actual portion of the portfolio. Calculate f for each asset. f will be negative for underweighted assets and positive for overweighted assets. Note that a denotes the portion relative to the final total portfolio value; this is obtained by adding the contribution amount to the original total portfolio value.

Step 2: Add money to asset(s) with lowest fractional deviation

Add money to the asset(s) with least f until they are tied with the asset(s) with the next-least f. The money added to each asset must be proportional to that asset's target allocation so that the minimum f's increase in synchrony. Repeat this until the contribution amount is exhausted. If the assets are pre-sorted according to f, this process can be implemented such that its running time increases linearly with the number of assets.


  • cargo fmt