pub struct PortfolioRisk { /* private fields */ }Expand description
Account-wide risk: a latching daily-loss halt plus a pre-trade entry gate over aggregate exposure and concurrency. See the module docs.
§Example
use rustrade_risk::{PortfolioRisk, PortfolioRiskConfig, PortfolioState, PortfolioBlock};
let mut pf = PortfolioRisk::new(PortfolioRiskConfig {
max_daily_loss: -100.0,
max_concurrent_positions: 2,
max_gross_exposure: 10_000.0,
});
// A fresh entry that fits every limit is allowed.
assert!(pf.check_entry(PortfolioState {
open_positions: 1,
gross_exposure: 3_000.0,
new_notional: 2_000.0,
symbol_already_open: false,
account_net_pnl: -10.0,
}).is_ok());
// Once the account net PnL breaches the limit the whole account halts.
pf.observe(-120.0);
assert!(pf.is_halted());
assert!(matches!(
pf.check_entry(PortfolioState {
open_positions: 0, gross_exposure: 0.0, new_notional: 1.0,
symbol_already_open: false, account_net_pnl: -120.0,
}),
Err(PortfolioBlock::DailyLossHalt { .. })
));Implementations§
Source§impl PortfolioRisk
impl PortfolioRisk
Sourcepub fn new(config: PortfolioRiskConfig) -> PortfolioRisk
pub fn new(config: PortfolioRiskConfig) -> PortfolioRisk
Create with the default system clock.
Sourcepub fn with_clock(
config: PortfolioRiskConfig,
clock: Arc<dyn Clock>,
) -> PortfolioRisk
pub fn with_clock( config: PortfolioRiskConfig, clock: Arc<dyn Clock>, ) -> PortfolioRisk
Create with an injected clock — typically Arc<ManualClock> in tests.
Sourcepub fn config(&self) -> &PortfolioRiskConfig
pub fn config(&self) -> &PortfolioRiskConfig
Borrow the configuration.
Sourcepub fn observe(&mut self, account_net_pnl: f64)
pub fn observe(&mut self, account_net_pnl: f64)
Observe the account-wide net realised PnL (the sum of every symbol’s
session net PnL). Latches the daily-loss halt once it breaches the
limit; the latch is sticky until the next UTC rollover (Self::tick).
The framework calls this from its periodic risk sweep.
Sourcepub fn check_entry(&self, state: PortfolioState) -> Result<(), PortfolioBlock>
pub fn check_entry(&self, state: PortfolioState) -> Result<(), PortfolioBlock>
Pre-trade gate for a new entry. Returns Err with the binding
reason if any account-level limit blocks it. Exits / reduce-only orders
should not be gated by this — only entries that add risk.
The daily-loss check fires on either the latched halt or a live breach
in state.account_net_pnl, so a fresh breach blocks immediately even
between sweeps.
Sourcepub fn tick(&mut self)
pub fn tick(&mut self)
Call periodically to detect the 00:00 UTC rollover and clear the halt
latch. Mirrors SessionPnl::tick.
Sourcepub fn reset_session(&mut self)
pub fn reset_session(&mut self)
Force a session reset (normally automatic at the UTC rollover).
Sourcepub fn snapshot(&self) -> PortfolioRiskSnapshot
pub fn snapshot(&self) -> PortfolioRiskSnapshot
Capture the latch state for persistence.
Sourcepub fn restore(&mut self, snap: PortfolioRiskSnapshot)
pub fn restore(&mut self, snap: PortfolioRiskSnapshot)
Restore latch state from a snapshot, keeping the live config + clock.
Call Self::tick afterwards so a snapshot from an earlier UTC day
rolls over instead of resuming a stale halt.
Trait Implementations§
Source§impl Clone for PortfolioRisk
impl Clone for PortfolioRisk
Source§fn clone(&self) -> PortfolioRisk
fn clone(&self) -> PortfolioRisk
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more