1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! Rolling Sharpe Ratio.
use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::traits::Indicator;
/// Rolling Sharpe Ratio over `period` period-returns.
///
/// The input is treated as a single period-return (e.g. one day's percentage
/// return). Over the trailing window of `period` returns the indicator
/// computes:
///
/// ```text
/// Sharpe = (mean(returns) − risk_free_per_period) / stddev(returns)
/// ```
///
/// `stddev` is the sample standard deviation with `n − 1` in the denominator.
/// `risk_free_per_period` is the per-period risk-free rate the caller supplies
/// (e.g. `0.0` for excess-of-zero or a daily-equivalent rate to match the
/// return frequency). Wickra does not annualise: feed already-annualised
/// returns and supply an annual risk-free rate if you want an annualised
/// Sharpe.
///
/// A flat window has zero standard deviation and Sharpe is undefined; the
/// indicator returns `0.0` in that case rather than producing `NaN`.
///
/// Each `update` is O(1) — Welford-style running sums maintain `Σr`, `Σr²`
/// as the window slides.
///
/// # Example
///
/// ```
/// use wickra_core::{Indicator, SharpeRatio};
///
/// let mut sr = SharpeRatio::new(20, 0.0).unwrap();
/// let mut last = None;
/// for i in 0..40 {
/// last = sr.update(0.001 + (f64::from(i) * 0.1).sin() * 0.01);
/// }
/// assert!(last.is_some());
/// ```
#[derive(Debug, Clone)]
pub struct SharpeRatio {
period: usize,
risk_free: f64,
window: VecDeque<f64>,
sum: f64,
sum_sq: f64,
}
impl SharpeRatio {
/// Construct a new rolling Sharpe Ratio with the given window and
/// per-period risk-free rate.
///
/// # Errors
/// Returns [`Error::InvalidPeriod`] if `period < 2` (sample standard
/// deviation needs at least two observations).
pub fn new(period: usize, risk_free: f64) -> Result<Self> {
if period < 2 {
return Err(Error::InvalidPeriod {
message: "sharpe ratio needs period >= 2",
});
}
Ok(Self {
period,
risk_free,
window: VecDeque::with_capacity(period),
sum: 0.0,
sum_sq: 0.0,
})
}
/// Configured window length.
pub const fn period(&self) -> usize {
self.period
}
/// Configured per-period risk-free rate.
pub const fn risk_free(&self) -> f64 {
self.risk_free
}
}
impl Indicator for SharpeRatio {
type Input = f64;
type Output = f64;
fn update(&mut self, input: f64) -> Option<f64> {
if !input.is_finite() {
return None;
}
if self.window.len() == self.period {
let old = self.window.pop_front().expect("non-empty");
self.sum -= old;
self.sum_sq -= old * old;
}
self.window.push_back(input);
self.sum += input;
self.sum_sq += input * input;
if self.window.len() < self.period {
return None;
}
let n = self.period as f64;
let mean = self.sum / n;
// Sample variance with Bessel's correction.
let var = (self.sum_sq - n * mean * mean).max(0.0) / (n - 1.0);
let sd = var.sqrt();
if sd == 0.0 {
return Some(0.0);
}
Some((mean - self.risk_free) / sd)
}
fn reset(&mut self) {
self.window.clear();
self.sum = 0.0;
self.sum_sq = 0.0;
}
fn warmup_period(&self) -> usize {
self.period
}
fn is_ready(&self) -> bool {
self.window.len() == self.period
}
fn name(&self) -> &'static str {
"SharpeRatio"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
#[test]
fn rejects_period_less_than_two() {
assert!(matches!(
SharpeRatio::new(1, 0.0),
Err(Error::InvalidPeriod { .. })
));
assert!(matches!(
SharpeRatio::new(0, 0.0),
Err(Error::InvalidPeriod { .. })
));
}
#[test]
fn accessors_and_metadata() {
let sr = SharpeRatio::new(20, 0.001).unwrap();
assert_eq!(sr.period(), 20);
assert_relative_eq!(sr.risk_free(), 0.001, epsilon = 1e-12);
assert_eq!(sr.name(), "SharpeRatio");
assert_eq!(sr.warmup_period(), 20);
}
#[test]
fn constant_returns_yield_zero() {
let mut sr = SharpeRatio::new(5, 0.0).unwrap();
let out = sr.batch(&[0.01; 10]);
for v in out.into_iter().flatten() {
assert_relative_eq!(v, 0.0, epsilon = 1e-12);
}
}
#[test]
fn reference_value() {
// returns = [0.01, 0.02, 0.03, 0.04], rf = 0.
// mean = 0.025, var = ((0.01-.025)^2 + (.02-.025)^2 + (.03-.025)^2
// + (.04-.025)^2) / 3 = 0.00016666..., sd = sqrt(0.000166..) =
// 0.01290994..., Sharpe = 0.025 / 0.01290994 ≈ 1.936491673.
let mut sr = SharpeRatio::new(4, 0.0).unwrap();
let out = sr.batch(&[0.01, 0.02, 0.03, 0.04]);
let expected = 0.025_f64 / (0.000_166_666_666_666_666_67_f64).sqrt();
assert_relative_eq!(out[3].unwrap(), expected, epsilon = 1e-9);
}
#[test]
fn ignores_non_finite_input() {
let mut sr = SharpeRatio::new(3, 0.0).unwrap();
assert_eq!(sr.update(0.01), None);
assert_eq!(sr.update(f64::NAN), None);
assert_eq!(sr.update(0.02), None);
assert!(sr.update(0.03).is_some());
}
#[test]
fn warmup_returns_none() {
let mut sr = SharpeRatio::new(5, 0.0).unwrap();
for i in 0..4 {
assert_eq!(sr.update(f64::from(i) * 0.01), None);
}
assert!(sr.update(0.05).is_some());
}
#[test]
fn reset_clears_state() {
let mut sr = SharpeRatio::new(3, 0.0).unwrap();
sr.batch(&[0.01, 0.02, 0.03]);
assert!(sr.is_ready());
sr.reset();
assert!(!sr.is_ready());
assert_eq!(sr.update(0.01), None);
}
#[test]
fn batch_equals_streaming() {
let returns: Vec<f64> = (0..50)
.map(|i| 0.001 + (f64::from(i) * 0.2).sin() * 0.01)
.collect();
let batch = SharpeRatio::new(10, 0.0).unwrap().batch(&returns);
let mut s = SharpeRatio::new(10, 0.0).unwrap();
let streamed: Vec<_> = returns.iter().map(|p| s.update(*p)).collect();
assert_eq!(batch, streamed);
}
}