nautilus_analysis/statistics/
winner_min.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 MinWinner {}
25
26impl PortfolioStatistic for MinWinner {
27    type Item = f64;
28
29    fn name(&self) -> String {
30        stringify!(MinWinner).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            .filter(|&&pnl| pnl > 0.0)
41            .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
42            .copied()
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use nautilus_core::approx_eq;
49    use rstest::rstest;
50
51    use super::*;
52
53    #[rstest]
54    fn test_empty_pnls() {
55        let min_winner = MinWinner {};
56        let result = min_winner.calculate_from_realized_pnls(&[]);
57        assert!(result.is_some());
58        assert!(approx_eq!(f64, result.unwrap(), 0.0, epsilon = 1e-9));
59    }
60
61    #[rstest]
62    fn test_no_winning_trades() {
63        let min_winner = MinWinner {};
64        let realized_pnls = vec![-100.0, -50.0, -200.0];
65        let result = min_winner.calculate_from_realized_pnls(&realized_pnls);
66        assert!(result.is_none());
67    }
68
69    #[rstest]
70    fn test_all_winning_trades() {
71        let min_winner = MinWinner {};
72        let realized_pnls = vec![100.0, 50.0, 200.0];
73        let result = min_winner.calculate_from_realized_pnls(&realized_pnls);
74        assert!(result.is_some());
75        assert!(approx_eq!(f64, result.unwrap(), 50.0, epsilon = 1e-9)); // Minimum of 100.0, 50.0, and 200.0 is 50.0
76    }
77
78    #[rstest]
79    fn test_mixed_trades() {
80        let min_winner = MinWinner {};
81        let realized_pnls = vec![100.0, -50.0, 200.0, -100.0];
82        let result = min_winner.calculate_from_realized_pnls(&realized_pnls);
83        assert!(result.is_some());
84        assert!(approx_eq!(f64, result.unwrap(), 100.0, epsilon = 1e-9)); // Minimum of 100.0 and 200.0 is 100.0
85    }
86
87    #[rstest]
88    fn test_single_winning_trade() {
89        let min_winner = MinWinner {};
90        let realized_pnls = vec![50.0];
91        let result = min_winner.calculate_from_realized_pnls(&realized_pnls);
92        assert!(result.is_some());
93        assert!(approx_eq!(f64, result.unwrap(), 50.0, epsilon = 1e-9));
94    }
95
96    #[rstest]
97    fn test_name() {
98        let min_winner = MinWinner {};
99        assert_eq!(min_winner.name(), "MinWinner");
100    }
101}