Rebalancing
How QuantJourney converts target weights into realized drifted weights and rebalance trades.
Rebalancing is a weight-mode portfolio process. It decides when target weights are allowed to become actual weights.
Source
backtester/portfolio/rebalance.py + backtester/core.pyLayerPortfolio execution timing for weights
ModeWeight mode
Inputshifted target weights, asset returns, optional benchmark returns
Outputactual weights, rebalance flags and rebalance statistics
Primary API
rebalance_policy=RebalancePolicy(...)Main caveatRebalancing is not the same as market/limit/stop order simulation.
Engine Contract
Between rebalance dates, weights drift with price movement. On rebalance dates, selected positions snap back to target weights, subject to policy gates.
target weights -> shift(1) -> cash buffer
-> RebalanceEngine.run()
-> actual weights + rebalance_flags
-> turnover cost + positions + NAV
Level 1: Monthly Rebalance
Business month-end rebalance
python
from backtester.portfolio.rebalance import RebalancePolicy
strategy = MyStrategy(
...,
execution_mode="weights",
rebalance_policy=RebalancePolicy(frequency="BME"),
)Level 2: Monthly Plus Drift Band
Rebalance monthly or when drift exceeds 5 percent
python
strategy = MyStrategy(
...,
rebalance_policy=RebalancePolicy(
frequency="BME",
drift_threshold=0.05,
),
)Level 3: Institutional-Style Policy
Partial rebalance with turnover budget
python
strategy = MyStrategy(
...,
rebalance_policy=RebalancePolicy(
frequency="BME",
drift_threshold=0.03,
partial_rebalance=True,
max_annual_turnover=6.0,
avoid_short_term_gains=True,
),
)Supported Triggers
| Trigger | Field |
|---|---|
| Calendar | frequency, weekday |
| Drift | drift_threshold, drift_type |
| Tracking error | tracking_error_threshold |
| Signal change | rebalance_on_signal_change |
| Drawdown breaker | max_drawdown_trigger |
| Turnover budget | max_annual_turnover |
| Partial rebalance | partial_rebalance |
| Tax heuristic | avoid_short_term_gains |
Implementation Note
The enum RebalanceAt.OPEN/CLOSE/VWAP_WINDOW records timing intent. The current weight-mode performance path should still be treated as daily close-to-close accounting unless open/VWAP execution is explicitly implemented and tested in the performance path.
Failure Modes
- Using daily rebalance by default without checking turnover.
- Confusing rebalance trades with order-mode fills.
- Assuming drift threshold triggers are free of transaction cost.
- Ignoring the cash buffer applied by the performance path.
- Advertising open/VWAP weight execution before implementation is fully wired.