"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(backtest): disclose lot assumptions and reuse aligned calendar mappings
Haozhe Wucb9f9c7ab58e
diff --git a/agent/backtest/engines/base.py b/agent/backtest/engines/base.pyindex a9d9ee922..a21d9342c 100644--- a/agent/backtest/engines/base.py+++ b/agent/backtest/engines/base.py@@ -281,6 +281,14 @@ def _align(# Use int64 view for O(log n) searchsorted lookupsdates_i8 = dates.values.view("i8")+ # Daily panels usually share one calendar. Direct placement avoids two+ # binary-search passes per symbol over an already identical grid. Reuse+ # each irregular calendar's mapping for both prices and shifted signals.+ row_indexes = {+ c: slice(None) if ns_index[c].equals(dates)+ else np.searchsorted(dates_i8, ns_index[c].asi8)+ for c in codes+ }# ffill with limit to avoid masking long suspensions (e.g. 3-week halt)# Cross-market needs larger limit (Chinese New Year can be 9-10 bars)@@ -290,8 +298,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, indexes[j].asi8)- close_arr[row_idx, j] = series.values+ close_arr[row_indexes[c], j] = series.values# Vectorized ffill with limit using pandas (C-optimized internals)_tmp = pd.DataFrame(close_arr)@@ -330,8 +337,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, ns_index[c].asi8)- pos_arr[row_idx, j] = shifted_vals+ pos_arr[row_indexes[c], j] = shifted_vals# Vectorized ffill with limit using pandas (C-optimized)_tmp = pd.DataFrame(pos_arr)diff --git a/agent/backtest/engines/composite.py b/agent/backtest/engines/composite.pyindex 069300efd..b74a45625 100644--- a/agent/backtest/engines/composite.py+++ b/agent/backtest/engines/composite.py@@ -8,7 +8,7 @@ All state (capital, positions, trades) lives in CompositeEngine.from __future__ import annotations-from typing import Dict, List+from typing import Any, Dict, Listimport pandas as pd@@ -171,6 +171,26 @@ class CompositeEngine(BaseEngine):self._run_interval = str(config.get("interval", "1D"))return super().run_backtest(config, *args, **kwargs)+ def _engine_diagnostics(self) -> Dict[str, Any]:+ """Carry sub-engine assumptions into the shared run's metrics.++ Returns:+ Per-market diagnostics, plus combined list-valued assumptions+ using the same keys as a single-market run.+ """+ by_market = {+ market: details for market, engine in self._rule_engines.items()+ if (details := engine._engine_diagnostics())+ }+ if not by_market:+ return {}+ result: Dict[str, Any] = {"market_diagnostics": by_market}+ for details in by_market.values():+ for key, value in details.items():+ if isinstance(value, list):+ result.setdefault(key, []).extend(value)+ return result+def _rule_for(self, symbol: str) -> BaseEngine:"""Get the sub-engine that provides rules for this symbol."""market = self._symbol_market.get(symbol, "a_share")diff --git a/agent/backtest/engines/global_equity.py b/agent/backtest/engines/global_equity.pyindex 8237d1874..b73dbe07a 100644--- a/agent/backtest/engines/global_equity.py+++ b/agent/backtest/engines/global_equity.py@@ -68,10 +68,9 @@ class GlobalEquityEngine(BaseEngine):- hk_levy: default 0.0000565 (SFC + FRC)- hk_settlement: default 0.00002 (CCASS)- hk_lot_sizes: ``{symbol: board lot}``. HK board lots are set per- issuer, so a flat 100 mis-sizes most names. When the table is given,- any HK symbol missing from it falls back to 100 and is reported in- the run metrics under ``lot_assumptions``. Without the table every HK- symbol uses 100, as before.+ issuer, so a flat 100 mis-sizes most names. Any HK symbol missing+ from the table falls back to 100 and is reported in the run metrics+ under ``lot_assumptions``, including runs with no table.- slippage_ca: defaults to slippage_us- ca_commission: broker commission rate, default 0.0- slippage_uk: defaults to slippage_us@@ -131,7 +130,7 @@ class GlobalEquityEngine(BaseEngine):lot = self.hk_lot_sizes.get(key, 0)if lot > 0:return lot- if self.hk_lot_sizes and key not in self.lot_assumptions:+ if key not in self.lot_assumptions:self.lot_assumptions[key] = _DEFAULT_HK_BOARD_LOTlogger.warning("HK symbol %r has no usable hk_lot_sizes entry; sizing it on "diff --git a/agent/src/skills/strategy-generate/SKILL.md b/agent/src/skills/strategy-generate/SKILL.mdindex 37a63df51..31fcb520b 100644--- a/agent/src/skills/strategy-generate/SKILL.md+++ b/agent/src/skills/strategy-generate/SKILL.md@@ -158,7 +158,7 @@ Self-check after writing `signal_engine.py`:- `rebalance_tolerance`: drift band around the target, as a fraction of it, used only under `"rebalance"`. A resize executes once the held weight has moved further than this from its target; a **changed** target breaches any sane band on its own, so target changes always execute. Default `0.0` means no band, and then the resize test is decided by the slippage width alone — measured on a constant 20% target over 60 bars, `0.0` produced 60 fills, `0.02` produced 12, and `0.05` produced 5 while the weight never left 0.21. Use `rebalance_mask`, not tolerance, to express a strategy's execution cadence. `0.05` is a reasonable starting point, not a recommendation with evidence behind it — it is your modelling choice and the report records the value the run used.- `initial_cash`: default 1,000,000- `commission`: default 0.1%-- `hk_lot_sizes`: optional, Hong Kong only — `{"00700.HK": 100, "02800.HK": 500}`. HK board lots are set per issuer, so without this table every HK order is rounded to 100 shares and a name whose real lot is smaller than its target trades zero times. Codes match with or without leading zeros. A symbol missing from a supplied table is sized on 100 shares and listed under `lot_assumptions` in the metrics, so the report says which lots were assumed.+- `hk_lot_sizes`: optional, Hong Kong only — `{"00700.HK": 100, "02800.HK": 500}`. HK board lots are set per issuer, so without this table every HK order is rounded to 100 shares and a name whose real lot is smaller than its target trades zero times. Codes match with or without leading zeros. Every symbol with no configured lot size uses 100 shares and is listed under `lot_assumptions` in the metrics, including runs without a table. Reports must identify these as assumptions rather than verified exchange lots.- `validation`: optional statistical validation after backtest completes. Omit to skip. Example:```json"validation": {diff --git a/agent/tests/test_global_equity_hk_lot_sizes.py b/agent/tests/test_global_equity_hk_lot_sizes.pyindex cf17889e2..b369eb3f3 100644--- a/agent/tests/test_global_equity_hk_lot_sizes.py+++ b/agent/tests/test_global_equity_hk_lot_sizes.py@@ -80,7 +80,7 @@ def test_non_positive_lot_is_treated_as_missing() -> None:assert engine.lot_assumptions == {"0001.HK": 100}-def test_without_a_table_sizing_and_output_are_unchanged(+def test_without_a_table_default_lots_are_still_disclosed(caplog: pytest.LogCaptureFixture,) -> None:engine = _engine(lots=None)@@ -89,8 +89,9 @@ def test_without_a_table_sizing_and_output_are_unchanged(assert _sized(engine, "00001.HK", 99) == 0assert _sized(engine, "00002.HK", 250) == 200- assert engine._engine_diagnostics() == {}- assert not caplog.records+ assert engine.lot_assumptions == {"0001.HK": 100, "0002.HK": 100}+ assert len(engine._engine_diagnostics()["lot_assumptions"]) == 2+ assert len(caplog.records) == 2def test_non_hk_markets_ignore_the_table() -> None:@@ -127,3 +128,65 @@ def test_a_rebalance_fills_each_symbol_on_its_own_lot() -> None:assert opened["00001.HK"] > 0 and opened["00001.HK"] % 20 == 0assert opened["00002.HK"] > 0 and opened["00002.HK"] % 500 == 0assert engine.lot_assumptions == {}+++def test_composite_preserves_lot_and_futures_assumptions():+ from backtest.engines.composite import CompositeEngine++ engine = CompositeEngine({"hk_lot_sizes": {"0700.HK": 20}}, ["0700.HK", "0005.HK"])+ engine._active_symbol = "0005.HK"+ assert engine.round_size(250, 10) == 200+ hk = engine._rule_for("0005.HK")+ # Exercise an independent diagnostic provider without inventing a future's+ # exchange metadata: the composite must retain every sub-engine report.+ class OtherDiagnostics:+ def _engine_diagnostics(self):+ return {"pricing_assumptions": [{"product": "ZZ", "field": "tick", "value": 1}]}+ engine._rule_engines["synthetic"] = OtherDiagnostics()+ report = engine._engine_diagnostics()+ assert report["lot_assumptions"] == hk._engine_diagnostics()["lot_assumptions"]+ assert report["pricing_assumptions"] == [{"product": "ZZ", "field": "tick", "value": 1}]+ assert "hk_equity" in report["market_diagnostics"]++[email protected]("composite", [False, True])+def test_local_csv_run_persists_lot_assumptions(tmp_path, monkeypatch, composite):+ """Actual CSV ingestion and artifact writing preserve sizing diagnostics."""+ import json++ from backtest.engines.composite import CompositeEngine+ from backtest.loaders import local_loader++ codes = ["00001.HK", "00002.HK"]+ dates = pd.date_range("2026-01-05", periods=4, freq="B")+ sources = []+ for symbol in codes:+ path = tmp_path / f"{symbol}.csv"+ pd.DataFrame({"date": dates, "open": 10, "high": 10, "low": 10,+ "close": 10, "volume": 100_000}).to_csv(path, index=False)+ sources.append({"symbol": symbol, "type": "csv", "path": str(path)})+ config_path = tmp_path / "data-bridge.yaml"+ config_path.write_text(json.dumps({"sources": sources}), encoding="utf-8")+ monkeypatch.setattr(local_loader, "_CONFIG_PATH", config_path)+ config = {"codes": codes, "initial_cash": 100_000, "source": "local",+ "start_date": "2026-01-05", "end_date": "2026-01-08",+ "hk_lot_sizes": {"00001.HK": 20}}++ class Signals:+ def generate(self, frames):+ return {symbol: pd.Series(1.0, index=frame.index)+ for symbol, frame in frames.items()}++ engine = CompositeEngine(config, codes) if composite else GlobalEquityEngine(config, market="hk")+ run_dir = tmp_path / "run"+ engine.run_backtest(config, local_loader.DataLoader(), Signals(), run_dir)+ fills = [json.loads(line) for line in (run_dir / "artifacts/fills.jsonl").read_text().splitlines()]+ opened = [fill for fill in fills if fill["action"] == "open"]+ assert {fill["symbol"] for fill in opened} == set(codes)+ assert all(abs(fill["signed_quantity"]) % (20 if fill["symbol"] == codes[0] else 100) == 0+ for fill in opened)+ card = json.loads((run_dir / "run_card.json").read_text())+ expected = [{"symbol": "0002.HK", "field": "board_lot", "value": 100}]+ assert card["structured_metrics"]["lot_assumptions"] == expected+ if composite:+ assert card["structured_metrics"]["market_diagnostics"]["hk_equity"]["lot_assumptions"] == expecteddiff --git a/agent/tests/test_signal_alignment_perf.py b/agent/tests/test_signal_alignment_perf.pyindex 9f32c5729..8ceb0c69f 100644--- a/agent/tests/test_signal_alignment_perf.py+++ b/agent/tests/test_signal_alignment_perf.py@@ -833,3 +833,32 @@ class TestFundPanelCompatibility:assert not (close_df == sentinel).any().any(), ("Sentinel 999.0 from fund:* columns leaked into close matrix")++[email protected]("unit", ["ns", "us", "ms"])[email protected]("tz", [None, "Asia/Hong_Kong"])+def test_reused_calendar_mapping_preserves_own_bar_lag_after_column_drop(unit, tz):+ """Shared and irregular calendars retain exact dates, marks and signal lag."""+ dates = pd.bdate_range("2026-01-05", periods=4, tz=tz).as_unit("ns")+ sparse_dates = dates[[0, 2]].as_unit(unit)+ codes = ["EMPTY.SZ", "000001.SZ", "600519.SH"]+ frames = {+ codes[0]: pd.DataFrame({"close": np.nan}, index=dates),+ codes[1]: pd.DataFrame({"close": [10., 11., 12., 13.]}, index=dates),+ codes[2]: pd.DataFrame({"close": [20., 22.]}, index=sparse_dates),+ }+ signals = {+ codes[0]: pd.Series(1., index=dates),+ codes[1]: pd.Series([1., 0., -1., 1.], index=dates),+ codes[2]: pd.Series([1., -1.], index=sparse_dates),+ }+ actual_dates, close, valuation, positions, returns = _align(frames, signals, codes)+ pd.testing.assert_index_equal(actual_dates, dates)+ expected_close = pd.DataFrame({codes[1]: [10., 11., 12., 13.],+ codes[2]: [20., 20., 22., 22.]}, index=dates)+ expected_positions = pd.DataFrame({codes[1]: [0., 1., 0., -0.5],+ codes[2]: [0., 0., 1., 0.5]}, index=dates)+ pd.testing.assert_frame_equal(close, expected_close, check_freq=False)+ pd.testing.assert_frame_equal(valuation, expected_close, check_freq=False)+ pd.testing.assert_frame_equal(positions, expected_positions, check_freq=False)+ assert returns[codes[2]].tolist() == pytest.approx([0., 0., 0.1, 0.])