Skip to content

fix(strategies): guard Positive boundaries in butterfly/spread P&L (#387)#389

Merged
joaquinbejar merged 1 commit into
mainfrom
issue-387-positive-panic
Apr 19, 2026
Merged

fix(strategies): guard Positive boundaries in butterfly/spread P&L (#387)#389
joaquinbejar merged 1 commit into
mainfrom
issue-387-positive-panic

Conversation

@joaquinbejar

Copy link
Copy Markdown
Owner

Summary

Six example binaries panicked mid-optimizer-scan with `Positive invariant broken` because strategy P&L and break-even expressions crossed the `Positive` boundary with an unchecked `Add` / `Sub`.

Root-cause fixes:

  • `CallButterfly::update_break_even_points`: `strike ± profit/qty` lowered to `Decimal` then wrapped via `Positive::new_decimal(..)`; out-of-range candidates are silently dropped instead of panicking. The same optimizer then skips that combination cleanly.
  • `CallButterfly::get_profit_area`: `be[1] - be[0]` and `short_high_strike - short_low_strike` lowered to `Decimal` first, then wrapped; non-positive widths fall back to `Positive::ZERO`.
  • `LongButterflySpread::update_break_even_points`: same `Positive ± Decimal` guard.
  • `BullPutSpread::get_max_loss`: width computed as `Decimal`, wrapped with `Positive::new_decimal(..).map_err` instead of `Positive - Positive`; inverted-strike candidates now return a typed `MaxLossError` instead of panicking.

Unblocks: `strategy_call_butterfly_best_{area,ratio}`, `strategy_long_butterfly_spread_best_{area,ratio}`, `strategy_call_butterfly_delta`, `strategy_bull_put_spread_extended_delta`.

Closes #387.

Test plan

  • All six example binaries run to completion.
  • `cargo test --lib --all-features` — 3760 pass, 0 fail.
  • `cargo clippy --all-targets --all-features -- -D warnings` clean.
  • `cargo fmt --all --check`.

…th (#387)

Multiple example binaries panicked mid-optimizer-scan with
`Positive invariant broken` because strategy P&L and break-even
expressions crossed the `Positive` boundary with an unchecked
`Add<Decimal>` / `Sub<Positive>`:

- `CallButterfly::update_break_even_points`: `strike ± profit/qty`
  replaced by `Positive::new_decimal(..)` on the underlying `Decimal`
  sum; out-of-range candidates are dropped cleanly instead of
  panicking.
- `CallButterfly::get_profit_area`: `be[1] - be[0]` and
  `short_high_strike - short_low_strike` lowered to `Decimal` first
  then wrapped; non-positive widths fall back to `Positive::ZERO`.
- `LongButterflySpread::update_break_even_points`: same `Positive
  ± Decimal` guard.
- `BullPutSpread::get_max_loss`: width computed as `Decimal`,
  wrapped with `Positive::new_decimal(..).map_err` instead of
  `Positive - Positive`; inverted-strike candidates return a typed
  `MaxLossError` instead of panicking.

Unblocks the following example binaries mid-optimizer-scan:
`strategy_call_butterfly_best_{area,ratio}`,
`strategy_long_butterfly_spread_best_{area,ratio}`,
`strategy_call_butterfly_delta`,
`strategy_bull_put_spread_extended_delta`.

Closes #387.
@joaquinbejar joaquinbejar merged commit 0c12afd into main Apr 19, 2026
11 checks passed
@joaquinbejar joaquinbejar deleted the issue-387-positive-panic branch April 19, 2026 21:53
@codecov

codecov Bot commented Apr 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.47368% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/strategies/bull_put_spread.rs 66.66% 4 Missing ⚠️
Files with missing lines Coverage Δ
src/strategies/call_butterfly.rs 76.31% <100.00%> (+0.29%) ⬆️
src/strategies/long_butterfly_spread.rs 73.56% <100.00%> (+0.09%) ⬆️
src/strategies/bull_put_spread.rs 76.97% <66.66%> (-0.20%) ⬇️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

strategies: Positive + Decimal panic in CallButterfly / LongButterflySpread break-even computation

1 participant