nautilus_analysis/statistics/
sortino_ratio.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 std::fmt::{self, Display};
17
18use nautilus_model::position::Position;
19
20use crate::{Returns, statistic::PortfolioStatistic};
21
22/// Calculates the Sortino ratio for portfolio returns.
23///
24/// The Sortino ratio is a variation of the Sharpe ratio that only penalizes downside
25/// volatility, making it more appropriate for strategies with asymmetric return distributions.
26///
27/// Formula: `Mean Return / Downside Deviation * sqrt(period)`
28///
29/// Where downside deviation is calculated as:
30/// `sqrt(sum(negative_returns^2) / total_observations)`
31///
32/// Note: Uses total observations count (not just negative returns) as per Sortino's methodology.
33///
34/// # References
35///
36/// - Sortino, F. A., & van der Meer, R. (1991). "Downside Risk". *Journal of Portfolio Management*, 17(4), 27-31.
37/// - Sortino, F. A., & Price, L. N. (1994). "Performance Measurement in a Downside Risk Framework".
38///   *Journal of Investing*, 3(3), 59-64.
39#[repr(C)]
40#[derive(Debug, Clone)]
41#[cfg_attr(
42    feature = "python",
43    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.analysis")
44)]
45pub struct SortinoRatio {
46    period: usize,
47}
48
49impl SortinoRatio {
50    /// Creates a new [`SortinoRatio`] instance.
51    #[must_use]
52    pub fn new(period: Option<usize>) -> Self {
53        Self {
54            period: period.unwrap_or(252),
55        }
56    }
57}
58
59impl Display for SortinoRatio {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        write!(f, "Sortino Ratio ({} days)", self.period)
62    }
63}
64
65impl PortfolioStatistic for SortinoRatio {
66    type Item = f64;
67
68    fn name(&self) -> String {
69        self.to_string()
70    }
71
72    fn calculate_from_returns(&self, raw_returns: &Returns) -> Option<Self::Item> {
73        if !self.check_valid_returns(raw_returns) {
74            return Some(f64::NAN);
75        }
76
77        let returns = self.downsample_to_daily_bins(raw_returns);
78        let total_n = returns.len() as f64;
79        let mean = returns.values().sum::<f64>() / total_n;
80
81        let downside = (returns
82            .values()
83            .filter(|&&x| x < 0.0)
84            .map(|x| x.powi(2))
85            .sum::<f64>()
86            / total_n)
87            .sqrt();
88
89        if downside < f64::EPSILON {
90            return Some(f64::NAN);
91        }
92
93        let annualized_ratio = (mean / downside) * (self.period as f64).sqrt();
94
95        Some(annualized_ratio)
96    }
97    fn calculate_from_realized_pnls(&self, _realized_pnls: &[f64]) -> Option<Self::Item> {
98        None
99    }
100
101    fn calculate_from_positions(&self, _positions: &[Position]) -> Option<Self::Item> {
102        None
103    }
104}
105
106////////////////////////////////////////////////////////////////////////////////
107// Tests
108////////////////////////////////////////////////////////////////////////////////
109
110#[cfg(test)]
111mod tests {
112    use std::collections::BTreeMap;
113
114    use nautilus_core::{UnixNanos, approx_eq};
115    use rstest::rstest;
116
117    use super::*;
118
119    fn create_returns(values: Vec<f64>) -> BTreeMap<UnixNanos, f64> {
120        let mut new_return = BTreeMap::new();
121        let one_day_in_nanos = 86_400_000_000_000;
122        let start_time = 1_600_000_000_000_000_000;
123
124        for (i, &value) in values.iter().enumerate() {
125            let timestamp = start_time + i as u64 * one_day_in_nanos;
126            new_return.insert(UnixNanos::from(timestamp), value);
127        }
128
129        new_return
130    }
131
132    #[rstest]
133    fn test_empty_returns() {
134        let ratio = SortinoRatio::new(None);
135        let returns = create_returns(vec![]);
136        let result = ratio.calculate_from_returns(&returns);
137        assert!(result.is_some());
138        assert!(result.unwrap().is_nan());
139    }
140
141    #[rstest]
142    fn test_zero_downside_deviation() {
143        let ratio = SortinoRatio::new(None);
144        let returns = create_returns(vec![0.02, 0.03, 0.01]);
145        let result = ratio.calculate_from_returns(&returns);
146        assert!(result.is_some());
147        assert!(result.unwrap().is_nan());
148    }
149
150    #[rstest]
151    fn test_valid_sortino_ratio() {
152        let ratio = SortinoRatio::new(Some(252));
153        let returns = create_returns(vec![-0.01, 0.02, -0.015, 0.005, -0.02]);
154        let result = ratio.calculate_from_returns(&returns);
155        assert!(result.is_some());
156        assert!(approx_eq!(
157            f64,
158            result.unwrap(),
159            -5.273224492824493,
160            epsilon = 1e-9
161        ));
162    }
163
164    #[rstest]
165    fn test_name() {
166        let ratio = SortinoRatio::new(None);
167        assert_eq!(ratio.name(), "Sortino Ratio (252 days)");
168    }
169}