pub struct Portfolio { /* private fields */ }Implementations§
Source§impl Portfolio
impl Portfolio
pub fn new( clock: Rc<RefCell<dyn Clock>>, cache: Rc<RefCell<Cache>>, config: Option<PortfolioConfig>, ) -> Self
Sourcepub fn clone_shallow(&self) -> Self
pub fn clone_shallow(&self) -> Self
Creates a shallow clone of the Portfolio that shares the same internal state.
This is useful when multiple components need to reference the same Portfolio without creating duplicate msgbus handler registrations.
pub fn reset(&mut self)
Sourcepub fn is_initialized(&self) -> bool
pub fn is_initialized(&self) -> bool
Returns true if the portfolio has been initialized.
Sourcepub fn balances_locked(&self, venue: &Venue) -> IndexMap<Currency, Money>
pub fn balances_locked(&self, venue: &Venue) -> IndexMap<Currency, Money>
Returns the locked balances for the given venue.
Locked balances represent funds reserved for open orders.
Sourcepub fn margins_init(&self, venue: &Venue) -> IndexMap<InstrumentId, Money>
pub fn margins_init(&self, venue: &Venue) -> IndexMap<InstrumentId, Money>
Returns the initial margin requirements for the given venue.
Only applicable for margin accounts. Returns empty map for cash accounts.
Sourcepub fn margins_maint(&self, venue: &Venue) -> IndexMap<InstrumentId, Money>
pub fn margins_maint(&self, venue: &Venue) -> IndexMap<InstrumentId, Money>
Returns the maintenance margin requirements for the given venue.
Only applicable for margin accounts. Returns empty map for cash accounts.
Sourcepub fn unrealized_pnls(
&mut self,
venue: &Venue,
account_id: Option<&AccountId>,
) -> IndexMap<Currency, Money>
pub fn unrealized_pnls( &mut self, venue: &Venue, account_id: Option<&AccountId>, ) -> IndexMap<Currency, Money>
Returns the unrealized PnLs for all positions at the given venue.
Calculates mark-to-market PnL based on current market prices.
Sourcepub fn realized_pnls(
&mut self,
venue: &Venue,
account_id: Option<&AccountId>,
) -> IndexMap<Currency, Money>
pub fn realized_pnls( &mut self, venue: &Venue, account_id: Option<&AccountId>, ) -> IndexMap<Currency, Money>
Returns the realized PnLs for all positions at the given venue.
Calculates total realized profit and loss from closed positions.
pub fn net_exposures( &self, venue: &Venue, account_id: Option<&AccountId>, ) -> Option<IndexMap<Currency, Money>>
pub fn unrealized_pnl(&mut self, instrument_id: &InstrumentId) -> Option<Money>
pub fn unrealized_pnl_for_account( &mut self, instrument_id: &InstrumentId, account_id: Option<&AccountId>, ) -> Option<Money>
pub fn realized_pnl(&mut self, instrument_id: &InstrumentId) -> Option<Money>
pub fn realized_pnl_for_account( &mut self, instrument_id: &InstrumentId, account_id: Option<&AccountId>, ) -> Option<Money>
Sourcepub fn total_pnl(&mut self, instrument_id: &InstrumentId) -> Option<Money>
pub fn total_pnl(&mut self, instrument_id: &InstrumentId) -> Option<Money>
Returns the total PnL for the given instrument ID.
Total PnL = Realized PnL + Unrealized PnL
pub fn total_pnl_for_account( &mut self, instrument_id: &InstrumentId, account_id: Option<&AccountId>, ) -> Option<Money>
Sourcepub fn total_pnls(
&mut self,
venue: &Venue,
account_id: Option<&AccountId>,
) -> IndexMap<Currency, Money>
pub fn total_pnls( &mut self, venue: &Venue, account_id: Option<&AccountId>, ) -> IndexMap<Currency, Money>
Returns the total PnLs for the given venue.
Total PnL = Realized PnL + Unrealized PnL for each currency. Pass account_id
to scope the aggregation to a single account when multiple accounts share the venue.
Sourcepub fn mark_values(
&mut self,
venue: &Venue,
account_id: Option<&AccountId>,
) -> IndexMap<Currency, Money>
pub fn mark_values( &mut self, venue: &Venue, account_id: Option<&AccountId>, ) -> IndexMap<Currency, Money>
Returns the per-currency mark-to-market value of open positions at the given venue.
For each open position the valuation uses the portfolio’s internal price
resolution, which prefers mark prices (when configured), falls back to
side-appropriate bid/ask, then last trade, then the most recent bar close.
Instruments without any available price are skipped and the venue is flagged
for a no-price warning. Pass account_id to scope the aggregation to a
single account when multiple accounts share the venue.
Sourcepub fn equity(
&mut self,
venue: &Venue,
account_id: Option<&AccountId>,
) -> IndexMap<Currency, Money>
pub fn equity( &mut self, venue: &Venue, account_id: Option<&AccountId>, ) -> IndexMap<Currency, Money>
Returns the per-currency total equity for the given venue.
For cash accounts: balance.total + Σ mark_value(open positions) per currency.
For margin accounts: balance.total + Σ unrealized_pnl(open positions) per currency.
Open-position instruments that cannot be priced are tracked via
Portfolio::missing_price_instruments (and warned once) for both branches,
so equity understatement does not go unnoticed. Pass account_id to scope
the aggregation to a single account when multiple accounts share the venue.
Sourcepub fn build_snapshot(
&mut self,
account_id: &AccountId,
) -> Option<PortfolioSnapshot>
pub fn build_snapshot( &mut self, account_id: &AccountId, ) -> Option<PortfolioSnapshot>
Builds a [PortfolioSnapshot] for the given account at the current clock time.
Unrealized PnL and mark values span the venues the account currently
holds open positions on; realized PnL spans every venue the account has
touched (open or closed) so a multi-venue account where one venue is
now flat still reports its accumulated realized PnL. Returns None if
no account is registered.
Sourcepub fn snapshots(&self, account_id: &AccountId) -> Vec<PortfolioSnapshot>
pub fn snapshots(&self, account_id: &AccountId) -> Vec<PortfolioSnapshot>
Returns the recorded portfolio snapshots for the given account, in order of emission.
Snapshots accumulate whenever snapshot_interval_ms is set and the account
holds at least one open position. The ring is bounded; long-lived live
deployments should consume snapshots via the message bus instead of relying
on this buffer. Cleared on Portfolio::reset.
Sourcepub fn missing_price_instruments(&self, venue: &Venue) -> Vec<InstrumentId>
pub fn missing_price_instruments(&self, venue: &Venue) -> Vec<InstrumentId>
Returns the instruments currently flagged as unpriced for the given venue.
An entry is added the first time Portfolio::mark_values cannot source a
price for an open position (after also emitting a warn log), and removed
once the instrument is priced again so a subsequent drop re-warns.
pub fn net_exposure( &self, instrument_id: &InstrumentId, account_id: Option<&AccountId>, ) -> Option<Money>
pub fn net_position(&self, instrument_id: &InstrumentId) -> Decimal
pub fn is_net_long(&self, instrument_id: &InstrumentId) -> bool
pub fn is_net_short(&self, instrument_id: &InstrumentId) -> bool
pub fn is_flat(&self, instrument_id: &InstrumentId) -> bool
pub fn is_completely_flat(&self) -> bool
Sourcepub fn initialize_orders(&mut self)
pub fn initialize_orders(&mut self)
Initializes account margin based on existing open orders.
§Panics
Panics if updating the cache with a mutated account fails.
Sourcepub fn initialize_positions(&mut self)
pub fn initialize_positions(&mut self)
Initializes account margin based on existing open positions.
§Panics
Panics if calculation of PnL or updating the cache with a mutated account fails.
Sourcepub fn update_quote_tick(&mut self, quote: &QuoteTick)
pub fn update_quote_tick(&mut self, quote: &QuoteTick)
Updates portfolio calculations based on a new quote tick.
Recalculates unrealized PnL for positions affected by the quote update.
Sourcepub fn update_bar(&mut self, bar: &Bar)
pub fn update_bar(&mut self, bar: &Bar)
Updates portfolio calculations based on a new bar.
Updates cached bar close prices and recalculates unrealized PnL.
Sourcepub fn update_account(&mut self, event: &AccountState)
pub fn update_account(&mut self, event: &AccountState)
Updates portfolio with a new account state event.
Sourcepub fn update_order(&mut self, event: &OrderEventAny)
pub fn update_order(&mut self, event: &OrderEventAny)
Updates portfolio calculations based on an order event.
Handles balance updates for order fills and margin calculations for order changes.
Sourcepub fn recorded_realized_pnls(
&self,
) -> AHashMap<Currency, Vec<(PositionId, UnixNanos, f64)>>
pub fn recorded_realized_pnls( &self, ) -> AHashMap<Currency, Vec<(PositionId, UnixNanos, f64)>>
Returns realized PnLs recorded during portfolio event processing.
Each record is (position_id, ts_event, realized_pnl).
Sourcepub fn statistics(&self) -> PortfolioStatistics
pub fn statistics(&self) -> PortfolioStatistics
Computes an owned [PortfolioStatistics] snapshot from the portfolio’s current cache state.
Aggregates balances across every account, includes cached positions and their snapshots, and merges close-time PnLs recorded during processing. Recomputes on each call; callers on hot paths should invoke it sparingly.
Sourcepub fn update_position(&mut self, event: &PositionEvent)
pub fn update_position(&mut self, event: &PositionEvent)
Updates portfolio calculations based on a position event.
Recalculates net positions, unrealized PnL, and margin requirements.