Documentation
Skip to content

Rebalancing

The multi-asset rebalancing tutorial runs equities, ETFs, futures, and FX from bundled inputs.

For multi-asset strategies that target portfolio weights, the broker provides rebalance_to_weights() and the execution module provides a TargetWeightExecutor for advanced control.

The broker calls below belong inside a strategy callback. The linked multi-asset tutorial supplies the feed, strategy, and result checks.

Simple Rebalancing

class EqualWeightStrategy(Strategy):
    def __init__(self, assets, rebalance_interval=21):
        self.assets = assets
        self.rebalance_interval = rebalance_interval
        self.bar_count = 0

    def on_data(self, timestamp, data, context, broker):
        self.bar_count += 1
        if self.bar_count % self.rebalance_interval != 1:
            return

        n = len(self.assets)
        weights = {asset: 1.0 / n for asset in self.assets}
        broker.rebalance_to_weights(weights)

rebalance_to_weights() computes the delta between current holdings and target weights, then submits sell orders (to reduce overweight positions) before buy orders (to fill underweight positions).

Rebalance Modes

Set RebalanceConfig.rebalance_mode when using TargetWeightExecutor:

Mode Behavior inside an executor call
SNAPSHOT (executor default) Use equity and current weights captured at the start of the call for every target.
INCREMENTAL Process eligible orders between submissions; refresh equity and current weights for the next target.
HYBRID Keep the initial equity and weights while processing eligible orders between submissions.
from ml4t.backtest.config import RebalanceMode
from ml4t.backtest.execution.rebalancer import TargetWeightExecutor, RebalanceConfig

executor = TargetWeightExecutor(RebalanceConfig(rebalance_mode=RebalanceMode.SNAPSHOT))

Execution timing and account rules still determine which orders can fill. The broker's rebalance_to_weights() helper computes targets from account value captured at the start of its call; BacktestConfig.rebalance_mode does not select an executor mode. For framework comparisons, use the explicitly configured comparison profiles.

Rebalance Headroom

The broker's rebalance_headroom_pct parameter scales weights passed to rebalance_to_weights(). Its default is 1.0; set a smaller value to reserve part of the requested allocation:

config = BacktestConfig(rebalance_headroom_pct=0.998)
# Request 99.8% of each target weight

This creates headroom in the requested targets. Rounding, costs and subsequent price changes still affect the resulting exposure. The 0.998 value above is an explicit example setting, not a Backtrader default.

Late Assets and Missing Prices

When assets start trading at different times (e.g., IPOs), two parameters control behavior:

from ml4t.backtest.config import LateAssetPolicy, MissingPricePolicy

config = BacktestConfig(
    # Require 2 bars of history before trading an asset
    late_asset_policy=LateAssetPolicy.REQUIRE_HISTORY,
    late_asset_min_bars=2,

    # Use last known price when current bar is missing
    missing_price_policy=MissingPricePolicy.USE_LAST,
)

Rebalance after all daily closes

When daily bars have different close times, exact-timestamp callbacks contain only the assets closing at that instant. Building a target dictionary from data at each callback may leave out held assets, and TargetWeightExecutor submits closes for holdings missing from the target. Use the feed's explicit session_col option for a complete cross-sectional decision. The callback runs after the last close in the session and receives one bar per asset; orders then wait for each asset's next bar. See Daily decisions across different close times for the input contract and example. Keep execution_mode=NEXT_BAR for this workflow.

Advanced: TargetWeightExecutor

For more control, use the TargetWeightExecutor and RebalanceConfig API:

from ml4t.backtest.execution.rebalancer import TargetWeightExecutor, RebalanceConfig

rebalance_config = RebalanceConfig(
    min_trade_value=100,        # Optional: skip trades smaller than $100
    min_weight_change=0.01,     # Optional: skip changes smaller than 1%
    allow_fractional=False,     # Round to whole shares
    max_single_weight=0.25,     # Limit target weight before share rounding
    cancel_before_rebalance=True,
)

executor = TargetWeightExecutor(rebalance_config)

With $100,000 equity and a $150 price, a 25% target rounds to 167 whole shares: $25,050 of exposure. max_single_weight limits the requested target before rounding; it does not enforce a continuous limit on realized portfolio weights.

