chunk negative index bugfix

This commit is contained in:
Tim
2024-04-04 00:25:10 -04:00
parent 328aea1213
commit 923c1a664b

View File

@@ -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)