Add --stop-loss and equity/margin threshold alerts (fixes #219)#235
Merged
Conversation
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>
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.
Summary
The maker bot had no automatic financial brake.
alert_lossonly 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 viacancel_maker_orders_with_retry, await the critical webhook, exit) using a newMakerExit::StopLossvariant. 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 belowX.--alert-margin-below <X>— edge-triggered alert when available cross margin drops belowX.The account snapshot already fetched each cycle is threaded out of the cycle (
CycleResult.balance) and compared inAlertMonitor::evaluate_accountwith 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.rsMakerRunArgs,config.rsMakerFileConfig). TheSTANDX_ENABLE_LIVE_MAKERlive 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)AlertMonitorequity-floor edge fire/clear with hysteresis, and margin-floor firing independently (standx-maker)MakerExit::StopLosslifecycle/terminal messaging (standx-cli)stop_loss,alert_equity_below,alert_margin_belowMakerStats, so an adverse mark drivesstats.pnlbelow-stop_lossand the fail-safe shutdown fires.Fixes #219
🤖 Generated with Claude Code