"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(backtest): rebalance mask works with non-nanosecond dates
Lanre Shittue8fa344363ac
diff --git a/agent/backtest/rebalance_mask.py b/agent/backtest/rebalance_mask.pyindex 1d32f105c..23a239e6f 100644--- a/agent/backtest/rebalance_mask.py+++ b/agent/backtest/rebalance_mask.py@@ -57,7 +57,12 @@ def resolve_rebalance_dates(if isinstance(validated, str):offset = to_offset(validated)- bounds: list[int] = dates.asi8.tolist()+ # as_unit("ns"): asi8 is in the index's own storage unit (ns/us/ms/s+ # since pandas 2.0), not always nanoseconds, while offset_nanos and+ # Timestamp.value below are always nanoseconds. A non-ns index (the+ # loader cache's duckdb/parquet round-trip can produce one) silently+ # defeated this guard by comparing mismatched units.+ bounds: list[int] = dates.as_unit("ns").asi8.tolist()spacings = [current - previousfor previous, current in zip(bounds, bounds[1:])@@ -83,7 +88,9 @@ def resolve_rebalance_dates(selected.append(timestamp)else:selected = []- bounds: list[int] = dates.asi8.tolist()+ # See the as_unit("ns") note above: bisect_left below compares+ # against Timestamp.value, which is always nanoseconds.+ bounds: list[int] = dates.as_unit("ns").asi8.tolist()for item in validated:requested = cast(pd.Timestamp, pd.Timestamp(item))if dates.tz is not None:diff --git a/agent/tests/test_rebalance_mask.py b/agent/tests/test_rebalance_mask.pyindex b60069cf1..509671418 100644--- a/agent/tests/test_rebalance_mask.py+++ b/agent/tests/test_rebalance_mask.py@@ -134,6 +134,25 @@ def test_monthly_alias_executes_first_trading_bar_of_each_month() -> None:assert engine.bar_sizes == pytest.approx([2.0, 8.0, 8.0])[email protected]("unit", ["s", "ms", "us"])+def test_explicit_date_list_matches_at_any_index_resolution(unit: str) -> None:+ # bisect_left compares asi8 bounds against Timestamp.value (always+ # nanoseconds); a non-ns index's asi8 is in its own storage unit, which+ # silently mismatched every explicit rebalance date without raising.+ dates = pd.DatetimeIndex(+ [+ pd.Timestamp("2026-01-02"),+ pd.Timestamp("2026-01-05"),+ pd.Timestamp("2026-01-06"),+ ]+ ).as_unit(unit)+ engine = _MaskEngine(rebalance_mask=["2026-01-05"])++ _run_masked(engine, [0.2, 0.8, 0.2], dates=dates)++ assert engine.bar_sizes == pytest.approx([None, 8.0, 8.0])++def test_mask_without_trading_date_intersection_is_rejected() -> None:engine = _MaskEngine(rebalance_mask=["2030-01-01"])@@ -155,6 +174,22 @@ def test_alias_finer_than_aligned_bar_spacing_is_rejected(alias: str) -> None:_run_masked(engine, [0.2, 0.8, 0.2], dates=dates)[email protected]("unit", ["s", "ms", "us"])+def test_alias_finer_than_aligned_bar_spacing_is_rejected_at_any_index_resolution(+ unit: str,+) -> None:+ # asi8 is in the index's own storage unit since pandas 2.0, not always+ # nanoseconds, while the offset/Timestamp comparisons it feeds are. A+ # non-ns index -- e.g. one that round-tripped through the loader's+ # duckdb/parquet cache, which can rewrite datetime resolution -- must+ # still be rejected exactly like the default ns-resolution index.+ dates = pd.bdate_range("2026-01-02", periods=3).as_unit(unit)+ engine = _MaskEngine(rebalance_mask="h")++ with pytest.raises(ValueError, match="rebalance_mask.*finer"):+ _run_masked(engine, [0.2, 0.8, 0.2], dates=dates)++@pytest.mark.parametrize(("mask", "position_adjustment"),[