nautilus_analysis/statistics/
winner_max.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 crate::statistic::PortfolioStatistic;
17
18#[repr(C)]
19#[derive(Debug)]
20#[cfg_attr(
21    feature = "python",
22    pyo3::pyclass(module = "posei_trader.core.nautilus_pyo3.analysis")
23)]
24pub struct MaxWinner {}
25
26impl PortfolioStatistic for MaxWinner {
27    type Item = f64;
28
29    fn name(&self) -> String {
30        stringify!(MaxWinner).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            .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use rstest::rstest;
49
50    use super::*;
51
52    #[rstest]
53    fn test_empty_pnls() {
54        let max_winner = MaxWinner {};
55        let result = max_winner.calculate_from_realized_pnls(&[]);
56        assert!(result.is_some());
57        assert_eq!(result.unwrap(), 0.0);
58    }
59
60    #[rstest]
61    fn test_no_winning_trades() {
62        let max_winner = MaxWinner {};
63        let realized_pnls = vec![-100.0, -50.0, -200.0];
64        let result = max_winner.calculate_from_realized_pnls(&realized_pnls);
65        assert!(result.is_none());
66    }
67
68    #[rstest]
69    fn test_all_winning_trades() {
70        let max_winner = MaxWinner {};
71        let realized_pnls = vec![100.0, 50.0, 200.0];
72        let result = max_winner.calculate_from_realized_pnls(&realized_pnls);
73        assert!(result.is_some());
74        assert_eq!(result.unwrap(), 200.0);
75    }
76
77    #[rstest]
78    fn test_mixed_trades() {
79        let max_winner = MaxWinner {};
80        let realized_pnls = vec![100.0, -50.0, 200.0, -100.0];
81        let result = max_winner.calculate_from_realized_pnls(&realized_pnls);
82        assert!(result.is_some());
83        assert_eq!(result.unwrap(), 200.0);
84    }
85
86    #[rstest]
87    fn test_name() {
88        let max_winner = MaxWinner {};
89        assert_eq!(max_winner.name(), "MaxWinner");
90    }
91}