Skip to main content

SessionPnl

Struct SessionPnl 

Source
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: String

Symbol this PnL tracker is for (used in log records).

§realised: f64

Cumulative gross realised PnL, in quote currency.

§fees: f64

Cumulative fees paid this session, in quote currency.

§trades: u32

Total trades recorded this session.

§wins: u32

Wins (net PnL > 0).

§losses: u32

Losses (net PnL < 0).

§breakevens: u32

Break-evens (net PnL == 0).

Implementations§

Source§

impl SessionPnl

Source

pub fn new(symbol: impl Into<String>, config: SessionPnlConfig) -> SessionPnl

Create with the default system clock.

Source

pub fn with_clock( symbol: impl Into<String>, config: SessionPnlConfig, clock: Arc<dyn Clock>, ) -> SessionPnl

Create with an injected clock — typically Arc<ManualClock> from crate::clock in tests.

Source

pub fn net_pnl(&self) -> f64

Net PnL after fees.

Source

pub fn win_rate(&self) -> f64

Win rate over decided trades (excludes break-evens).

Source

pub fn is_session_halted(&self) -> bool

Is the session currently halted by the loss cap?

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> SessionPnl

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SessionPnl

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more