"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(backtest): the engine's date alignment works at any index resolution too (#1560)
Haozhe Wudb78d0c3c153
diff --git a/agent/backtest/engines/base.py b/agent/backtest/engines/base.pyindex 86eda9de0..fc62a3a85 100644--- a/agent/backtest/engines/base.py+++ b/agent/backtest/engines/base.py@@ -257,8 +257,12 @@ def _align(is the bounded-ffill trading view; ``close_val_df`` carries the lasttraded close through halts of any length and is only for valuation."""- # Build unified sorted date index from all symbols' trading calendars- indexes = [data_map[c].index for c in codes]+ # Build unified sorted date index from all symbols' trading calendars.+ # Everything below works on int64 epochs, and ``asi8`` / ``view("i8")``+ # count in the index's own unit: a duckdb-backed local source arrives as+ # datetime64[us], which read as nanoseconds put the run in 1970 and, beside+ # a nanosecond source, matched none of its bars. One unit for all of them.+ indexes = [data_map[c].index.as_unit("ns") for c in codes]merged = np.unique(np.concatenate([index.asi8 for index in indexes]))common_tz = indexes[0].tzif all(index.tz == common_tz for index in indexes) and common_tz is not None:@@ -280,7 +284,7 @@ def _align(close_arr = np.full((n_dates, n_codes), np.nan)for j, c in enumerate(codes):series = data_map[c]["close"]- row_idx = np.searchsorted(dates_i8, series.index.values.view("i8"))+ row_idx = np.searchsorted(dates_i8, indexes[j].asi8)close_arr[row_idx, j] = series.values# Vectorized ffill with limit using pandas (C-optimized internals)@@ -320,7 +324,7 @@ def _align(shifted_vals[0] = 0.0shifted_vals[1:] = sig_vals[:-1]# Place into unified grid via searchsorted- row_idx = np.searchsorted(dates_i8, own_idx.values.view("i8"))+ row_idx = np.searchsorted(dates_i8, own_idx.as_unit("ns").asi8)pos_arr[row_idx, j] = shifted_vals# Vectorized ffill with limit using pandas (C-optimized)diff --git a/agent/tests/test_base_engine.py b/agent/tests/test_base_engine.pyindex b3c6acdd3..f7e3df66c 100644--- a/agent/tests/test_base_engine.py+++ b/agent/tests/test_base_engine.py@@ -985,3 +985,30 @@ def test_rebalance_with_negative_cash_still_executes_its_reductions():assert _sizes(engine.bar_positions[1]) == {"A": 9.0}assert ("B", "insufficient_capital") in engine.rejected++[email protected]("units", [("us", "us"), ("ns", "us"), ("s", "ms")])+def test_align_is_the_same_at_any_index_resolution(units: tuple[str, str]) -> None:+ """_align works on int64 epochs; a duckdb local source arrives as datetime64[us].++ Read as nanoseconds, a microsecond index put the whole run in 1970, and beside+ a nanosecond source it matched none of that symbol's bars (an all-NaN column).+ """+ days = pd.bdate_range("2026-01-02", periods=5)+ closes = np.array([10.0, 11.0, 12.0, 13.0, 14.0])++ def run(unit_a: str, unit_b: str):+ idx_a, idx_b = days.as_unit(unit_a), days.as_unit(unit_b)+ data_map = {+ "A": pd.DataFrame({"open": closes, "high": closes, "low": closes, "close": closes}, index=idx_a),+ "B": pd.DataFrame({"open": closes, "high": closes, "low": closes, "close": closes * 2}, index=idx_b),+ }+ signal_map = {"A": pd.Series([1.0, 0, 1, 0, 1], index=idx_a), "B": pd.Series(0.5, index=idx_b)}+ return _align(data_map, signal_map, ["A", "B"])++ expected_dates, expected_close, _, expected_pos, _ = run("ns", "ns")+ dates, close_df, _, pos_df, _ = run(*units)++ assert list(dates) == list(expected_dates)+ np.testing.assert_array_equal(close_df.to_numpy(), expected_close.to_numpy())+ np.testing.assert_array_equal(pos_df.to_numpy(), expected_pos.to_numpy())