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
//! Open-Interest-Weighted Price — cumulative mark price weighted by open
//! interest.
use crate::derivatives::DerivativesTick;
use crate::traits::Indicator;
/// Open-Interest-Weighted Price — the running mean mark price, weighting each
/// tick by its open interest.
///
/// ```text
/// oiWeighted = Σ(markPrice · openInterest) / Σ openInterest
/// ```
///
/// Where a plain mean treats every tick equally, the OI-weighted price pulls
/// toward the levels at which the most contracts were actually outstanding — the
/// price the bulk of open positioning sits around, a fair-value anchor for
/// liquidations and mean-reversion. The accumulation runs from construction;
/// call [`reset`] at each session boundary to re-anchor. Until any open interest
/// has accrued the indicator returns the current mark price.
///
/// `Input = DerivativesTick`, `Output = f64`. Ready after the first tick.
///
/// [`reset`]: crate::Indicator::reset
///
/// # Example
///
/// ```
/// use wickra_core::{DerivativesTick, Indicator, OIWeighted};
///
/// fn tick(mark: f64, oi: 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 oiw = OIWeighted::new();
/// assert_eq!(oiw.update(tick(100.0, 10.0)), Some(100.0));
/// // (100·10 + 110·30) / (10 + 30) = 4300 / 40 = 107.5.
/// assert_eq!(oiw.update(tick(110.0, 30.0)), Some(107.5));
/// ```
#[derive(Debug, Clone, Default)]
pub struct OIWeighted {
sum_weighted: f64,
sum_oi: f64,
has_emitted: bool,
}
impl OIWeighted {
/// Construct a new OI-weighted price indicator.
#[must_use]
pub const fn new() -> Self {
Self {
sum_weighted: 0.0,
sum_oi: 0.0,
has_emitted: false,
}
}
}
impl Indicator for OIWeighted {
type Input = DerivativesTick;
type Output = f64;
fn update(&mut self, tick: DerivativesTick) -> Option<f64> {
self.has_emitted = true;
self.sum_weighted += tick.mark_price * tick.open_interest;
self.sum_oi += tick.open_interest;
if self.sum_oi == 0.0 {
// No open interest has accrued yet: fall back to the mark price.
return Some(tick.mark_price);
}
Some(self.sum_weighted / self.sum_oi)
}
fn reset(&mut self) {
self.sum_weighted = 0.0;
self.sum_oi = 0.0;
self.has_emitted = false;
}
fn warmup_period(&self) -> usize {
1
}
fn is_ready(&self) -> bool {
self.has_emitted
}
fn name(&self) -> &'static str {
"OIWeighted"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
fn tick(mark: f64, oi: 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 accessors_and_metadata() {
let oiw = OIWeighted::new();
assert_eq!(oiw.name(), "OIWeighted");
assert_eq!(oiw.warmup_period(), 1);
assert!(!oiw.is_ready());
}
#[test]
fn weights_by_open_interest() {
let mut oiw = OIWeighted::new();
assert_eq!(oiw.update(tick(100.0, 10.0)), Some(100.0));
// (100·10 + 110·30) / 40 = 107.5.
assert_eq!(oiw.update(tick(110.0, 30.0)), Some(107.5));
assert!(oiw.is_ready());
}
#[test]
fn zero_open_interest_falls_back_to_mark() {
let mut oiw = OIWeighted::new();
assert_eq!(oiw.update(tick(123.0, 0.0)), Some(123.0));
// Still no OI on the second zero-OI tick.
assert_eq!(oiw.update(tick(125.0, 0.0)), Some(125.0));
}
#[test]
fn batch_equals_streaming() {
let ticks: Vec<DerivativesTick> = (0..20)
.map(|i| tick(100.0 + f64::from(i % 5), 1.0 + f64::from(i % 4)))
.collect();
let mut a = OIWeighted::new();
let mut b = OIWeighted::new();
assert_eq!(
a.batch(&ticks),
ticks.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
);
}
#[test]
fn reset_re_anchors() {
let mut oiw = OIWeighted::new();
oiw.update(tick(100.0, 10.0));
oiw.update(tick(110.0, 30.0));
assert!(oiw.is_ready());
oiw.reset();
assert!(!oiw.is_ready());
// After reset the accumulation starts again from the next tick.
assert_eq!(oiw.update(tick(200.0, 5.0)), Some(200.0));
}
}