nautilus_analysis/statistics/
calmar_ratio.rs1use std::collections::BTreeMap;
19
20use nautilus_core::UnixNanos;
21
22use crate::{
23 statistic::PortfolioStatistic,
24 statistics::{cagr::CAGR, max_drawdown::MaxDrawdown},
25};
26
27#[repr(C)]
37#[derive(Debug, Clone)]
38#[cfg_attr(
39 feature = "python",
40 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.analysis")
41)]
42pub struct CalmarRatio {
43 pub period: usize,
45}
46
47impl CalmarRatio {
48 #[must_use]
50 pub fn new(period: Option<usize>) -> Self {
51 Self {
52 period: period.unwrap_or(252),
53 }
54 }
55}
56
57impl PortfolioStatistic for CalmarRatio {
58 type Item = f64;
59
60 fn name(&self) -> String {
61 format!("Calmar Ratio ({} days)", self.period)
62 }
63
64 fn calculate_from_returns(&self, returns: &BTreeMap<UnixNanos, f64>) -> Option<Self::Item> {
65 if returns.is_empty() {
66 return Some(f64::NAN);
67 }
68
69 let cagr_stat = CAGR::new(Some(self.period));
71 let cagr = cagr_stat.calculate_from_returns(returns)?;
72
73 let max_dd_stat = MaxDrawdown::new();
75 let max_dd = max_dd_stat.calculate_from_returns(returns)?;
76
77 if max_dd.abs() < f64::EPSILON {
81 return Some(f64::NAN);
82 }
83
84 let calmar = cagr / max_dd.abs();
85
86 if calmar.is_finite() {
87 Some(calmar)
88 } else {
89 Some(f64::NAN)
90 }
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use rstest::rstest;
97
98 use super::*;
99
100 fn create_returns(values: Vec<f64>) -> BTreeMap<UnixNanos, f64> {
101 let mut returns = BTreeMap::new();
102 let nanos_per_day = 86_400_000_000_000;
103 let start_time = 1_600_000_000_000_000_000;
104
105 for (i, &value) in values.iter().enumerate() {
106 let timestamp = start_time + i as u64 * nanos_per_day;
107 returns.insert(UnixNanos::from(timestamp), value);
108 }
109
110 returns
111 }
112
113 #[rstest]
114 fn test_name() {
115 let ratio = CalmarRatio::new(Some(252));
116 assert_eq!(ratio.name(), "Calmar Ratio (252 days)");
117 }
118
119 #[rstest]
120 fn test_empty_returns() {
121 let ratio = CalmarRatio::new(Some(252));
122 let returns = BTreeMap::new();
123 let result = ratio.calculate_from_returns(&returns);
124 assert!(result.is_some());
125 assert!(result.unwrap().is_nan());
126 }
127
128 #[rstest]
129 fn test_no_drawdown() {
130 let ratio = CalmarRatio::new(Some(252));
131 let returns = create_returns(vec![0.01; 252]);
133 let result = ratio.calculate_from_returns(&returns);
134
135 assert!(result.is_some());
137 assert!(result.unwrap().is_nan());
138 }
139
140 #[rstest]
141 fn test_positive_ratio() {
142 let ratio = CalmarRatio::new(Some(252));
143 let mut returns_vec = vec![0.001; 200]; returns_vec.extend(vec![-0.002; 52]); let returns = create_returns(returns_vec);
150 let result = ratio.calculate_from_returns(&returns).unwrap();
151
152 assert!(result > 0.0);
154 }
155
156 #[rstest]
157 fn test_high_calmar_better() {
158 let ratio = CalmarRatio::new(Some(252));
159
160 let returns_a = create_returns(vec![0.002; 252]);
162 let calmar_a = ratio.calculate_from_returns(&returns_a);
163
164 let returns_b = create_returns(vec![0.001; 252]);
166 let calmar_b = ratio.calculate_from_returns(&returns_b);
167
168 assert!(calmar_a.is_some());
171 assert!(calmar_b.is_some());
172 }
173}