nautilus_analysis/statistic.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3// https://poseitrader.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::{collections::BTreeMap, fmt::Debug};
17
18use nautilus_model::{orders::Order, position::Position};
19
20use crate::Returns;
21
22const IMPL_ERR: &str = "is not implemented for";
23
24/// Trait for portfolio performance statistics that can be calculated from different data sources.
25///
26/// This trait provides a flexible framework for implementing various financial performance
27/// metrics that can operate on returns, realized PnLs, orders, or positions data.
28/// Each statistic implementation should override the relevant calculation methods.
29#[allow(unused_variables)]
30pub trait PortfolioStatistic: Debug {
31 type Item;
32
33 /// Returns the name of this statistic for display and identification purposes.
34 fn name(&self) -> String;
35
36 /// Calculates the statistic from time-indexed returns data.
37 ///
38 /// # Panics
39 ///
40 /// Panics if this method is not implemented for the specific statistic.
41 fn calculate_from_returns(&self, returns: &Returns) -> Option<Self::Item> {
42 panic!("`calculate_from_returns` {IMPL_ERR} `{}`", self.name());
43 }
44
45 /// Calculates the statistic from realized profit and loss values.
46 ///
47 /// # Panics
48 ///
49 /// Panics if this method is not implemented for the specific statistic.
50 fn calculate_from_realized_pnls(&self, realized_pnls: &[f64]) -> Option<Self::Item> {
51 panic!(
52 "`calculate_from_realized_pnls` {IMPL_ERR} `{}`",
53 self.name()
54 );
55 }
56
57 /// Calculates the statistic from order data.
58 ///
59 /// # Panics
60 ///
61 /// Panics if this method is not implemented for the specific statistic.
62 #[allow(dead_code)]
63 fn calculate_from_orders(&self, orders: Vec<Box<dyn Order>>) -> Option<Self::Item> {
64 panic!("`calculate_from_orders` {IMPL_ERR} `{}`", self.name());
65 }
66
67 /// Calculates the statistic from position data.
68 ///
69 /// # Panics
70 ///
71 /// Panics if this method is not implemented for the specific statistic.
72 fn calculate_from_positions(&self, positions: &[Position]) -> Option<Self::Item> {
73 panic!("`calculate_from_positions` {IMPL_ERR} `{}`", self.name());
74 }
75
76 /// Validates that returns data is not empty.
77 fn check_valid_returns(&self, returns: &Returns) -> bool {
78 !returns.is_empty()
79 }
80
81 /// Downsamples high-frequency returns to daily bins for daily statistics calculation.
82 fn downsample_to_daily_bins(&self, returns: &Returns) -> Returns {
83 let nanos_per_day = 86_400_000_000_000; // Number of nanoseconds in a day
84 let mut daily_bins = BTreeMap::new();
85
86 for (×tamp, &value) in returns {
87 // Calculate the start of the day in nanoseconds for the given timestamp
88 let day_start = timestamp - (timestamp.as_u64() % nanos_per_day);
89
90 // Sum returns for each day
91 *daily_bins.entry(day_start).or_insert(0.0) += value;
92 }
93
94 daily_bins
95 }
96
97 /// Calculates the standard deviation of returns with Bessel's correction.
98 fn calculate_std(&self, returns: &Returns) -> f64 {
99 let n = returns.len() as f64;
100 if n < 2.0 {
101 return f64::NAN;
102 }
103
104 let mean = returns.values().sum::<f64>() / n;
105
106 let variance = returns.values().map(|x| (x - mean).powi(2)).sum::<f64>() / (n - 1.0);
107
108 variance.sqrt()
109 }
110}