nautilus_indicators/momentum/
obv.rs1use std::fmt::Display;
17
18use arraydeque::{ArrayDeque, Wrapping};
19use nautilus_model::data::Bar;
20
21use crate::indicator::Indicator;
22
23const MAX_PERIOD: usize = 1_024;
24
25#[repr(C)]
26#[derive(Debug)]
27#[cfg_attr(
28 feature = "python",
29 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.indicators")
30)]
31#[cfg_attr(
32 feature = "python",
33 pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.indicators")
34)]
35pub struct OnBalanceVolume {
36 pub period: usize,
37 pub value: f64,
38 pub initialized: bool,
39 has_inputs: bool,
40 obv: ArrayDeque<f64, MAX_PERIOD, Wrapping>,
41}
42
43impl Display for OnBalanceVolume {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 write!(f, "{}({})", self.name(), self.period)
46 }
47}
48
49impl Indicator for OnBalanceVolume {
50 fn name(&self) -> String {
51 stringify!(OnBalanceVolume).to_string()
52 }
53
54 fn has_inputs(&self) -> bool {
55 self.has_inputs
56 }
57
58 fn initialized(&self) -> bool {
59 self.initialized
60 }
61
62 fn handle_bar(&mut self, bar: &Bar) {
63 self.update_raw(
64 (&bar.open).into(),
65 (&bar.close).into(),
66 (&bar.volume).into(),
67 );
68 }
69
70 fn reset(&mut self) {
71 self.obv.clear();
72 self.value = 0.0;
73 self.has_inputs = false;
74 self.initialized = false;
75 }
76}
77
78impl OnBalanceVolume {
79 #[must_use]
86 pub fn new(period: usize) -> Self {
87 assert!(
88 period <= MAX_PERIOD,
89 "OnBalanceVolume: period {period} exceeds MAX_PERIOD ({MAX_PERIOD})"
90 );
91
92 Self {
93 period,
94 value: 0.0,
95 obv: ArrayDeque::new(),
96 has_inputs: false,
97 initialized: false,
98 }
99 }
100
101 pub fn update_raw(&mut self, open: f64, close: f64, volume: f64) {
102 let delta = if close > open {
103 volume
104 } else if close < open {
105 -volume
106 } else {
107 0.0
108 };
109
110 if self.period == 0 {
111 self.value += delta;
113 } else {
114 if self.obv.len() == self.period {
116 let _ = self.obv.pop_front();
117 }
118 let _ = self.obv.push_back(delta);
119 self.value = self.obv.iter().sum();
120 }
121
122 if !self.initialized {
123 self.has_inputs = true;
124
125 if self.period == 0 || self.obv.len() >= self.period {
126 self.initialized = true;
127 }
128 }
129 }
130}
131
132#[cfg(test)]
133mod tests {
134 use rstest::rstest;
135
136 use super::*;
137 use crate::stubs::obv_10;
138
139 #[rstest]
140 fn test_name_returns_expected_string(obv_10: OnBalanceVolume) {
141 assert_eq!(obv_10.name(), "OnBalanceVolume");
142 }
143
144 #[rstest]
145 fn test_str_repr_returns_expected_string(obv_10: OnBalanceVolume) {
146 assert_eq!(format!("{obv_10}"), "OnBalanceVolume(10)");
147 }
148
149 #[rstest]
150 fn test_period_returns_expected_value(obv_10: OnBalanceVolume) {
151 assert_eq!(obv_10.period, 10);
152 }
153
154 #[rstest]
155 fn test_initialized_without_inputs_returns_false(obv_10: OnBalanceVolume) {
156 assert!(!obv_10.initialized());
157 }
158
159 #[rstest]
160 fn test_value_with_all_higher_inputs_returns_expected_value(mut obv_10: OnBalanceVolume) {
161 let open_values = [
162 104.25, 105.50, 106.75, 108.00, 109.25, 110.50, 111.75, 113.00, 114.25, 115.50, 116.75,
163 118.00, 119.25, 120.50, 121.75,
164 ];
165
166 let close_values = [
167 105.50, 106.75, 108.00, 109.25, 110.50, 111.75, 113.00, 114.25, 115.50, 116.75, 118.00,
168 119.25, 120.50, 121.75, 123.00,
169 ];
170
171 let volume_values = [
172 1000.0, 1200.0, 1500.0, 1800.0, 2000.0, 2200.0, 2500.0, 2800.0, 3000.0, 3200.0, 3500.0,
173 3800.0, 4000.0, 4200.0, 4500.0,
174 ];
175
176 for i in 0..15 {
177 obv_10.update_raw(open_values[i], close_values[i], volume_values[i]);
178 }
179
180 assert!(obv_10.initialized());
181 assert_eq!(obv_10.value, 33700.0);
182 }
183
184 #[rstest]
185 fn test_reset_successfully_returns_indicator_to_fresh_state(mut obv_10: OnBalanceVolume) {
186 obv_10.update_raw(1.00020, 1.00050, 1000.0);
187 obv_10.update_raw(1.00030, 1.00060, 1200.0);
188 obv_10.update_raw(1.00070, 1.00080, 1500.0);
189
190 obv_10.reset();
191
192 assert!(!obv_10.initialized());
193 assert_eq!(obv_10.value, 0.0);
194 assert_eq!(obv_10.obv.len(), 0);
195 assert!(!obv_10.has_inputs);
196 assert!(!obv_10.initialized);
197 }
198
199 #[rstest]
200 fn test_value_respects_period_window() {
201 let mut obv = OnBalanceVolume::new(3);
202
203 obv.update_raw(1.0, 2.0, 1000.0); obv.update_raw(1.0, 2.0, 1.0);
207 obv.update_raw(1.0, 2.0, 2.0);
208 obv.update_raw(1.0, 2.0, 3.0);
209
210 assert_eq!(obv.value, 6.0);
212 }
213
214 #[rstest]
215 fn test_period_zero_is_unbounded_cumulative() {
216 let mut obv = OnBalanceVolume::new(0);
217
218 let count = MAX_PERIOD + 100;
221 for _ in 0..count {
222 obv.update_raw(1.0, 2.0, 1.0);
223 }
224
225 assert!(obv.initialized());
226 assert_eq!(obv.value, count as f64);
227 }
228}