rustrade-risk
Generic risk primitives for the rustrade trading bot
framework. Nothing strategy- or exchange-specific lives here; if a piece
of logic needs to special-case a particular brain or exchange, it belongs
in the brain implementation, not in this crate.
What's inside
| Module | Purpose |
|---|---|
circuit_breaker |
Sliding-window loss breaker with configurable cooldown |
session_pnl |
Realised PnL tracker, drawdown cap, automatic 00:00 UTC rollover |
sizing |
Notional-based contract sizing from margin × leverage |
Usage
use ;
let mut breaker = new;
let sizer = new;
let mut pnl = new;
// Pre-trade gates (this order matters — see the workspace TODO.md):
if pnl.is_session_halted || breaker.is_tripped
let contracts = sizer.contracts;
// ... place order ...
// Post-close:
pnl.record_close;
if net < 0.0 else
Design notes
CircuitBreakeruses a rolling window rather than consecutive losses, because losses spaced hours apart would reset a consecutive counter before ever tripping it. A single win does not clear the tripped state — only elapsed cooldown does.SessionPnlclassifies trades as W/L/B on net (after fees) PnL so that fee-flipped trades (small gross win, large fee = real loss) are counted correctly.PositionSizerreturns0on any degenerate input rather than panicking; callers must treat0as "skip this trade, too small."
Status
Complete. 13 unit tests + 2 doc tests pass on the workspace's pinned
toolchain. See TODO.md for the small polish items
(proptest coverage, injectable clock for time-dependent tests).
Licence
MIT.