The executor integrates with external portfolio optimizers (riskfolio-lib, PyPortfolioOpt, cvxpy) through the WeightProvider protocol:

class MyOptimizer:
    def get_weights(self, data, broker):
        # Your optimization logic here
        return {"AAPL": 0.3, "MSFT": 0.3, "GOOG": 0.4}

By default, RebalanceConfig uses min_trade_value=0.0 and min_weight_change=0.0, so no trade-size or weight-delta filter is applied unless you opt into one explicitly.

Schedule metadata

Session-based schedules need enough metadata to identify exchange session closes. On the next-version calendar boundary, omission in RebalanceConfig and the scheduling functions resolves to NYSE; resolve_rebalance_timestamps() first inherits declared feed metadata. Explicit calendar=None disables that calendar selection, including when the feed names an exchange. Weekly and month-end schedules reject calendar-free operation. Use a named calendar for another exchange or continuous trading. Daily feeds must also set data_frequency="daily". Explicit event_time or bar_close semantics still require observations at the actual session close, even at daily frequency. Intraday feeds must set calendar, timezone, data_frequency, and timestamp_semantics. Set session_start_time for a custom evening boundary. Morning-start sessions use the local calendar date, so a morning value may only restate the configured calendar's standard open. Intraday feeds without calendar boundary metadata may instead pass is_session_close=True or False on every event. Under this option, session identity uses the timestamp's raw date, so it is valid only when sessions do not cross local midnight. Overnight sessions must provide data_frequency and timestamp_semantics. Do not mix explicit and inferred boundaries on one date. Without either form of intraday metadata, events less than 12 hours apart are rejected.

from ml4t.backtest import RebalanceSchedule
from ml4t.backtest.execution import RebalanceConfig

rebalance_config = RebalanceConfig(
    schedule=RebalanceSchedule.fixed_n_sessions(5),
    calendar="CME_Equity",
    timezone="UTC",
    session_start_time="17:00",
    data_frequency="1m",
    timestamp_semantics="bar_close",
)

Naive timestamps use timezone as their source timezone. Session boundaries are then applied in the exchange timezone. Weekly and month-end schedules fire only on the final scheduled exchange session in the period. If the feed advances into a later period without the completed period's final exchange session, evaluation raises a feed-completeness error. An incomplete final period does not move the rebalance to an earlier bar.

For intraday schedules, every complete exchange session that requires a rebalance must contain a timestamp aligned with the exchange close. Batch and online evaluation identify the first complete required session whose close was reached but never matched. Exchange holidays and a final session whose data ends before its close are not alignment failures.

Explicit timestamp schedules match absolute instants. A naive scheduled timestamp uses the configured timezone, so it can match an aware feed timestamp expressed in another timezone. If a scheduled instant falls between the first and last observed feed instants but no event matches it, batch resolution and final online validation raise an alignment error. Scheduled instants before or after the observed window are ignored, including a schedule earlier than the first event on a partially observed day. Online evaluation requires chronological events and rejects a timestamp that moves backward.

TargetWeightExecutor.prepare_schedule() was removed before 0.1.0 because it required the future feed timestamp sequence. Put the metadata shown above on RebalanceConfig; should_rebalance() then evaluates only the current event and prior evaluator state. Call should_rebalance() or scheduled execute() for every event in chronological order, including warm-up periods. After the final event, call validate_completed_run() so a missing close in the final session is reported. When scheduled execute() receives an Engine broker, the Engine registers and runs this final validation automatically. Standalone use and direct should_rebalance() calls still require the explicit completion call. Gating execute() on should_rebalance() for the same timestamp is supported when both calls receive the same is_session_close, or when only should_rebalance() receives it. every_bar schedules impose no event-coverage requirement. Other conditional skips fail completion validation because session counters would be incomplete. Call reset() before reusing an executor for another run or changing its public RebalanceConfig. Backward session dates and mid-run configuration changes raise instead of silently restarting session counters. The engine validates LongShortStrategy schedule alignment after on_end, independently of any callback override.

In the book

Chapter 17, Section 17.7, Library comparison compares allocation methods on matched inputs. This page covers how those target weights become timed, sized orders.

Next Steps