nautilus_analysis/statistics/
loser_max.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.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 crate::statistic::PortfolioStatistic;
17
18#[repr(C)]
19#[derive(Debug)]
20#[cfg_attr(
21    feature = "python",
22    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.analysis")
23)]
24pub struct MaxLoser {}
25
26impl PortfolioStatistic for MaxLoser {
27    type Item = f64;
28
29    fn name(&self) -> String {
30        stringify!(MaxLoser).to_string()
31    }
32
33    fn calculate_from_realized_pnls(&self, realized_pnls: &[f64]) -> Option<Self::Item> {
34        if realized_pnls.is_empty() {
35            return Some(0.0);
36        }
37
38        realized_pnls
39            .iter()
40            .copied()
41            .filter(|&pnl| pnl < 0.0)
42            .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_empty_pnls() {
52        let max_loser = MaxLoser {};
53        let result = max_loser.calculate_from_realized_pnls(&[]);
54        assert!(result.is_some());
55        assert_eq!(result.unwrap(), 0.0);
56    }
57
58    #[test]
59    fn test_all_positive() {
60        let max_loser = MaxLoser {};
61        let pnls = vec![10.0, 20.0, 30.0];
62        let result = max_loser.calculate_from_realized_pnls(&pnls);
63        assert!(result.is_none());
64    }
65
66    #[test]
67    fn test_all_negative() {
68        let max_loser = MaxLoser {};
69        let pnls = vec![-10.0, -20.0, -30.0];
70        let result = max_loser.calculate_from_realized_pnls(&pnls);
71        assert!(result.is_some());
72        assert_eq!(result.unwrap(), -30.0);
73    }
74
75    #[test]
76    fn test_mixed_pnls() {
77        let max_loser = MaxLoser {};
78        let pnls = vec![10.0, -20.0, 30.0, -40.0];
79        let result = max_loser.calculate_from_realized_pnls(&pnls);
80        assert!(result.is_some());
81        assert_eq!(result.unwrap(), -40.0);
82    }
83
84    #[test]
85    fn test_with_zero() {
86        let max_loser = MaxLoser {};
87        let pnls = vec![10.0, 0.0, -20.0, -30.0];
88        let result = max_loser.calculate_from_realized_pnls(&pnls);
89        assert!(result.is_some());
90        assert_eq!(result.unwrap(), -30.0);
91    }
92
93    #[test]
94    fn test_single_value() {
95        let max_loser = MaxLoser {};
96        let pnls = vec![-10.0];
97        let result = max_loser.calculate_from_realized_pnls(&pnls);
98        assert!(result.is_some());
99        assert_eq!(result.unwrap(), -10.0);
100    }
101
102    #[test]
103    fn test_name() {
104        let max_loser = MaxLoser {};
105        assert_eq!(max_loser.name(), "MaxLoser");
106    }
107}