perf: replace O(n^2) data slice with a windowed view (~2.6x faster py-next)

This commit is contained in:
KhizarImran
2026-07-22 22:37:26 +01:00
parent 494f157c13
commit 4972863b5f
2 changed files with 48 additions and 1 deletions
+15
View File
@@ -5,6 +5,21 @@ import unittest
import pandas as pd
from backtestingfx import Backtest, Strategy
from backtestingfx.backtest import _DataView
class DataViewTest(unittest.TestCase):
def test_window_hides_future_bars_and_slices_correctly(self):
view = _DataView(["a", "b", "c", "d"])
view._len = 3 # only the first 3 bars are "visible" so far
self.assertEqual(len(view), 3)
self.assertEqual(view[-1], "c") # last visible, not "d"
self.assertEqual(view[0], "a")
self.assertEqual(view[-2:], ["b", "c"]) # trailing slice, no "d"
self.assertEqual(list(view), ["a", "b", "c"])
with self.assertRaises(IndexError):
view[3] # "d" is in the future, not addressable yet
class BuyAndHold(Strategy):