Repository navigation
fix(regime): round partial hard-band steps up so zero-target exits finish - #719
Merged
Merged
Conversation
…nish Half-to-even rounding of a one-share gap at hard_band_rebalance_fraction 0.5 gives round(-0.5) == 0, so a zero-target position stranded its last share. That share's full drift re-triggered a hard rebalance on every run. Round the step magnitude up (1e-9 tolerance) so each hard step moves at least one share toward the target and never past it. Soft, flow and deficit paths unchanged.
brndnmtthws
marked this pull request as ready for review
October 16, 2026 20:43
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Defect
With
hard_band_rebalance_fraction < 1(e.g. 0.5), a hard-band step sizes each order asround(gap * fraction). Python rounds half to even, so a one-share gap givesround(-0.5) == 0. A position whose target weight is zero therefore keeps its last share forever. Because a zero target with a live position counts as full drift (relative_drift = 1.0), that share sets off a hard rebalance on every run, which keeps re-sizing (and churning) the rest of the basket.Live evidence
Seen in a live regime-rebalance account during a long zero-target (risk-off) spell for one symbol: the exit halved each run (… → 123 → 61 → 30 → 15 → 7 → 3 → 1) and then stopped at one share. After that, every run was a hard rebalance, about 5.6 orders per session on average, until the symbol's target turned positive again. A replay of 2018–2026 showed no measurable CAGR/drawdown effect, so this fixes order hygiene and a stuck state. It is not a returns change.
Fix
Round the step magnitude up, with a 1e-9 tolerance so float products such as
10 * 0.3don't round up a whole share:Every hard step now moves at least one share toward the target and never past it (
ceil(|gap| * f) <= |gap|forf <= 1). Soft-band, flow and deficit paths are unchanged, a full step (fraction == 1.0) is unchanged, and exits stay staged (492 → 246 → 123 → 61 → 30 → 15 → 7 → 3 → 1 → 0).Behavioral impact: odd partial gaps now trade one share more than before (e.g. 3 of a 5-share gap instead of 2). Risk controls are unaffected.
Tests
New tests in
tests/test_regime_rebalance.py:All three fail on
main(077e457) and pass with the fix. Existing partial-rebalance tests use even gaps and are unchanged.Validation run locally:
uv run pytest: 821 passeduv run ruff check .,uv run ruff format --check .,uv run ty check: cleanMerge caution
Deployments that track the
mainimage pick this up on their next scheduled run. It changes live order sizing, so merge deliberately.