Skip to main content

nautilus_analysis/python/statistics/
omega_ratio.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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::collections::BTreeMap;
17
18use nautilus_core::python::to_pyvalue_err;
19use nautilus_model::position::Position;
20use pyo3::prelude::*;
21
22use super::transform_returns;
23use crate::{statistic::PortfolioStatistic, statistics::omega_ratio::OmegaRatio};
24
25#[pymethods]
26#[pyo3_stub_gen::derive::gen_stub_pymethods]
27impl OmegaRatio {
28    /// Calculates the Omega ratio of portfolio returns.
29    ///
30    /// The Omega ratio is the ratio of probability-weighted gains to losses relative
31    /// to a return threshold `θ`. It captures the entire return distribution (all
32    /// moments), unlike the Sharpe ratio which only uses the first two:
33    ///
34    /// `Omega(θ) = sum(max(r - θ, 0)) / sum(max(θ - r, 0))`
35    ///
36    /// The threshold `θ` defaults to `0` (gains vs losses about zero). A value above
37    /// `1` means gains above the threshold outweigh losses below it. Returns `NaN`
38    /// for an empty series, or when there are no returns below the threshold (the
39    /// ratio is undefined).
40    ///
41    /// # References
42    ///
43    /// - Keating, C., & Shadwick, W. F. (2002). "A Universal Performance Measure".
44    ///   *Journal of Performance Measurement*, 6(3), 59-84.
45    #[new]
46    #[pyo3(signature = (threshold=None))]
47    fn py_new(threshold: Option<f64>) -> PyResult<Self> {
48        Self::new_checked(threshold).map_err(to_pyvalue_err)
49    }
50
51    fn __repr__(&self) -> String {
52        self.to_string()
53    }
54
55    #[getter]
56    #[pyo3(name = "name")]
57    fn py_name(&self) -> String {
58        self.name()
59    }
60
61    #[pyo3(name = "calculate_from_returns")]
62    #[expect(clippy::needless_pass_by_value)]
63    fn py_calculate_from_returns(&mut self, raw_returns: BTreeMap<u64, f64>) -> Option<f64> {
64        self.calculate_from_returns(&transform_returns(&raw_returns))
65    }
66
67    #[pyo3(name = "calculate_from_realized_pnls")]
68    fn py_calculate_from_realized_pnls(&mut self, _realized_pnls: Vec<f64>) -> Option<f64> {
69        None
70    }
71
72    #[pyo3(name = "calculate_from_positions")]
73    fn py_calculate_from_positions(&mut self, _positions: Vec<Position>) -> Option<f64> {
74        None
75    }
76}