Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(runner): the spacing check pins its own properties, reaches the run card, and annualises a weekly file from the calendar (#1432)

Haozhe Wu9d72ce970bd4
diff --git a/agent/backtest/engines/base.py b/agent/backtest/engines/base.py
index 7619615ac..4cb57ebf4 100644
--- a/agent/backtest/engines/base.py
+++ b/agent/backtest/engines/base.py
@@ -1138,6 +1138,8 @@ class BaseEngine(ABC):
card_warnings = list(config.get("content_filter_warnings") or [])
if config.get("_run_card_caliber_warning"):
card_warnings.append(config["_run_card_caliber_warning"])
+ if config.get("_run_card_annualisation_warning"):
+ card_warnings.append(config["_run_card_annualisation_warning"])
from backtest.run_card import write_run_card
write_run_card(
run_dir,
diff --git a/agent/backtest/runner.py b/agent/backtest/runner.py
index 30d300dee..4071fac15 100644
--- a/agent/backtest/runner.py
+++ b/agent/backtest/runner.py
@@ -1288,9 +1288,12 @@ def main(run_dir: Path) -> None:
if len(market_types) > 1:
bars_per_year = None
else:
+ annualisation_warnings: list[str] = []
bars_per_year = _annualisation_bars(
- interval, effective_source, data_map, codes
+ interval, effective_source, data_map, codes, warnings=annualisation_warnings
)
+ if annualisation_warnings:
+ config["_run_card_annualisation_warning"] = annualisation_warnings[0]
# Every source has already been fetched, sanitized, and enriched above.
# Reuse that exact snapshot so provider costs and run-card provenance stay
@@ -1330,6 +1333,13 @@ _SPACING_MISMATCH_RATIO = 1.5
#: (``[1, 1, 3]`` -> 1 day); two do not (``[1, 3]`` -> 2 days).
_MIN_BARS_FOR_SPACING = 4
+#: Spacing at least this many times a day is a weekly or monthly series, not a
+#: daily one with gaps: a daily index over a holiday week measures a median of
+#: two or three days, a weekly file seven. Such a series has no trading-day
+#: table to look up and needs none -- its bars per year is the calendar's.
+_WIDER_THAN_DAILY_RATIO = 4.0
+_CALENDAR_YEAR_SECONDS = 365.25 * 86_400.0
+
def _observed_spacing(data_map: dict, codes: List[str]) -> float | None:
"""Median spacing of the served price bars in seconds, or None when
@@ -1356,7 +1366,11 @@ def _observed_spacing(data_map: dict, codes: List[str]) -> float | None:
def _annualisation_bars(
- interval: str, source: str, data_map: dict, codes: List[str]
+ interval: str,
+ source: str,
+ data_map: dict,
+ codes: List[str],
+ warnings: list[str] | None = None,
) -> int:
"""Bars per year for a single-market run, checked against the served bars.
@@ -1379,19 +1393,30 @@ def _annualisation_bars(
:func:`~backtest.metrics.calc_bars_per_year` -- looked up with the interval
the spacing actually matches -- so the per-source trading-day table keeps
producing the number and a run card never picks up a window-dependent one.
+ Bars spaced wider than any supported interval (a weekly or monthly file)
+ are annualised from the calendar instead, 52 for a weekly series; a
+ spacing that matches nothing in either direction keeps the declaration.
Args:
interval: Bar size the caller declared.
source: Primary source name, for the per-source trading-day table.
data_map: Fetched ``code -> frame`` map.
codes: The instrument codes.
+ warnings: When given, a mismatch report is appended here as well as
+ logged, so the run card carries it next to the caliber warning.
Returns:
- Bars per year for the declared interval, or for the interval the served
- spacing matches when the two disagree.
+ Bars per year for the declared interval; for the interval the served
+ spacing matches when the two disagree; or the calendar count of a
+ spacing wider than every supported interval.
"""
from backtest.metrics import _normalize_interval, calc_bars_per_year
+ def _report(message: str) -> None:
+ logger.warning("%s", message)
+ if warnings is not None:
+ warnings.append(message)
+
declared = calc_bars_per_year(interval, source)
declared_spacing = _INTERVAL_SECONDS.get(_normalize_interval(interval))
observed = _observed_spacing(data_map, codes)
@@ -1407,27 +1432,34 @@ def _annualisation_bars(
)
matched_spacing = _INTERVAL_SECONDS[matched]
if max(matched_spacing, observed) / min(matched_spacing, observed) >= _SPACING_MISMATCH_RATIO:
- # Spacing that is no supported interval (a weekly file, say). Naming a
- # bar count for it would be a guess, so the declaration stands and the
- # mismatch is reported instead.
- logger.warning(
- "interval=%s declares bars spaced %.0fs but the served data is "
- "spaced %.0fs, which matches no supported interval; annualising at "
- "the declared rate. Check the earlier loader warning and re-run at "
- "the granularity the source actually has.",
- interval, declared_spacing, observed,
+ if observed >= _WIDER_THAN_DAILY_RATIO * _INTERVAL_SECONDS["1D"]:
+ calendar_bars = max(1, round(_CALENDAR_YEAR_SECONDS / observed))
+ _report(
+ f"interval={interval} declares bars spaced {declared_spacing:.0f}s but the "
+ f"served data is spaced {observed:.0f}s, wider than any supported interval; "
+ f"annualising at {calendar_bars} bars/year from that spacing instead of "
+ f"{declared}."
+ )
+ return calendar_bars
+ # Between two supported intervals, or finer than a minute: no count to
+ # look up, so the declaration stands. A daily series over a holiday
+ # week lands here with a two-day median, so the report states the
+ # spacings and nothing more.
+ _report(
+ f"interval={interval} declares bars spaced {declared_spacing:.0f}s but the "
+ f"served data is spaced {observed:.0f}s, which matches no supported interval; "
+ f"annualising at the declared rate ({declared} bars/year)."
)
return declared
- logger.warning(
- "interval=%s declares bars spaced %.0fs but the served data is spaced "
- "%.0fs; annualising as %s (%d bars/year) instead of %d. The loader returned "
- "bars coarser or finer than requested -- check the earlier loader "
- "warning, and set interval to the granularity the source actually has.",
- interval, declared_spacing, observed, matched,
- calc_bars_per_year(matched, source), declared,
+ resolved = calc_bars_per_year(matched, source)
+ _report(
+ f"interval={interval} declares bars spaced {declared_spacing:.0f}s but the served "
+ f"data is spaced {observed:.0f}s; annualising as {matched} ({resolved} bars/year) "
+ f"instead of {declared}. The loader served bars coarser or finer than requested; "
+ f"set interval to the granularity the source has if that was not intended."
)
- return calc_bars_per_year(matched, source)
+ return resolved
def _create_market_engine(source: str, config: dict, codes: List[str]):
diff --git a/agent/tests/test_cross_market_annualization.py b/agent/tests/test_cross_market_annualization.py
index d97a8be31..27c9557ae 100644
--- a/agent/tests/test_cross_market_annualization.py
+++ b/agent/tests/test_cross_market_annualization.py
@@ -251,17 +251,140 @@ class TestSingleMarketAnnualisationChecksTheServedData:
)
assert _annualisation_bars("1D", "tushare", data, ["600519.SH"]) == 252
- def test_unsupported_spacing_keeps_the_declaration_and_says_so(self, caplog):
- """Weekly bars match no supported interval; naming a count would guess."""
+ # --- spacing wider than any supported interval (review point 2) ---
+
+ @pytest.mark.parametrize("declared", ["1D", "1H"])
+ def test_weekly_bars_are_annualised_from_the_calendar(self, declared, caplog):
+ """A weekly file has no trading-day table and needs none: 52 a year.
+
+ Keeping the declaration was the same bug one step coarser -- a weekly
+ file declared ``1H`` annualised at 1,764 bars a year.
+ """
from backtest.runner import _annualisation_bars
data = self._frame(pd.date_range("2024-01-05", periods=60, freq="W-FRI"))
with caplog.at_level("WARNING", logger="backtest.runner"):
- resolved = _annualisation_bars("1D", "tushare", data, ["600519.SH"])
+ resolved = _annualisation_bars(declared, "tushare", data, ["600519.SH"])
+
+ assert resolved == 52
+ assert any("wider than any supported interval" in r.getMessage() for r in caplog.records)
+
+ def test_monthly_bars_are_twelve_a_year(self):
+ from backtest.runner import _annualisation_bars
+
+ data = self._frame(pd.date_range("2020-01-01", periods=48, freq="MS"))
+ assert _annualisation_bars("1D", "yahoo", data, ["600519.SH"]) == 12
+
+ def test_a_daily_series_over_a_holiday_week_is_not_read_as_weekly(self, caplog):
+ """Five daily bars around Christmas measure a two-day median: the
+ declaration stands, the report states the spacings, and the count is
+ not recomputed from a spacing that is only gaps."""
+ from backtest.runner import _annualisation_bars
+
+ index = pd.to_datetime(["2025-12-22", "2025-12-23", "2025-12-24", "2025-12-26", "2025-12-29"])
+ with caplog.at_level("WARNING", logger="backtest.runner"):
+ resolved = _annualisation_bars("1D", "yahoo", self._frame(index), ["600519.SH"])
assert resolved == 252
+ message = " ".join(r.getMessage() for r in caplog.records)
+ assert "matches no supported interval" in message
+ assert "re-run" not in message
+
+ def test_sub_minute_bars_keep_the_declaration_and_say_so(self, caplog):
+ from backtest.runner import _annualisation_bars
+
+ data = self._frame(pd.date_range("2026-09-07 09:30", periods=200, freq="10s"))
+ with caplog.at_level("WARNING", logger="backtest.runner"):
+ resolved = _annualisation_bars("1m", "yahoo", data, ["600519.SH"])
+
+ assert resolved == calc_bars_per_year("1m", "yahoo")
assert any("matches no supported interval" in r.getMessage() for r in caplog.records)
+ # --- properties the code relies on, each pinned against its mutation ---
+
+ def test_hourly_bars_declared_30m_switch_to_the_hourly_count(self):
+ """Neighbouring intervals differ by 2x, above the 1.5 gate."""
+ from backtest.runner import _annualisation_bars
+
+ data = self._frame(self._session(days=5, per_day=7, freq="1h"))
+ assert _annualisation_bars("30m", "yahoo", data, ["600519.SH"]) == calc_bars_per_year("1H", "yahoo")
+
+ def test_four_hour_bars_declared_1h_switch_to_the_four_hour_count(self):
+ """A 4x mismatch must switch too: the gate is 1.5, not a larger number."""
+ from backtest.runner import _annualisation_bars
+
+ data = self._frame(self._session(days=5, per_day=2, freq="4h"))
+ assert _annualisation_bars("1H", "yahoo", data, ["600519.SH"]) == calc_bars_per_year("4H", "yahoo")
+
+ def test_spacing_inside_the_tolerance_keeps_the_declaration(self):
+ """Bars 72 minutes apart declared 1H sit at ratio 1.2: not a mismatch."""
+ from backtest.runner import _annualisation_bars
+
+ data = self._frame(self._session(days=5, per_day=5, freq="72min"))
+ assert _annualisation_bars("1H", "yahoo", data, ["600519.SH"]) == calc_bars_per_year("1H", "yahoo")
+
+ def test_spacing_near_a_neighbouring_interval_resolves_to_it(self):
+ """Bars 72 minutes apart declared 30m are a mismatch (ratio 2.4) whose
+ nearest interval, 1H, sits inside the tolerance (ratio 1.2): the run
+ annualises as 1H. A tighter gate would find no match and keep 30m."""
+ from backtest.runner import _annualisation_bars
+
+ data = self._frame(self._session(days=5, per_day=5, freq="72min"))
+ assert _annualisation_bars("30m", "yahoo", data, ["600519.SH"]) == calc_bars_per_year("1H", "yahoo")
+
+ def test_bars_finer_than_declared_switch_as_well(self):
+ """The gate is two-sided: hourly bars declared 1D annualise as 1H."""
+ from backtest.runner import _annualisation_bars
+
+ data = self._frame(self._session(days=5, per_day=7, freq="1h"))
+ assert _annualisation_bars("1D", "yahoo", data, ["600519.SH"]) == calc_bars_per_year("1H", "yahoo")
+
+ def test_three_bars_are_too_few_to_overrule_the_declaration(self):
+ """Two differences cannot outvote one gap, so the declaration stands."""
+ from backtest.runner import _annualisation_bars
+
+ data = self._frame(pd.bdate_range("2026-09-08", periods=3))
+ assert _annualisation_bars("1H", "yahoo", data, ["600519.SH"]) == calc_bars_per_year("1H", "yahoo")
+
+ def test_only_price_frames_are_measured_even_when_a_panel_is_longer(self):
+ """A longer injected panel must not win the measurement by length."""
+ from backtest.runner import _annualisation_bars
+
+ data = self._frame(pd.date_range("2024-01-02", periods=654, freq="B"))
+ data["_fundamentals"] = pd.DataFrame(
+ {"pe": [1.0] * 1000}, index=pd.date_range("2024-01-02", periods=1000, freq="h")
+ )
+ assert _annualisation_bars("1D", "tushare", data, ["600519.SH"]) == 252
+
+ def test_the_longest_price_frame_decides(self):
+ """A short hourly stub beside a long daily series does not switch the run."""
+ from backtest.runner import _annualisation_bars
+
+ data = self._frame(pd.date_range("2024-01-02", periods=654, freq="B"))
+ data["000001.SZ"] = pd.DataFrame(
+ {"close": [10.0] * 10}, index=pd.date_range("2024-01-02 09:30", periods=10, freq="h")
+ )
+ assert _annualisation_bars("1D", "tushare", data, ["600519.SH", "000001.SZ"]) == 252
+
+ def test_the_report_is_handed_to_the_caller_for_the_run_card(self):
+ from backtest.runner import _annualisation_bars
+
+ data = self._frame(pd.date_range("2024-01-02", periods=654, freq="B"))
+ warnings: list[str] = []
+ resolved = _annualisation_bars("1H", "tushare", data, ["600519.SH"], warnings=warnings)
+
+ assert resolved == 252
+ assert len(warnings) == 1 and "annualising as 1D" in warnings[0]
+
+ def test_a_clean_run_hands_over_no_report(self):
+ from backtest.runner import _annualisation_bars
+
+ data = self._frame(pd.date_range("2024-01-02", periods=654, freq="B"))
+ warnings: list[str] = []
+ _annualisation_bars("1D", "tushare", data, ["600519.SH"], warnings=warnings)
+
+ assert warnings == []
+
def test_mismatch_is_logged(self, caplog):
from backtest.runner import _annualisation_bars