#[repr(C)]pub struct PortfolioAnalyzer {
pub statistics: AHashMap<String, Statistic>,
pub account_balances_starting: IndexMap<Currency, Money>,
pub account_balances: IndexMap<Currency, Money>,
pub positions: Vec<Position>,
pub realized_pnls: AHashMap<Currency, Vec<(PositionId, UnixNanos, f64)>>,
pub recorded_realized_pnls: AHashMap<Currency, Vec<(PositionId, UnixNanos, f64)>>,
pub position_returns: Returns,
pub portfolio_returns: Returns,
pub returns: Returns,
}Expand description
Analyzes portfolio performance and calculates various statistics.
The PortfolioAnalyzer tracks account balances, positions, and realized PnLs
to provide portfolio analysis including returns, PnL calculations,
and customizable statistics.
Fields§
§statistics: AHashMap<String, Statistic>§account_balances_starting: IndexMap<Currency, Money>§account_balances: IndexMap<Currency, Money>§positions: Vec<Position>§realized_pnls: AHashMap<Currency, Vec<(PositionId, UnixNanos, f64)>>§recorded_realized_pnls: AHashMap<Currency, Vec<(PositionId, UnixNanos, f64)>>§position_returns: Returns§portfolio_returns: Returns§returns: ReturnsAlias for the primary returns source.
Contains portfolio returns when available, otherwise position returns.
Kept as a public field for API stability; prefer the returns() accessor.
Implementations§
Source§impl PortfolioAnalyzer
impl PortfolioAnalyzer
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new PortfolioAnalyzer instance.
Starts with empty state.
Sourcepub fn register_statistic(&mut self, statistic: Statistic)
pub fn register_statistic(&mut self, statistic: Statistic)
Registers a new portfolio statistic for calculation.
Sourcepub fn deregister_statistic(&mut self, statistic: &Statistic)
pub fn deregister_statistic(&mut self, statistic: &Statistic)
Removes a specific statistic from calculation.
Sourcepub fn deregister_statistics(&mut self)
pub fn deregister_statistics(&mut self)
Removes all registered statistics.
Sourcepub fn currencies(&self) -> Vec<&Currency>
pub fn currencies(&self) -> Vec<&Currency>
Returns all tracked currencies.
Sourcepub fn statistic(&self, name: &str) -> Option<&Statistic>
pub fn statistic(&self, name: &str) -> Option<&Statistic>
Retrieves a specific statistic by name.
Sourcepub const fn returns(&self) -> &Returns
pub const fn returns(&self) -> &Returns
Returns the primary calculated returns.
This returns portfolio returns when available, otherwise it falls back to position returns for backward compatibility.
Sourcepub const fn position_returns(&self) -> &Returns
pub const fn position_returns(&self) -> &Returns
Returns the per-position calculated returns.
Sourcepub const fn portfolio_returns(&self) -> &Returns
pub const fn portfolio_returns(&self) -> &Returns
Returns the portfolio calculated returns.
Sourcepub fn calculate_statistics(
&mut self,
account: &dyn Account,
positions: &[Position],
)
pub fn calculate_statistics( &mut self, account: &dyn Account, positions: &[Position], )
Calculates statistics based on account and position data.
This clears calculated state before calculating, while preserving close-time PnLs recorded during portfolio processing.
Sourcepub fn from_accounts(
accounts: &[AccountAny],
positions: &[Position],
snapshots: &[Position],
recorded_realized_pnls: AHashMap<Currency, Vec<(PositionId, UnixNanos, f64)>>,
) -> Self
pub fn from_accounts( accounts: &[AccountAny], positions: &[Position], snapshots: &[Position], recorded_realized_pnls: AHashMap<Currency, Vec<(PositionId, UnixNanos, f64)>>, ) -> Self
Builds a populated analyzer from venue accounts and positions.
Aggregates starting and total balances across all accounts, adds positions and
snapshots, and seeds recorded_realized_pnls (close-time PnLs observed during the run).
Sourcepub fn from_accounts_with_snapshots<'a>(
accounts: &[AccountAny],
positions: &[Position],
position_snapshots: &[Position],
portfolio_snapshots: impl IntoIterator<Item = &'a PortfolioSnapshot>,
recorded_realized_pnls: AHashMap<Currency, Vec<(PositionId, UnixNanos, f64)>>,
) -> Self
pub fn from_accounts_with_snapshots<'a>( accounts: &[AccountAny], positions: &[Position], position_snapshots: &[Position], portfolio_snapshots: impl IntoIterator<Item = &'a PortfolioSnapshot>, recorded_realized_pnls: AHashMap<Currency, Vec<(PositionId, UnixNanos, f64)>>, ) -> Self
Builds a populated analyzer from accounts, positions, and portfolio snapshots.
Portfolio returns use daily mark-to-market equity when at least two UTC dates are available and every account resolves to one common currency. Otherwise the primary returns source falls back to position returns.
Sourcepub fn set_portfolio_returns_from_snapshots<'a>(
&mut self,
account_ids: &[AccountId],
snapshots: impl IntoIterator<Item = &'a PortfolioSnapshot>,
)
pub fn set_portfolio_returns_from_snapshots<'a>( &mut self, account_ids: &[AccountId], snapshots: impl IntoIterator<Item = &'a PortfolioSnapshot>, )
Replaces the primary returns source with snapshot-backed portfolio returns when resolvable.
Sourcepub fn statistics(&self) -> PortfolioStatistics
pub fn statistics(&self) -> PortfolioStatistics
Collects an owned PortfolioStatistics snapshot from the current analyzer state.
Sourcepub fn add_positions(&mut self, positions: &[Position])
pub fn add_positions(&mut self, positions: &[Position])
Adds new positions for analysis.
Sourcepub fn add_trade(
&mut self,
position_id: &PositionId,
ts_event: UnixNanos,
pnl: &Money,
)
pub fn add_trade( &mut self, position_id: &PositionId, ts_event: UnixNanos, pnl: &Money, )
Records a trade’s PnL realized at ts_event.
Sourcepub fn record_trade(
&mut self,
position_id: &PositionId,
ts_event: UnixNanos,
pnl: &Money,
)
pub fn record_trade( &mut self, position_id: &PositionId, ts_event: UnixNanos, pnl: &Money, )
Records a trade’s PnL realized at ts_event, observed during portfolio processing.
Sourcepub fn add_position_return(&mut self, timestamp: UnixNanos, value: f64)
pub fn add_position_return(&mut self, timestamp: UnixNanos, value: f64)
Records a position return at a specific timestamp.
Sourcepub fn add_return(&mut self, timestamp: UnixNanos, value: f64)
pub fn add_return(&mut self, timestamp: UnixNanos, value: f64)
Records a return at a specific timestamp.
This is a backward-compatible alias for Self::add_position_return.
Sourcepub fn trade_pnl_records(
&self,
currency: Option<&Currency>,
) -> Option<Vec<(PositionId, UnixNanos, f64)>>
pub fn trade_pnl_records( &self, currency: Option<&Currency>, ) -> Option<Vec<(PositionId, UnixNanos, f64)>>
Retrieves trade PnL records for a specific currency.
Each record is (position_id, ts_event, realized_pnl), where ts_event is the
position’s last event time (the close time for closed cycles). Duplicate position
IDs are preserved for NETTING position cycles.
Native PnLs (derived from analyzed positions) and PnLs recorded live during
portfolio processing are merged per cycle: a native record is excluded only when a
recorded record shares its (position_id, ts_event). Recorded values therefore take
precedence for the cycles they cover, while native cycles that were never recorded
are retained rather than dropped by position ID.
Returns None if no PnLs exist, or if multiple currencies exist
without an explicit currency specified.
Sourcepub fn realized_pnls(
&self,
currency: Option<&Currency>,
) -> Option<Vec<(PositionId, UnixNanos, f64)>>
pub fn realized_pnls( &self, currency: Option<&Currency>, ) -> Option<Vec<(PositionId, UnixNanos, f64)>>
Retrieves realized PnLs for a specific currency.
Each record is (position_id, ts_event, realized_pnl). Returns None if no PnLs
exist, or if multiple currencies exist without an explicit currency specified.
Sourcepub fn total_pnl(
&self,
currency: Option<&Currency>,
unrealized_pnl: Option<&Money>,
) -> Result<f64, &'static str>
pub fn total_pnl( &self, currency: Option<&Currency>, unrealized_pnl: Option<&Money>, ) -> Result<f64, &'static str>
Calculates total PnL including unrealized PnL if provided.
§Errors
Returns an error if:
- No currency is specified in a multi-currency portfolio.
- The specified currency is not found in account balances.
- The unrealized PnL currency does not match the specified currency.
Sourcepub fn total_pnl_percentage(
&self,
currency: Option<&Currency>,
unrealized_pnl: Option<&Money>,
) -> Result<f64, &'static str>
pub fn total_pnl_percentage( &self, currency: Option<&Currency>, unrealized_pnl: Option<&Money>, ) -> Result<f64, &'static str>
Calculates total PnL as a percentage of starting balance.
§Errors
Returns an error if:
- No currency is specified in a multi-currency portfolio.
- The specified currency is not found in account balances.
- The unrealized PnL currency does not match the specified currency.
Sourcepub fn get_performance_stats_pnls(
&self,
currency: Option<&Currency>,
unrealized_pnl: Option<&Money>,
) -> Result<AHashMap<String, f64>, &'static str>
pub fn get_performance_stats_pnls( &self, currency: Option<&Currency>, unrealized_pnl: Option<&Money>, ) -> Result<AHashMap<String, f64>, &'static str>
Gets all PnL-related performance statistics.
§Errors
Returns an error if PnL calculations fail, for example due to:
- No currency specified for a multi-currency portfolio.
- Unrealized PnL currency not matching the specified currency.
- Specified currency not found in account balances.
Sourcepub fn get_performance_stats_returns(&self) -> AHashMap<String, f64>
pub fn get_performance_stats_returns(&self) -> AHashMap<String, f64>
Gets all return-based performance statistics.
Sourcepub fn get_performance_stats_position_returns(&self) -> AHashMap<String, f64>
pub fn get_performance_stats_position_returns(&self) -> AHashMap<String, f64>
Gets all position-return-based performance statistics.
Sourcepub fn get_performance_stats_portfolio_returns(&self) -> AHashMap<String, f64>
pub fn get_performance_stats_portfolio_returns(&self) -> AHashMap<String, f64>
Gets all portfolio-return-based performance statistics.
Sourcepub fn get_performance_stats_returns_vs_benchmark(
&self,
benchmark: &Returns,
) -> AHashMap<String, f64>
pub fn get_performance_stats_returns_vs_benchmark( &self, benchmark: &Returns, ) -> AHashMap<String, f64>
Gets all benchmark-relative return statistics for the primary returns.
This is stateless: the benchmark series is supplied by the caller rather
than stored on the analyzer. Only statistics that override
PortfolioStatistic::calculate_from_returns_with_benchmark (the benchmark-relative
statistics) contribute values; all others return None and are skipped.
Sourcepub fn get_performance_stats_general(&self) -> AHashMap<String, f64>
pub fn get_performance_stats_general(&self) -> AHashMap<String, f64>
Gets general portfolio statistics.
Sourcepub fn get_stats_pnls_formatted(
&self,
currency: Option<&Currency>,
unrealized_pnl: Option<&Money>,
) -> Result<Vec<String>, String>
pub fn get_stats_pnls_formatted( &self, currency: Option<&Currency>, unrealized_pnl: Option<&Money>, ) -> Result<Vec<String>, String>
Gets formatted PnL statistics as strings.
§Errors
Returns an error if PnL statistics calculation fails.
Sourcepub fn get_stats_returns_formatted(&self) -> Vec<String>
pub fn get_stats_returns_formatted(&self) -> Vec<String>
Gets formatted return statistics as strings.
Sourcepub fn get_stats_position_returns_formatted(&self) -> Vec<String>
pub fn get_stats_position_returns_formatted(&self) -> Vec<String>
Gets formatted position-return statistics as strings.
Sourcepub fn get_stats_portfolio_returns_formatted(&self) -> Vec<String>
pub fn get_stats_portfolio_returns_formatted(&self) -> Vec<String>
Gets formatted portfolio-return statistics as strings.
Sourcepub fn get_stats_general_formatted(&self) -> Vec<String>
pub fn get_stats_general_formatted(&self) -> Vec<String>
Gets formatted general statistics as strings.
Trait Implementations§
Source§impl Debug for PortfolioAnalyzer
impl Debug for PortfolioAnalyzer
Source§impl Default for PortfolioAnalyzer
impl Default for PortfolioAnalyzer
Source§fn default() -> Self
fn default() -> Self
Creates a new default PortfolioAnalyzer instance.