Skip to main content

nautilus_analysis/statistics/
max_drawdown.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
16//! Maximum Drawdown statistic.
17
18use std::collections::BTreeMap;
19
20use nautilus_core::UnixNanos;
21
22use crate::statistic::PortfolioStatistic;
23
24/// Calculates the Maximum Drawdown for returns.
25///
26/// Maximum Drawdown is the maximum observed loss from a peak to a trough,
27/// before a new peak is attained. It is an indicator of downside risk over
28/// a specified time period.
29///
30/// Formula: Max((Peak - Trough) / Peak) for all peak-trough sequences
31#[repr(C)]
32#[derive(Debug, Clone, Default)]
33#[cfg_attr(
34    feature = "python",
35    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.analysis", from_py_object)
36)]
37#[cfg_attr(
38    feature = "python",
39    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.analysis")
40)]
41pub struct MaxDrawdown {}
42
43impl MaxDrawdown {
44    /// Creates a new [`MaxDrawdown`] instance.
45    #[must_use]
46    pub fn new() -> Self {
47        Self {}
48    }
49}
50
51impl PortfolioStatistic for MaxDrawdown {
52    type Item = f64;
53
54    fn name(&self) -> String {
55        "Max Drawdown".to_string()
56    }
57
58    fn calculate_from_returns(&self, returns: &BTreeMap<UnixNanos, f64>) -> Option<Self::Item> {
59        if returns.is_empty() {
60            return Some(0.0);
61        }
62
63        // Calculate cumulative returns starting from 1.0
64        let mut cumulative = 1.0;
65        let mut running_max = 1.0;
66        let mut max_drawdown = 0.0;
67
68        for &ret in returns.values() {
69            cumulative *= 1.0 + ret;
70
71            // Update running maximum
72            if cumulative > running_max {
73                running_max = cumulative;
74            }
75
76            // Calculate drawdown from running max
77            let drawdown = (running_max - cumulative) / running_max;
78
79            // Update maximum drawdown
80            if drawdown > max_drawdown {
81                max_drawdown = drawdown;
82            }
83        }
84
85        // Return as negative percentage
86        Some(-max_drawdown)
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use rstest::rstest;
93
94    use super::*;
95
96    fn create_returns(values: &[f64]) -> BTreeMap<UnixNanos, f64> {
97        values
98            .iter()
99            .copied()
100            .enumerate()
101            .map(|(i, v)| (UnixNanos::from(i as u64), v))
102            .collect()
103    }
104
105    #[rstest]
106    fn test_name() {
107        let stat = MaxDrawdown::new();
108        assert_eq!(stat.name(), "Max Drawdown");
109    }
110
111    #[rstest]
112    fn test_empty_returns() {
113        let stat = MaxDrawdown::new();
114        let returns = BTreeMap::new();
115        let result = stat.calculate_from_returns(&returns);
116        assert_eq!(result, Some(0.0));
117    }
118
119    #[rstest]
120    fn test_no_drawdown() {
121        let stat = MaxDrawdown::new();
122        // Only positive returns, no drawdown
123        let returns = create_returns(&[0.01, 0.02, 0.01, 0.015]);
124        let result = stat.calculate_from_returns(&returns).unwrap();
125        assert_eq!(result, 0.0);
126    }
127
128    #[rstest]
129    fn test_simple_drawdown() {
130        let stat = MaxDrawdown::new();
131        // Start at 1.0, go to 1.1 (+10%), then drop to 0.99 (-10% from peak)
132        // Max DD = (1.1 - 0.99) / 1.1 = 0.1 / 1.1 = 0.0909 (9.09%)
133        let returns = create_returns(&[0.10, -0.10]);
134        let result = stat.calculate_from_returns(&returns).unwrap();
135
136        // Should be approximately -0.10 (reported as negative)
137        assert!((result + 0.10).abs() < 0.01);
138    }
139
140    #[rstest]
141    fn test_multiple_drawdowns() {
142        let stat = MaxDrawdown::new();
143        // Peak at 1.5, trough at 1.0
144        // DD1: 10% from 1.0
145        // DD2: 20% from 1.5
146        let returns = create_returns(&[0.10, -0.10, 0.50, -0.20, 0.10]);
147        let result = stat.calculate_from_returns(&returns).unwrap();
148
149        // Max DD should be the larger one (20%)
150        assert!((result + 0.20).abs() < 0.01);
151    }
152
153    #[rstest]
154    fn test_initial_loss() {
155        let stat = MaxDrawdown::new();
156        // Start with 40% loss
157        let returns = create_returns(&[-0.40, -0.10]);
158        let result = stat.calculate_from_returns(&returns).unwrap();
159
160        // From 1.0 -> 0.6 -> 0.54
161        // Max DD from initial 1.0 is 46%
162        assert!((result + 0.46).abs() < 0.01);
163    }
164}