Skip to main content

PortfolioRisk

Struct PortfolioRisk 

Source
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

Source

pub fn new(config: PortfolioRiskConfig) -> PortfolioRisk

Create with the default system clock.

Source

pub fn with_clock( config: PortfolioRiskConfig, clock: Arc<dyn Clock>, ) -> PortfolioRisk

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

Source

pub fn is_halted(&self) -> bool

Is the account currently halted by the daily-loss limit?

Source

pub fn config(&self) -> &PortfolioRiskConfig

Borrow the configuration.

Source

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.

Source

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.

Source

pub fn tick(&mut self)

Call periodically to detect the 00:00 UTC rollover and clear the halt latch. Mirrors SessionPnl::tick.

Source

pub fn reset_session(&mut self)

Force a session reset (normally automatic at the UTC rollover).

Source

pub fn snapshot(&self) -> PortfolioRiskSnapshot

Capture the latch state for persistence.

Source

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

Source§

fn clone(&self) -> PortfolioRisk

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 PortfolioRisk

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