Skip to content

fix(backtest): keep trade review candles on the strategy timeframe - #368

Merged
brokermr810 merged 1 commit into
OpenByteInc:mainfrom
Mystery01092000:fix/backtest-review-strategy-timeframe
Nov 13, 2026
Merged

brokermr810 merged 1 commit into
OpenByteInc:mainfrom
Mystery01092000:fix/backtest-review-strategy-timeframe

Conversation

@Mystery01092000

Copy link
Copy Markdown
Contributor

Summary

The trade review chart in the backtest workbench draws the candles stored in result.reviewCandles[symbol] and uses that snapshot's timeframe as the chart period. _build_review_candle_snapshots picked the finest timeframe whose bars could cover the whole requested backtest range within 3000 bars. Once a 15m backtest ran longer than about 31 days, the snapshot switched to 30m (1m switched after about 2 days, 1H after about 125 days). Entries and exits then landed on coarser bars than the strategy actually ran on, which is what #328 reports.

This PR keeps the review snapshot on the strategy's driving frequency. It also fixes 3m strategies, which were reviewed on daily candles because 3m was missing from the review frequency table.

Related issue

Changes

  • app/services/strategy_v2/service.py
    • _build_review_candle_snapshots always resamples at the strategy's driving frequency instead of calling _review_frequency_for_window.
    • If the full requested range fits the 3000-bar budget at that frequency, nothing changes: the whole range is kept.
    • If it doesn't fit, the window is narrowed to the span that contains this symbol's trades (plus 8 bars of padding on each side, limited to the requested range). If that span is still too long, the existing iloc[-max_bars:] tail crop keeps the most recent trades.
    • Added ("3m", "3min", 180) to _REVIEW_FREQUENCIES. Before this, _review_frequency_seconds("3m") fell back to 86400, so 3m strategies were reviewed on 1D candles.
    • Small helpers _review_frequency_spec and _review_window_fits. _review_frequency_for_window is unchanged and still picks the benchmark fetch frequency.
  • tests/test_strategy_v2_service.py
    • The existing ..._uses_the_finest_bounded_timeframe... test encoded the old coarsening, so I replaced it with test_trade_review_snapshot_keeps_the_strategy_timeframe_and_only_ohlcv. It still checks that only OHLCV fields are kept.
    • test_fifteen_minute_strategy_review_is_not_compressed_to_thirty_minutes: regression for 回测工作台的交易复盘周期跟随策略运行周期 #328. It runs a two-month 15m backtest and checks that the candles are 15m apart and cover every entry and exit.
    • test_trade_review_snapshot_keeps_the_full_requested_range_when_it_fits: checks the unchanged path.
    • test_three_minute_strategy_review_uses_three_minute_candles.

Behavior notes

  • The delivered and stored results are already capped at 1500 review candles per symbol by compact_backtest_result. Before this change, a long backtest therefore already lost its earliest trades from the review chart, just on coarser candles. After this change, the chart keeps the strategy timeframe but shows a shorter time span when the trades cover more than 1500 bars. In a 15m backtest from 2026-06-01 to 2026-07-31 with 70 trades, the old snapshot was 30m from 2026-06-30 onward. The new one is 15m from 2026-07-13 to the last exit.
  • No API, schema or migration changes. The reviewCandles shape (timeframe and candles) is the same. History records that were saved before this change keep their stored timeframe.
  • Possible follow-up for the frontend repo: lazily load older candles at the snapshot timeframe when the user scrolls past the start of the snapshot.

Test plan

  • Tested locally with docker compose up -d --build: not done. The stack needs live market data to produce a backtest, so I verified through the service instead (see below).
  • Relevant pytest tests pass: tests/test_strategy_v2*.py tests/test_backtest*.py -m "not integration and not stress" gives 371 passed. Dependencies were installed as in basic-ci.yml (requirements.lock --no-deps plus requirements-dev.txt, Python 3.12). On the base commit, 3 of the new tests fail: the 1m, 15m (回测工作台的交易复盘周期跟随策略运行周期 #328) and 3m cases.
  • ruff check app scripts tests, compileall, scripts/backend_quality_check.py and scripts/check_docs.py pass.
  • Service-level check: I ran StrategyV2BacktestService.run with a 15m Crypto:BTC/USDT strategy over 2026-06-01 to 2026-07-31 (synthetic candles, 70 closed trades). Review snapshot before: timeframe 30m. After: timeframe 15m.

API documentation (if routes/schemas changed)

  • Regenerated docs/api/openapi.yaml: not needed, no route or schema changes
  • Updated docs/agent/agent-openapi.json: not needed
  • Breaking API changes: none

Screenshots (if UI change)

The fix is backend only. The chart takes its period from reviewCandles[symbol].timeframe, so it now matches the strategy timeframe.

The backtest trade review snapshot picked the finest timeframe whose bars
covered the whole requested range within 3000 bars, so a 15m strategy
backtested over more than about a month was reviewed on 30m candles and
entries and exits were squeezed onto coarser bars.

Review candles now always use the strategy's driving frequency. When the
full range does not fit the bar budget, the window is focused on the span
that contains trades, and the existing tail crop keeps the most recent
trades if that span is still too long. 3m strategies were also reviewed
on daily candles because 3m was missing from the review frequency table.

Closes OpenByteInc#328

Signed-off-by: Pranav Tomar <pranavtom1223@gmail.com>
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@brokermr810 brokermr810 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed trade-review timeframe preservation, 3m support, and coverage. This fixes the confirmed 15m-to-30m compression issue. The intentional boundary is that very long exact-timeframe reviews use a trade-focused window and the 3000-bar cap can omit older trades. Backend tests and security checks passed; the pip-audit failure is the pre-existing soupsieve 2.9 baseline on main.

@brokermr810
brokermr810 merged commit 7e8513b into OpenByteInc:main Nov 13, 2026
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

回测工作台的交易复盘周期跟随策略运行周期

2 participants