Skip to content

Commit 79a2f5c

Browse files
committed
Refine update_bar aggregation function
1 parent 3f0277b commit 79a2f5c

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

crates/data/src/aggregation.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,18 @@ impl BarBuilder {
233233
self.close = self.last_close;
234234
}
235235

236+
if let (Some(close), Some(low)) = (self.close, self.low) {
237+
if close < low {
238+
self.low = Some(close);
239+
}
240+
}
241+
242+
if let (Some(close), Some(high)) = (self.close, self.high) {
243+
if close > high {
244+
self.high = Some(close);
245+
}
246+
}
247+
236248
// SAFETY: The open was checked, so we can assume all prices are Some
237249
let bar = Bar::new(
238250
self.bar_type,

nautilus_trader/data/aggregation.pyx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ cdef class BarBuilder:
188188
self._low = bar.low
189189
self.initialized = True
190190
else:
191-
if bar.high > self._high:
191+
if bar.high._mem.raw > self._high._mem.raw:
192192
self._high = bar.high
193193

194-
if bar.low < self._low:
194+
if bar.low._mem.raw < self._low._mem.raw:
195195
self._low = bar.low
196196

197197
self._close = bar.close
@@ -245,6 +245,9 @@ cdef class BarBuilder:
245245
self._low = self._last_close
246246
self._close = self._last_close
247247

248+
self._low._mem.raw = min(self._close._mem.raw, self._low._mem.raw)
249+
self._high._mem.raw = max(self._close._mem.raw, self._max._mem.raw)
250+
248251
cdef Bar bar = Bar(
249252
bar_type=self._bar_type,
250253
open=self._open,

0 commit comments

Comments
 (0)