Skip to content

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.

for_monthly_with_5pct_threshold() classmethod

Monthly review with 5pp absolute drift threshold.

for_quarterly_with_10pct_threshold() classmethod

Quarterly review with 10pp absolute drift threshold.

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).

for_absolute(threshold=0.05) classmethod

Absolute drift threshold (default 5pp).

for_relative(threshold=0.25) classmethod

Relative drift threshold (default 25%).

ThresholdType

Bases: str, Enum

Threshold convention for drift-based rebalancing.

compute_drifted_weights(weights, returns)

Compute portfolio weights after one period of returns.

Parameters

weights : ndarray, shape (n_assets,) Current portfolio weights (must sum to 1). returns : ndarray, shape (n_assets,) Single-period asset returns.

Returns

ndarray, shape (n_assets,) Drifted weights after applying 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)

Compute one-way turnover between current and target weights.

Parameters

current_weights : ndarray, shape (n_assets,) Current portfolio weights. target_weights : ndarray, shape (n_assets,) Target portfolio weights.

Returns

float One-way turnover (sum of absolute weight changes / 2).

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:

  1. current_date is a calendar review date — at least config.calendar.trading_days business days have elapsed since last_review_date.
  2. 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.