#[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, 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, 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 all previous state before calculating, so can be called multiple times without accumulating stale data.
Sourcepub fn add_positions(&mut self, positions: &[Position])
pub fn add_positions(&mut self, positions: &[Position])
Adds new positions for analysis.
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 realized_pnls(
&self,
currency: Option<&Currency>,
) -> Option<Vec<(PositionId, f64)>>
pub fn realized_pnls( &self, currency: Option<&Currency>, ) -> Option<Vec<(PositionId, f64)>>
Retrieves realized PnLs for a specific currency.
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_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.
Source§impl<'py> IntoPyObject<'py> for PortfolioAnalyzer
impl<'py> IntoPyObject<'py> for PortfolioAnalyzer
Source§type Target = PortfolioAnalyzer
type Target = PortfolioAnalyzer
Source§type Output = Bound<'py, <PortfolioAnalyzer as IntoPyObject<'py>>::Target>
type Output = Bound<'py, <PortfolioAnalyzer as IntoPyObject<'py>>::Target>
Source§fn into_pyobject(
self,
py: Python<'py>,
) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
fn into_pyobject( self, py: Python<'py>, ) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
Source§impl PyClass for PortfolioAnalyzer
impl PyClass for PortfolioAnalyzer
Source§impl PyClassImpl for PortfolioAnalyzer
impl PyClassImpl for PortfolioAnalyzer
Source§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
Source§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
Source§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
Source§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
Source§const IS_IMMUTABLE_TYPE: bool = false
const IS_IMMUTABLE_TYPE: bool = false
Source§const RAW_DOC: &'static CStr = /// 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.
const RAW_DOC: &'static CStr = /// 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.
Source§const DOC: &'static CStr
const DOC: &'static CStr
text_signature if a constructor is defined. Read moreSource§type Layout = <<PortfolioAnalyzer as PyClassImpl>::BaseNativeType as PyClassBaseType>::Layout<PortfolioAnalyzer>
type Layout = <<PortfolioAnalyzer as PyClassImpl>::BaseNativeType as PyClassBaseType>::Layout<PortfolioAnalyzer>
Source§type ThreadChecker = NoopThreadChecker
type ThreadChecker = NoopThreadChecker
type Inventory = Pyo3MethodsInventoryForPortfolioAnalyzer
Source§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
Source§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny by default, and when you declare
#[pyclass(extends=PyDict)], it’s PyDict.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
§fn dict_offset() -> Option<PyObjectOffset>
fn dict_offset() -> Option<PyObjectOffset>
§fn weaklist_offset() -> Option<PyObjectOffset>
fn weaklist_offset() -> Option<PyObjectOffset>
Source§impl PyClassNewTextSignature for PortfolioAnalyzer
impl PyClassNewTextSignature for PortfolioAnalyzer
const TEXT_SIGNATURE: &'static str = "()"
Source§impl PyStubType for PortfolioAnalyzer
impl PyStubType for PortfolioAnalyzer
Source§fn type_output() -> TypeInfo
fn type_output() -> TypeInfo
§fn type_input() -> TypeInfo
fn type_input() -> TypeInfo
Source§impl PyTypeInfo for PortfolioAnalyzer
impl PyTypeInfo for PortfolioAnalyzer
Source§const NAME: &str = <Self as ::pyo3::PyClass>::NAME
const NAME: &str = <Self as ::pyo3::PyClass>::NAME
prefer using ::type_object(py).name() to get the correct runtime value
Source§const MODULE: Option<&str> = <Self as ::pyo3::impl_::pyclass::PyClassImpl>::MODULE
const MODULE: Option<&str> = <Self as ::pyo3::impl_::pyclass::PyClassImpl>::MODULE
prefer using ::type_object(py).module() to get the correct runtime value
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
§fn is_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type or a subclass of this type.§fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type.impl DerefToPyAny for PortfolioAnalyzer
impl ExtractPyClassWithClone for PortfolioAnalyzer
Auto Trait Implementations§
impl Freeze for PortfolioAnalyzer
impl !RefUnwindSafe for PortfolioAnalyzer
impl Send for PortfolioAnalyzer
impl Sync for PortfolioAnalyzer
impl Unpin for PortfolioAnalyzer
impl UnsafeUnpin for PortfolioAnalyzer
impl !UnwindSafe for PortfolioAnalyzer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
§fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
self into an owned Python object, dropping type information.§fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
self into an owned Python object, dropping type information and unbinding it
from the 'py lifetime.§fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
self into a Python object. Read more§impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
§fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>
fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>
§impl<T> PyErrArguments for T
impl<T> PyErrArguments for T
§impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
§const NAME: &'static str = T::NAME
const NAME: &'static str = T::NAME
Use ::classinfo_object() instead and format the type name at runtime. Note that using built-in cast features is often better than manual PyTypeCheck usage.
§fn type_check(object: &Bound<'_, PyAny>) -> bool
fn type_check(object: &Bound<'_, PyAny>) -> bool
§fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
isinstance and issubclass function. Read more