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(0.0);
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 {
80 return Some(0.0);
81 }
82
83 let calmar = cagr / max_dd.abs();
84
85 if calmar.is_finite() {
86 Some(calmar)
87 } else {
88 Some(0.0)
89 }
90 }
91}
92
93#[cfg(test)]
98mod tests {
99 use rstest::rstest;
100
101 use super::*;
102
103 fn create_returns(values: Vec<f64>) -> BTreeMap<UnixNanos, f64> {
104 let mut returns = BTreeMap::new();
105 let nanos_per_day = 86_400_000_000_000;
106 let start_time = 1_600_000_000_000_000_000;
107
108 for (i, &value) in values.iter().enumerate() {
109 let timestamp = start_time + i as u64 * nanos_per_day;
110 returns.insert(UnixNanos::from(timestamp), value);
111 }
112
113 returns
114 }
115
116 #[rstest]
117 fn test_name() {
118 let ratio = CalmarRatio::new(Some(252));
119 assert_eq!(ratio.name(), "Calmar Ratio (252 days)");
120 }
121
122 #[rstest]
123 fn test_empty_returns() {
124 let ratio = CalmarRatio::new(Some(252));
125 let returns = BTreeMap::new();
126 let result = ratio.calculate_from_returns(&returns);
127 assert_eq!(result, Some(0.0));
128 }
129
130 #[rstest]
131 fn test_no_drawdown() {
132 let ratio = CalmarRatio::new(Some(252));
133 let returns = create_returns(vec![0.01; 252]);
135 let result = ratio.calculate_from_returns(&returns);
136
137 assert_eq!(result, Some(0.0));
139 }
140
141 #[rstest]
142 fn test_positive_ratio() {
143 let ratio = CalmarRatio::new(Some(252));
144 let mut returns_vec = vec![0.001; 200]; returns_vec.extend(vec![-0.002; 52]); let returns = create_returns(returns_vec);
151 let result = ratio.calculate_from_returns(&returns).unwrap();
152
153 assert!(result > 0.0);
155 }
156
157 #[rstest]
158 fn test_high_calmar_better() {
159 let ratio = CalmarRatio::new(Some(252));
160
161 let returns_a = create_returns(vec![0.002; 252]);
163 let calmar_a = ratio.calculate_from_returns(&returns_a);
164
165 let returns_b = create_returns(vec![0.001; 252]);
167 let calmar_b = ratio.calculate_from_returns(&returns_b);
168
169 assert!(calmar_a.is_some());
172 assert!(calmar_b.is_some());
173 }
174}