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
//! Funding Rate Z-Score — how extreme the latest funding rate is versus its
//! recent history.
use std::collections::VecDeque;
use crate::derivatives::DerivativesTick;
use crate::error::{Error, Result};
use crate::traits::Indicator;
/// Funding Rate Z-Score — the latest funding rate expressed in standard
/// deviations from its rolling mean over the trailing window of `window` ticks.
///
/// ```text
/// zScore = (fundingRate − mean) / population_stddev over the last `window` ticks
/// ```
///
/// A reading of `+2` means funding is two standard deviations richer than its
/// recent norm — an unusually crowded long, a contrarian fade signal; `−2` is
/// the mirror. Normalising the [funding rate] this way makes funding extremes
/// comparable across regimes and assets. A window with zero dispersion (a flat
/// funding series) yields `0`. The indicator warms up for `window` ticks, then
/// emits the rolling z-score, maintained in O(1) per tick.
///
/// `Input = DerivativesTick`, `Output = f64`.
///
/// [funding rate]: crate::FundingRate
///
/// # Example
///
/// ```
/// use wickra_core::{DerivativesTick, FundingRateZScore, Indicator};
///
/// fn tick(rate: f64) -> DerivativesTick {
/// DerivativesTick::new(rate, 100.0, 100.0, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0)
/// .unwrap()
/// }
///
/// let mut z = FundingRateZScore::new(2).unwrap();
/// assert_eq!(z.update(tick(0.001)), None);
/// // Window [0.001, 0.003]: mean 0.002, population stddev 0.001 -> (0.003 - 0.002) / 0.001 = 1.
/// assert!((z.update(tick(0.003)).unwrap() - 1.0).abs() < 1e-9);
/// ```
#[derive(Debug, Clone)]
pub struct FundingRateZScore {
window: usize,
history: VecDeque<f64>,
sum: f64,
sum_sq: f64,
}
impl FundingRateZScore {
/// Construct a funding-rate z-score over a window of `window` ticks.
///
/// # Errors
///
/// Returns [`Error::PeriodZero`] if `window` is zero.
pub fn new(window: usize) -> Result<Self> {
if window == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
window,
history: VecDeque::with_capacity(window),
sum: 0.0,
sum_sq: 0.0,
})
}
/// The configured window length, in ticks.
#[must_use]
pub fn window(&self) -> usize {
self.window
}
}
impl Indicator for FundingRateZScore {
type Input = DerivativesTick;
type Output = f64;
fn update(&mut self, tick: DerivativesTick) -> Option<f64> {
let value = tick.funding_rate;
if self.history.len() == self.window {
let old = self.history.pop_front().expect("non-empty");
self.sum -= old;
self.sum_sq -= old * old;
}
self.history.push_back(value);
self.sum += value;
self.sum_sq += value * value;
if self.history.len() < self.window {
return None;
}
let n = self.window as f64;
let mean = self.sum / n;
// Population variance E[x²] − E[x]²; clamp away tiny negative drift.
let variance = (self.sum_sq / n - mean * mean).max(0.0);
let std = variance.sqrt();
if std == 0.0 {
// A window with no dispersion: funding is exactly its own mean.
return Some(0.0);
}
Some((value - mean) / std)
}
fn reset(&mut self) {
self.history.clear();
self.sum = 0.0;
self.sum_sq = 0.0;
}
fn warmup_period(&self) -> usize {
self.window
}
fn is_ready(&self) -> bool {
self.history.len() == self.window
}
fn name(&self) -> &'static str {
"FundingRateZScore"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
fn tick(rate: f64) -> DerivativesTick {
DerivativesTick::new_unchecked(
rate, 100.0, 100.0, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0,
)
}
#[test]
fn rejects_zero_window() {
assert!(matches!(FundingRateZScore::new(0), Err(Error::PeriodZero)));
}
#[test]
fn accessors_and_metadata() {
let z = FundingRateZScore::new(5).unwrap();
assert_eq!(z.name(), "FundingRateZScore");
assert_eq!(z.warmup_period(), 5);
assert_eq!(z.window(), 5);
assert!(!z.is_ready());
}
#[test]
fn reference_value() {
let mut z = FundingRateZScore::new(2).unwrap();
assert_eq!(z.update(tick(0.001)), None);
// Window [0.001, 0.003]: mean 0.002, var (1e-6 + 9e-6)/2 - 4e-6 = 1e-6,
// stddev 0.001; latest 0.003 is (0.003 - 0.002) / 0.001 = 1.
let out = z.update(tick(0.003)).unwrap();
assert!((out - 1.0).abs() < 1e-9);
assert!(z.is_ready());
}
#[test]
fn flat_window_is_zero() {
let mut z = FundingRateZScore::new(3).unwrap();
z.update(tick(0.002));
z.update(tick(0.002));
assert_eq!(z.update(tick(0.002)), Some(0.0));
}
#[test]
fn rolls_off_old_values() {
let mut z = FundingRateZScore::new(2).unwrap();
z.update(tick(0.001));
z.update(tick(0.003));
// Window now [0.003, 0.005]: mean 0.004, stddev 0.001 -> +1.
let out = z.update(tick(0.005)).unwrap();
assert!((out - 1.0).abs() < 1e-9);
}
#[test]
fn batch_equals_streaming() {
let ticks: Vec<DerivativesTick> = (0..30)
.map(|i| tick(0.0001 * f64::from(i % 5) - 0.0002))
.collect();
let mut a = FundingRateZScore::new(6).unwrap();
let mut b = FundingRateZScore::new(6).unwrap();
assert_eq!(
a.batch(&ticks),
ticks.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
);
}
#[test]
fn reset_clears_state() {
let mut z = FundingRateZScore::new(2).unwrap();
z.update(tick(0.001));
z.update(tick(0.003));
assert!(z.is_ready());
z.reset();
assert!(!z.is_ready());
assert_eq!(z.update(tick(0.002)), None);
}
}