pub struct SessionPnl {
pub symbol: String,
pub realised: f64,
pub fees: f64,
pub trades: u32,
pub wins: u32,
pub losses: u32,
pub breakevens: u32,
/* private fields */
}Expand description
Running PnL totals for one trading session, with an optional drawdown cap that auto-resets at the daily 00:00 UTC boundary.
§Example
use rustrade_risk::{SessionPnl, SessionPnlConfig};
let mut pnl = SessionPnl::new("XBTUSDTM", SessionPnlConfig {
loss_limit: -100.0,
});
pnl.record_close(20.0, 1.5); // gross +20, fee 1.5 → net +18.5
pnl.record_close(-50.0, 1.5); // gross -50, fee 1.5 → net -51.5
pnl.record_close(-80.0, 1.5); // running net = -113.0 → halts (≤ -100)
assert!(pnl.is_session_halted());Fields§
§symbol: StringSymbol this PnL tracker is for (used in log records).
realised: f64Cumulative gross realised PnL, in quote currency.
fees: f64Cumulative fees paid this session, in quote currency.
trades: u32Total trades recorded this session.
wins: u32Wins (net PnL > 0).
losses: u32Losses (net PnL < 0).
breakevens: u32Break-evens (net PnL == 0).
Implementations§
Source§impl SessionPnl
impl SessionPnl
Sourcepub fn new(symbol: impl Into<String>, config: SessionPnlConfig) -> Self
pub fn new(symbol: impl Into<String>, config: SessionPnlConfig) -> Self
Create with the default system clock.
Sourcepub fn with_clock(
symbol: impl Into<String>,
config: SessionPnlConfig,
clock: Arc<dyn Clock>,
) -> Self
pub fn with_clock( symbol: impl Into<String>, config: SessionPnlConfig, clock: Arc<dyn Clock>, ) -> Self
Create with an injected clock — typically Arc<ManualClock> from
crate::clock in tests.
Sourcepub fn is_session_halted(&self) -> bool
pub fn is_session_halted(&self) -> bool
Is the session currently halted by the loss cap?
Sourcepub fn record_close(&mut self, gross_pnl: f64, fee: f64)
pub fn record_close(&mut self, gross_pnl: f64, fee: f64)
Record a closed trade.
gross_pnl is the realised PnL before fees (negative for losses).
fee is the round-trip fee charged for opening + closing the
position. The trade is classified as W/L/B on net PnL so that
fee-flipped trades (small gross win, large fee = real loss) count
correctly.
Sourcepub fn tick(&mut self)
pub fn tick(&mut self)
Call periodically (e.g. once per candle poll) to detect 00:00 UTC rollover and reset the session totals + halt flag.
Sourcepub fn snapshot(&self) -> SessionPnlSnapshot
pub fn snapshot(&self) -> SessionPnlSnapshot
Capture the mutable session state for persistence.
Pairs with Self::restore. The configured loss_limit, symbol,
and clock are intentionally excluded — they belong to the live
instance, not the snapshot.
Sourcepub fn restore(&mut self, snap: SessionPnlSnapshot)
pub fn restore(&mut self, snap: SessionPnlSnapshot)
Restore session state from a SessionPnlSnapshot.
Overwrites the running totals, halt flag, and last-reset day; keeps
the symbol, configured loss limit, and clock from the live instance.
Call Self::tick afterwards so a snapshot taken on an earlier UTC
day rolls over to a fresh session instead of resuming a stale halt.
Sourcepub fn reset_session(&mut self)
pub fn reset_session(&mut self)
Force a session reset. Normally called automatically by tick at
the daily UTC rollover.
Trait Implementations§
Source§impl Clone for SessionPnl
impl Clone for SessionPnl
Source§fn clone(&self) -> SessionPnl
fn clone(&self) -> SessionPnl
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more