Skip to content

Add --stop-loss and equity/margin threshold alerts (fixes #219)#235

Merged
wjllance merged 1 commit into
mainfrom
fix-219-stop-loss
Jul 13, 2026
Merged

Add --stop-loss and equity/margin threshold alerts (fixes #219)#235
wjllance merged 1 commit into
mainfrom
fix-219-stop-loss

Conversation

@wjllance

Copy link
Copy Markdown
Owner

Summary

The maker bot had no automatic financial brake. alert_loss only sent a notification while the bot kept quoting, and account equity / available margin were fetched every cycle but never compared to any threshold — liquidation risk was watched only by a human.

This PR adds three opt-in financial guards:

  • --stop-loss <X> — when session mark-to-market PnL breaches -X (quote units), the bot routes through the existing fail-safe shutdown path (freeze, cancel the maker book via cancel_maker_orders_with_retry, await the critical webhook, exit) using a new MakerExit::StopLoss variant. No new shutdown mechanism is introduced. The check runs in the runtime cycle loop over the just-updated stats.
  • --alert-equity-below <X> — edge-triggered alert when account equity drops below X.
  • --alert-margin-below <X> — edge-triggered alert when available cross margin drops below X.

The account snapshot already fetched each cycle is threaded out of the cycle (CycleResult.balance) and compared in AlertMonitor::evaluate_account with a 10% recovery hysteresis to avoid flapping. The two account alerts are live-only (no account snapshot in paper mode).

All three flags flow CLI → file → default via the existing choose() pattern (cli.rs, mod.rs MakerRunArgs, config.rs MakerFileConfig). The STANDX_ENABLE_LIVE_MAKER live lock is untouched.

Test plan

  • cargo fmt -- --check (clean)
  • cargo clippy --workspace --all-targets -- -D warnings (clean)
  • cargo test -p standx-cli -p standx-maker (all pass)
  • New unit tests:
    • AlertMonitor equity-floor edge fire/clear with hysteresis, and margin-floor firing independently (standx-maker)
    • MakerExit::StopLoss lifecycle/terminal messaging (standx-cli)
    • config parsing of stop_loss, alert_equity_below, alert_margin_below
  • Stop-loss is verifiable in paper mode: paper fills feed MakerStats, so an adverse mark drives stats.pnl below -stop_loss and the fail-safe shutdown fires.

Fixes #219

🤖 Generated with Claude Code

The maker bot had no automatic financial brake: alert_loss only sent a
notification while quoting continued, and account equity/available margin
were fetched every cycle but never compared against any threshold, so
liquidation risk was watched only by a human.

This adds:

- `--stop-loss <X>`: when session mark-to-market PnL breaches -X, route
  through the existing fail-safe shutdown path (freeze, cancel the maker
  book via cancel_maker_orders_with_retry, await the critical webhook,
  exit) using a new MakerExit::StopLoss variant. No new shutdown
  mechanism is introduced. The check runs in the runtime cycle loop over
  the just-updated stats, so it is verifiable in paper mode (paper fills
  feed MakerStats).
- `--alert-equity-below <X>` / `--alert-margin-below <X>`: edge-triggered
  alerts over the account snapshot already fetched each cycle. The
  snapshot is threaded out of the cycle (CycleResult.balance) and
  compared in AlertMonitor::evaluate_account with 10% recovery
  hysteresis. Live-only (no account snapshot in paper).

All three flags flow CLI -> file -> default through the existing
choose() pattern (cli.rs, mod.rs MakerRunArgs, config.rs
MakerFileConfig). The STANDX_ENABLE_LIVE_MAKER live lock is untouched.

Tests: AlertMonitor account-floor edge/hysteresis, stop-loss exit
messaging, and config parsing of the new fields.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@wjllance wjllance merged commit 292ab85 into main Jul 13, 2026
7 checks passed
@wjllance wjllance deleted the fix-219-stop-loss branch July 13, 2026 07:57
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.

[P0][Feature] 止损 --stop-loss + equity/保证金阈值告警——金融层目前无任何刹车

2 participants