feat(backtest): show each cell as it starts, and the run id to continue with

- run_backtest takes a progress(done, total, ticker, date) callback; tradingagents backtest prints [n/total] per cell and ends with the --run-id that continues or settles the sweep
This commit is contained in:
Yijia-Xiao
2026-09-29 05:57:17 +00:00
parent 052a98d6eb
commit f4828b84ea
4 changed files with 59 additions and 13 deletions
+22
View File
@@ -274,3 +274,25 @@ def test_a_result_whose_cells_all_failed_summarizes_as_empty(tmp_path):
result = BacktestResult(run_id="r", log_path=tmp_path / "never-written.md")
assert summarize(result).resolved == 0
@pytest.mark.unit
def test_progress_is_reported_before_each_cell(tmp_path):
seen = []
run_backtest(["NVDA", "AAPL"], ["2026-01-05", "2026-01-12"], _config(tmp_path),
progress=lambda done, total, ticker, date: seen.append((done, total, ticker, date)))
assert seen == [(1, 4, "NVDA", "2026-01-05"), (2, 4, "NVDA", "2026-01-12"),
(3, 4, "AAPL", "2026-01-05"), (4, 4, "AAPL", "2026-01-12")]
@pytest.mark.unit
def test_a_resumed_sweep_reports_only_the_cells_it_runs(tmp_path):
first = run_backtest(["NVDA"], ["2026-01-05"], _config(tmp_path))
seen = []
run_backtest(["NVDA"], ["2026-01-05", "2026-01-12"], _config(tmp_path), run_id=first.run_id,
progress=lambda done, total, ticker, date: seen.append((done, total, date)))
assert seen == [(1, 1, "2026-01-12")]
+16
View File
@@ -135,3 +135,19 @@ def test_backtest_reports_a_setup_failure_in_one_line(runner, monkeypatch):
assert result.exit_code == 1
assert "API key" in result.output
assert "Traceback" not in result.output
@pytest.mark.unit
def test_backtest_shows_each_cell_and_how_to_continue(runner, monkeypatch, tmp_path):
def sweep(tickers, dates, config, progress=None, **kw):
for i, date in enumerate(dates, 1):
progress(i, len(dates), tickers[0], date)
return _Result(tmp_path)
monkeypatch.setattr(m, "run_backtest", sweep)
monkeypatch.setattr(m, "summarize", lambda log: _Summary())
result = runner.invoke(m.app, ["backtest", "NVDA", "--start", "2026-06-01", "--end", "2026-06-08"])
assert "[1/2] NVDA 2026-06-01" in result.output and "[2/2] NVDA 2026-06-08" in result.output
assert f"--run-id {_Result(tmp_path).run_id}" in result.output