nautilus_analysis/statistics/
winner_avg.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 AvgWinner {}
25
26impl PortfolioStatistic for AvgWinner {
27    type Item = f64;
28
29    fn name(&self) -> String {
30        stringify!(AvgWinner).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        let winners: Vec<f64> = realized_pnls
39            .iter()
40            .filter(|&&pnl| pnl > 0.0)
41            .copied()
42            .collect();
43
44        if winners.is_empty() {
45            return Some(0.0);
46        }
47
48        let sum: f64 = winners.iter().sum();
49        Some(sum / winners.len() as f64)
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_empty_pnls() {
59        let avg_winner = AvgWinner {};
60        let result = avg_winner.calculate_from_realized_pnls(&[]);
61        assert!(result.is_some());
62        assert_eq!(result.unwrap(), 0.0);
63    }
64
65    #[test]
66    fn test_no_winning_trades() {
67        let avg_winner = AvgWinner {};
68        let realized_pnls = vec![-100.0, -50.0, -200.0];
69        let result = avg_winner.calculate_from_realized_pnls(&realized_pnls);
70        assert!(result.is_some());
71        assert_eq!(result.unwrap(), 0.0);
72    }
73
74    #[test]
75    fn test_all_winning_trades() {
76        let avg_winner = AvgWinner {};
77        let realized_pnls = vec![100.0, 50.0, 200.0];
78        let result = avg_winner.calculate_from_realized_pnls(&realized_pnls);
79        assert!(result.is_some());
80        assert_eq!(result.unwrap(), 116.66666666666667);
81    }
82
83    #[test]
84    fn test_mixed_trades() {
85        let avg_winner = AvgWinner {};
86        let realized_pnls = vec![100.0, -50.0, 200.0, -100.0];
87        let result = avg_winner.calculate_from_realized_pnls(&realized_pnls);
88        assert!(result.is_some());
89        assert_eq!(result.unwrap(), 150.0);
90    }
91
92    #[test]
93    fn test_name() {
94        let avg_winner = AvgWinner {};
95        assert_eq!(avg_winner.name(), "AvgWinner");
96    }
97}