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
//! Open-Interest / Price Divergence — relative OI change minus relative price
//! change over a window.
use std::collections::VecDeque;
use crate::derivatives::DerivativesTick;
use crate::error::{Error, Result};
use crate::traits::Indicator;
/// Open-Interest / Price Divergence — the gap between how fast open interest and
/// the mark price have moved over the trailing window of `window` ticks.
///
/// ```text
/// oiChange = (openInterestₜ − openInterestₜ₋ₙ) / openInterestₜ₋ₙ
/// priceChange = (markPriceₜ − markPriceₜ₋ₙ) / markPriceₜ₋ₙ
/// divergence = oiChange − priceChange (n = window)
/// ```
///
/// Reading the two together is a classic positioning signal: open interest
/// rising while price falls (a positive divergence) marks fresh shorts piling
/// in; open interest falling while price rises marks a short squeeze / unwind.
/// A value near zero means OI and price moved in step. If the reference open
/// interest is zero, the OI term contributes zero (no base to grow from).
///
/// The indicator warms up for `window + 1` ticks — `update` returns `None` until
/// the window spans a full `window`-tick lookback — then emits the divergence,
/// maintained in O(1) per tick via a ring buffer.
///
/// `Input = DerivativesTick`, `Output = f64`.
///
/// # Example
///
/// ```
/// use wickra_core::{DerivativesTick, Indicator, OIPriceDivergence};
///
/// fn tick(oi: f64, mark: f64) -> DerivativesTick {
/// DerivativesTick::new(0.0, mark, mark, mark, oi, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0)
/// .unwrap()
/// }
///
/// let mut div = OIPriceDivergence::new(1).unwrap();
/// assert_eq!(div.update(tick(1_000.0, 100.0)), None);
/// // OI +10% while price flat -> divergence +0.1.
/// assert!((div.update(tick(1_100.0, 100.0)).unwrap() - 0.1).abs() < 1e-12);
/// ```
#[derive(Debug, Clone)]
pub struct OIPriceDivergence {
window: usize,
history: VecDeque<(f64, f64)>,
}
impl OIPriceDivergence {
/// Construct an OI / price divergence 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 + 1),
})
}
/// The configured window length, in ticks.
#[must_use]
pub fn window(&self) -> usize {
self.window
}
}
impl Indicator for OIPriceDivergence {
type Input = DerivativesTick;
type Output = f64;
fn update(&mut self, tick: DerivativesTick) -> Option<f64> {
self.history
.push_back((tick.open_interest, tick.mark_price));
if self.history.len() > self.window + 1 {
self.history.pop_front();
}
if self.history.len() < self.window + 1 {
return None;
}
let (old_oi, old_mark) = *self.history.front().expect("len == window + 1");
let (cur_oi, cur_mark) = *self.history.back().expect("len == window + 1");
// Open interest can legitimately be zero; with no base there is no
// relative change to report from it.
let oi_change = if old_oi == 0.0 {
0.0
} else {
(cur_oi - old_oi) / old_oi
};
// The mark price is finite and positive by `DerivativesTick`
// construction, so the denominator is always well-defined.
let price_change = (cur_mark - old_mark) / old_mark;
Some(oi_change - price_change)
}
fn reset(&mut self) {
self.history.clear();
}
fn warmup_period(&self) -> usize {
self.window + 1
}
fn is_ready(&self) -> bool {
self.history.len() == self.window + 1
}
fn name(&self) -> &'static str {
"OIPriceDivergence"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
fn tick(oi: f64, mark: f64) -> DerivativesTick {
DerivativesTick::new_unchecked(0.0, mark, mark, mark, oi, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0)
}
#[test]
fn rejects_zero_window() {
assert!(matches!(OIPriceDivergence::new(0), Err(Error::PeriodZero)));
}
#[test]
fn accessors_and_metadata() {
let div = OIPriceDivergence::new(5).unwrap();
assert_eq!(div.name(), "OIPriceDivergence");
assert_eq!(div.warmup_period(), 6);
assert_eq!(div.window(), 5);
assert!(!div.is_ready());
}
#[test]
fn oi_up_price_flat_is_positive() {
let mut div = OIPriceDivergence::new(1).unwrap();
assert_eq!(div.update(tick(1_000.0, 100.0)), None);
let out = div.update(tick(1_100.0, 100.0)).unwrap();
assert!((out - 0.1).abs() < 1e-12);
assert!(div.is_ready());
}
#[test]
fn oi_flat_price_up_is_negative() {
let mut div = OIPriceDivergence::new(1).unwrap();
div.update(tick(1_000.0, 100.0));
// OI flat, price +10% -> divergence -0.1.
let out = div.update(tick(1_000.0, 110.0)).unwrap();
assert!((out + 0.1).abs() < 1e-12);
}
#[test]
fn zero_reference_oi_drops_oi_term() {
let mut div = OIPriceDivergence::new(1).unwrap();
div.update(tick(0.0, 100.0));
// Reference OI is zero -> only the price term contributes: -(110-100)/100.
let out = div.update(tick(500.0, 110.0)).unwrap();
assert!((out + 0.1).abs() < 1e-12);
}
#[test]
fn batch_equals_streaming() {
let ticks: Vec<DerivativesTick> = (0..30)
.map(|i| tick(1_000.0 + f64::from(i % 7) * 10.0, 100.0 + f64::from(i % 5)))
.collect();
let mut a = OIPriceDivergence::new(4).unwrap();
let mut b = OIPriceDivergence::new(4).unwrap();
assert_eq!(
a.batch(&ticks),
ticks.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
);
}
#[test]
fn reset_clears_state() {
let mut div = OIPriceDivergence::new(1).unwrap();
div.update(tick(1_000.0, 100.0));
div.update(tick(1_100.0, 100.0));
assert!(div.is_ready());
div.reset();
assert!(!div.is_ready());
assert_eq!(div.update(tick(1_000.0, 100.0)), None);
}
}