pub trait PortfolioStatistic: Debug {
type Item;
// Required method
fn name(&self) -> String;
// Provided methods
fn calculate_from_returns(&self, returns: &Returns) -> Option<Self::Item> { ... }
fn calculate_from_realized_pnls(
&self,
realized_pnls: &[f64],
) -> Option<Self::Item> { ... }
fn calculate_from_orders(
&self,
orders: Vec<Box<dyn Order>>,
) -> Option<Self::Item> { ... }
fn calculate_from_positions(
&self,
positions: &[Position],
) -> Option<Self::Item> { ... }
fn calculate_from_returns_with_benchmark(
&self,
returns: &Returns,
benchmark: &Returns,
) -> Option<Self::Item> { ... }
fn align_returns(&self, a: &Returns, b: &Returns) -> (Vec<f64>, Vec<f64>) { ... }
fn check_valid_returns(&self, returns: &Returns) -> bool { ... }
fn downsample_to_daily_bins(&self, returns: &Returns) -> Returns { ... }
fn calculate_std(&self, returns: &Returns) -> f64 { ... }
}Expand description
Trait for portfolio performance statistics that can be calculated from different data sources.
This trait provides a flexible framework for implementing various financial performance metrics that can operate on returns, realized PnLs, orders, or positions data. Each statistic implementation should override the relevant calculation methods.
Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn calculate_from_returns(&self, returns: &Returns) -> Option<Self::Item>
fn calculate_from_returns(&self, returns: &Returns) -> Option<Self::Item>
Calculates the statistic from time-indexed returns data.
§Panics
Panics if this method is not implemented for the specific statistic.
Sourcefn calculate_from_realized_pnls(
&self,
realized_pnls: &[f64],
) -> Option<Self::Item>
fn calculate_from_realized_pnls( &self, realized_pnls: &[f64], ) -> Option<Self::Item>
Calculates the statistic from realized profit and loss values.
§Panics
Panics if this method is not implemented for the specific statistic.
Sourcefn calculate_from_orders(
&self,
orders: Vec<Box<dyn Order>>,
) -> Option<Self::Item>
fn calculate_from_orders( &self, orders: Vec<Box<dyn Order>>, ) -> Option<Self::Item>
Calculates the statistic from order data.
§Panics
Panics if this method is not implemented for the specific statistic.
Sourcefn calculate_from_positions(&self, positions: &[Position]) -> Option<Self::Item>
fn calculate_from_positions(&self, positions: &[Position]) -> Option<Self::Item>
Calculates the statistic from position data.
§Panics
Panics if this method is not implemented for the specific statistic.
Sourcefn calculate_from_returns_with_benchmark(
&self,
returns: &Returns,
benchmark: &Returns,
) -> Option<Self::Item>
fn calculate_from_returns_with_benchmark( &self, returns: &Returns, benchmark: &Returns, ) -> Option<Self::Item>
Calculates the statistic from time-indexed strategy returns relative to a benchmark.
Defaults to None; only benchmark-relative statistics (beta, alpha, information
ratio, tracking error, Treynor ratio) override this method. The None default
lets analyzer loops filter results by Option — non-benchmark statistics are
simply skipped, as get_performance_stats_general already does with
calculate_from_positions results — rather than panicking.
Sourcefn align_returns(&self, a: &Returns, b: &Returns) -> (Vec<f64>, Vec<f64>)
fn align_returns(&self, a: &Returns, b: &Returns) -> (Vec<f64>, Vec<f64>)
Aligns two returns series onto a common daily grid.
Both series are first downsampled to daily bins (geometric compounding within each
UTC day), then inner-joined on shared timestamps. Timestamps present in only one
series are dropped (not zero-filled). Returns the aligned (strategy, benchmark)
value vectors, in ascending timestamp order.
Sourcefn check_valid_returns(&self, returns: &Returns) -> bool
fn check_valid_returns(&self, returns: &Returns) -> bool
Validates that returns data is not empty.
Sourcefn downsample_to_daily_bins(&self, returns: &Returns) -> Returns
fn downsample_to_daily_bins(&self, returns: &Returns) -> Returns
Downsamples high-frequency returns to daily bins by geometric compounding.
Within each UTC day, returns are combined via (1 + r1)(1 + r2) - 1 to produce
the day’s effective return, which is the standard convention for chaining
arithmetic period returns. For daily-frequency inputs (one return per day) the
bin value is identical to the input value, so callers that already operate on
daily returns observe no behavior change.
Sourcefn calculate_std(&self, returns: &Returns) -> f64
fn calculate_std(&self, returns: &Returns) -> f64
Calculates the standard deviation of returns with Bessel’s correction.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".