rebalance
rebalance is a simple Python 3.9+ library for rebalancing investment portfolios. It supports cash flow rebalancing with contributions and withdrawals, opportunistic rebalancing with symmetric and asymmetric rebalance bands, and traditional calendar-based rebalancing. Works with any currency. Precise to the smallest unit.
>>> from decimal import Decimal
>>> from rebalance import Asset, Portfolio, Result, cash_flow_rebalance, opportunistic_rebalance
>>> a = Asset("A", Decimal("0.6"), 25_00)
>>> b = Asset("B", Decimal("0.4"), 25_00)
>>> portfolio = Portfolio((a, b))
>>> for result in cash_flow_rebalance(portfolio, 50_00):
... print(result.asset.name, result.delta)
...
A 3500
B 1500
>>> for result in cash_flow_rebalance(portfolio, -40_00):
... print(result.asset.name, result.delta)
...
B -2100
A -1900
>>> for result in opportunistic_rebalance(portfolio):
... print(result.asset.name, result.delta)
...
A 300
B -300
Use
Install rebalance:
pip install rebalance
Then read the documentation.
Test
Install Hypothesis:
pip install hypothesis
Then run this command in the project root directory:
python -m unittest
Build
Documentation
Install Sphinx:
pip install sphinx
Then run this command in the docs/
directory:
make html
Distribution package
Install build:
pip install build
Then run this command in the project root directory:
python -m build