Skip to main content

nautilus_analysis/python/statistics/
treynor_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_model::position::Position;
19use pyo3::prelude::*;
20
21use super::transform_returns;
22use crate::{statistic::PortfolioStatistic, statistics::treynor_ratio::TreynorRatio};
23
24#[pymethods]
25#[pyo3_stub_gen::derive::gen_stub_pymethods]
26impl TreynorRatio {
27    /// Calculates the Treynor ratio of portfolio returns relative to a benchmark.
28    ///
29    /// The Treynor ratio measures excess return per unit of systematic risk (beta):
30    ///
31    /// `Treynor = (annualized_return - rf_annual) / beta`
32    ///
33    /// The portfolio's annualized return is computed geometrically (CAGR-style) from the
34    /// aligned returns: `annualized_return = (prod(1 + r_i))^(period / n) - 1`. The
35    /// per-period risk-free rate is annualized geometrically as
36    /// `rf_annual = (1 + rf)^period - 1`. Beta is the sample (`ddof = 1`) beta of the
37    /// portfolio against the benchmark. The period defaults to 252 trading days and `rf`
38    /// defaults to 0.0.
39    ///
40    /// # References
41    ///
42    /// - Treynor, J. L. (1965). "How to Rate Management of Investment Funds".
43    ///   *Harvard Business Review*, 43(1), 63-75.
44    /// - CFA Institute Investment Foundations, 3rd Edition
45    #[new]
46    #[pyo3(signature = (period=None, risk_free_rate=None))]
47    fn py_new(period: Option<usize>, risk_free_rate: Option<f64>) -> Self {
48        Self::new(period, risk_free_rate)
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    fn py_calculate_from_returns(&self, _returns: BTreeMap<u64, f64>) -> Option<f64> {
63        None
64    }
65
66    #[pyo3(name = "calculate_from_realized_pnls")]
67    fn py_calculate_from_realized_pnls(&self, _realized_pnls: Vec<f64>) -> Option<f64> {
68        None
69    }
70
71    #[pyo3(name = "calculate_from_positions")]
72    fn py_calculate_from_positions(&self, _positions: Vec<Position>) -> Option<f64> {
73        None
74    }
75
76    #[pyo3(name = "calculate_from_returns_with_benchmark")]
77    #[expect(clippy::needless_pass_by_value)]
78    fn py_calculate_from_returns_with_benchmark(
79        &self,
80        returns: BTreeMap<u64, f64>,
81        benchmark: BTreeMap<u64, f64>,
82    ) -> Option<f64> {
83        self.calculate_from_returns_with_benchmark(
84            &transform_returns(&returns),
85            &transform_returns(&benchmark),
86        )
87    }
88}