rebalancing¶
optimizer.rebalancing
¶
Rebalancing frameworks for portfolio management.
Includes calendar-based, threshold-based, and hybrid rebalancing logic, turnover computation, and transaction cost estimation.
CalendarRebalancingConfig
dataclass
¶
Immutable configuration for calendar-based rebalancing.
Triggers portfolio reconstruction at fixed intervals regardless of portfolio drift.
Parameters¶
frequency : RebalancingFrequency Rebalancing frequency.
trading_days
property
¶
Number of trading days between rebalances.
for_monthly()
classmethod
¶
Monthly rebalancing (21 trading days).
for_quarterly()
classmethod
¶
Quarterly rebalancing (63 trading days).
for_semiannual()
classmethod
¶
Semiannual rebalancing (126 trading days).
for_annual()
classmethod
¶
Annual rebalancing (252 trading days).
HybridRebalancingConfig
dataclass
¶
Hybrid rebalancing: check threshold only at calendar review dates.
Combines calendar and threshold strategies: the portfolio is reviewed
at regular calendar intervals, but trades are executed only when drift
exceeds the threshold at that review date. Between review dates,
should_rebalance_hybrid always returns False regardless of drift.
Parameters¶
calendar : CalendarRebalancingConfig Calendar schedule that defines review dates. threshold : ThresholdRebalancingConfig Drift threshold evaluated at each review date.
RebalancingFrequency
¶
Bases: str, Enum
Calendar-based rebalancing frequency.
Each value corresponds to the approximate number of trading days in the rebalancing period.
ThresholdRebalancingConfig
dataclass
¶
Immutable configuration for threshold-based rebalancing.
Rebalances only when portfolio drift exceeds specified limits, avoiding unnecessary turnover during stable periods.
Parameters¶
threshold_type : ThresholdType Whether to use absolute or relative drift thresholds. threshold : float Drift threshold. For absolute: percentage points of weight (e.g. 0.05 = 5pp). For relative: fraction of target weight (e.g. 0.25 = 25% deviation).
ThresholdType
¶
Bases: str, Enum
Threshold convention for drift-based rebalancing.
compute_drifted_weights(weights, returns)
¶
compute_rebalancing_cost(current_weights, target_weights, transaction_costs)
¶
Compute the total transaction cost of rebalancing.
Parameters¶
current_weights : ndarray, shape (n_assets,) Current portfolio weights. target_weights : ndarray, shape (n_assets,) Target portfolio weights. transaction_costs : float or ndarray Per-unit transaction cost (scalar for uniform costs, array for asset-specific costs).
Returns¶
float Total rebalancing cost as a fraction of portfolio value.
compute_turnover(current_weights, target_weights)
¶
should_rebalance(current_weights, target_weights, config=None)
¶
Determine whether any asset breaches the drift threshold.
Parameters¶
current_weights : ndarray, shape (n_assets,) Current (drifted) portfolio weights. target_weights : ndarray, shape (n_assets,) Target portfolio weights from the optimiser. config : ThresholdRebalancingConfig or None Threshold configuration. Defaults to absolute 5pp threshold.
Returns¶
bool
True if at least one asset breaches the threshold.
should_rebalance_hybrid(current_weights, target_weights, config, current_date, last_review_date)
¶
Determine whether to rebalance under a hybrid calendar+threshold policy.
Returns True only when both conditions are met:
current_dateis a calendar review date — at leastconfig.calendar.trading_daysbusiness days have elapsed sincelast_review_date.- At least one asset's drift exceeds the threshold defined in
config.threshold.
Between calendar review dates the function always returns False
regardless of how much drift has accumulated.
Parameters¶
current_weights : ndarray, shape (n_assets,) Current (drifted) portfolio weights. target_weights : ndarray, shape (n_assets,) Target portfolio weights from the optimiser. config : HybridRebalancingConfig Hybrid configuration combining calendar and threshold rules. current_date : pd.Timestamp The date being evaluated. last_review_date : pd.Timestamp Date of the last calendar review.
Returns¶
bool
True only if it is a calendar review date AND drift exceeds
the threshold.