diff --git a/src/dexorder/ohlc.py b/src/dexorder/ohlc.py index 7ce533a..842c400 100644 --- a/src/dexorder/ohlc.py +++ b/src/dexorder/ohlc.py @@ -235,7 +235,7 @@ class Chunk: return None start = self.bars[0].start index = (time - start) // self.period - if index >= len(self.bars): + if index < 0 or index >= len(self.bars): return None bar = self.bars[index] assert bar.start == time @@ -263,6 +263,16 @@ class Chunk: time += self.period self.bars.append(NativeOHLC(time, None, None, None, last_price)) assert index == len(self.bars) + elif index < 0: + # the incoming bar is prior to anything we have yet. rebase our bars on the incoming and pad + bars = [native] + for _ in range(-index-1): + time = bars[-1].start + self.period + price = bars[-1].close + bars.append(NativeOHLC(time, None, None, None, price)) + assert bars[-1].start + self.period == self.bars[0].start + self.bars = [*bars, *self.bars] + return if index == len(self.bars): assert self.bars[-1].start + self.period == native.start self.bars.append(native)