diff --git a/src/lib.rs b/src/lib.rs index 5665480..89a7388 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ pub mod correlation; pub mod momentum; +pub mod performance; pub mod smooth; pub mod statistic; pub mod trend; diff --git a/src/performance.rs b/src/performance.rs new file mode 100644 index 0000000..7cd7e38 --- /dev/null +++ b/src/performance.rs @@ -0,0 +1,18 @@ +use itertools::izip; + +use crate::smooth; + +/// Drawdown +/// +/// Measures an investment or trading account's decline from the peak before it recovers back +/// to that peak. +/// +/// ## Sources +/// +/// [[1]](https://www.investopedia.com/terms/d/drawdown.asp) +pub fn drawdown(data: &[f64]) -> impl Iterator + '_ { + data.iter().scan(data[0], |state, &x| { + *state = state.max(x); + Some(1.0 - (x / *state)) + }) +}