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)]
31pub struct OnBalanceVolume {
32 pub period: usize,
33 pub value: f64,
34 pub initialized: bool,
35 has_inputs: bool,
36 obv: ArrayDeque<f64, MAX_PERIOD, Wrapping>,
37}
38
39impl Display for OnBalanceVolume {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 write!(f, "{}({})", self.name(), self.period)
42 }
43}
44
45impl Indicator for OnBalanceVolume {
46 fn name(&self) -> String {
47 stringify!(OnBalanceVolume).to_string()
48 }
49
50 fn has_inputs(&self) -> bool {
51 self.has_inputs
52 }
53
54 fn initialized(&self) -> bool {
55 self.initialized
56 }
57
58 fn handle_bar(&mut self, bar: &Bar) {
59 self.update_raw(
60 (&bar.open).into(),
61 (&bar.close).into(),
62 (&bar.volume).into(),
63 );
64 }
65
66 fn reset(&mut self) {
67 self.obv.clear();
68 self.value = 0.0;
69 self.has_inputs = false;
70 self.initialized = false;
71 }
72}
73
74impl OnBalanceVolume {
75 #[must_use]
82 pub fn new(period: usize) -> Self {
83 assert!(
84 period <= MAX_PERIOD,
85 "OnBalanceVolume: period {period} exceeds MAX_PERIOD ({MAX_PERIOD})"
86 );
87
88 Self {
89 period,
90 value: 0.0,
91 obv: ArrayDeque::new(),
92 has_inputs: false,
93 initialized: false,
94 }
95 }
96
97 pub fn update_raw(&mut self, open: f64, close: f64, volume: f64) {
98 let delta = if close > open {
99 volume
100 } else if close < open {
101 -volume
102 } else {
103 0.0
104 };
105
106 let _ = self.obv.push_back(delta);
107
108 self.value = self.obv.iter().sum();
109
110 if !self.initialized {
111 self.has_inputs = true;
112 if (self.period == 0 && !self.obv.is_empty()) || self.obv.len() >= self.period {
113 self.initialized = true;
114 }
115 }
116 }
117}
118
119#[cfg(test)]
120mod tests {
121 use rstest::rstest;
122
123 use super::*;
124 use crate::stubs::obv_10;
125
126 #[rstest]
127 fn test_name_returns_expected_string(obv_10: OnBalanceVolume) {
128 assert_eq!(obv_10.name(), "OnBalanceVolume");
129 }
130
131 #[rstest]
132 fn test_str_repr_returns_expected_string(obv_10: OnBalanceVolume) {
133 assert_eq!(format!("{obv_10}"), "OnBalanceVolume(10)");
134 }
135
136 #[rstest]
137 fn test_period_returns_expected_value(obv_10: OnBalanceVolume) {
138 assert_eq!(obv_10.period, 10);
139 }
140
141 #[rstest]
142 fn test_initialized_without_inputs_returns_false(obv_10: OnBalanceVolume) {
143 assert!(!obv_10.initialized());
144 }
145
146 #[rstest]
147 fn test_value_with_all_higher_inputs_returns_expected_value(mut obv_10: OnBalanceVolume) {
148 let open_values = [
149 104.25, 105.50, 106.75, 108.00, 109.25, 110.50, 111.75, 113.00, 114.25, 115.50, 116.75,
150 118.00, 119.25, 120.50, 121.75,
151 ];
152
153 let close_values = [
154 105.50, 106.75, 108.00, 109.25, 110.50, 111.75, 113.00, 114.25, 115.50, 116.75, 118.00,
155 119.25, 120.50, 121.75, 123.00,
156 ];
157
158 let volume_values = [
159 1000.0, 1200.0, 1500.0, 1800.0, 2000.0, 2200.0, 2500.0, 2800.0, 3000.0, 3200.0, 3500.0,
160 3800.0, 4000.0, 4200.0, 4500.0,
161 ];
162 for i in 0..15 {
163 obv_10.update_raw(open_values[i], close_values[i], volume_values[i]);
164 }
165
166 assert!(obv_10.initialized());
167 assert_eq!(obv_10.value, 41200.0);
168 }
169
170 #[rstest]
171 fn test_reset_successfully_returns_indicator_to_fresh_state(mut obv_10: OnBalanceVolume) {
172 obv_10.update_raw(1.00020, 1.00050, 1000.0);
173 obv_10.update_raw(1.00030, 1.00060, 1200.0);
174 obv_10.update_raw(1.00070, 1.00080, 1500.0);
175
176 obv_10.reset();
177
178 assert!(!obv_10.initialized());
179 assert_eq!(obv_10.value, 0.0);
180 assert_eq!(obv_10.obv.len(), 0);
181 assert!(!obv_10.has_inputs);
182 assert!(!obv_10.initialized);
183 }
184}