Skip to content

Commit e2336e6

Browse files
committed
revert: restore linebuffer.py LineOwnOperation.once() to avoid regression
1 parent 36b791e commit e2336e6

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

backtrader/linebuffer.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2427,12 +2427,46 @@ def once(self, start, end):
24272427
start: Starting index.
24282428
end: Ending index.
24292429
"""
2430+
# CRITICAL FIX: Ensure source operand is processed first
2431+
if self._parent_a is not None and hasattr(self._parent_a, "_once"):
2432+
try:
2433+
self._parent_a._once(start, end)
2434+
except Exception:
2435+
pass
2436+
2437+
# cache python dictionary lookups
24302438
dst = self.array
24312439
srca = self.a.array
24322440
op = self.operation
24332441

2442+
# CRITICAL FIX: Ensure destination array is properly sized
2443+
while len(dst) < end:
2444+
dst.append(float("nan"))
2445+
2446+
# CRITICAL FIX: Ensure source array has required data
2447+
if len(srca) < end:
2448+
# If source array is shorter than required range, only process available data
2449+
end = min(end, len(srca))
2450+
24342451
for i in range(start, end):
2435-
dst[i] = op(srca[i])
2452+
try:
2453+
# CRITICAL FIX: Bounds checking for source array
2454+
a_val = srca[i] if i < len(srca) else 0.0
2455+
2456+
# Ensure value is numeric
2457+
if a_val is None or (isinstance(a_val, float) and math.isnan(a_val)):
2458+
a_val = 0.0
2459+
2460+
result = op(a_val)
2461+
2462+
# Ensure result is valid
2463+
if result is None or (isinstance(result, float) and math.isnan(result)):
2464+
result = 0.0
2465+
2466+
dst[i] = result
2467+
except Exception:
2468+
# If operation fails, store 0.0
2469+
dst[i] = 0.0
24362470

24372471
def size(self):
24382472
"""Return the number of lines in this LineActions object"""

0 commit comments

Comments
 (0)