Repository navigation
fix(backtest): keep trade review candles on the strategy timeframe - #368
Merged
brokermr810 merged 1 commit intoNov 13, 2026
Conversation
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>
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
brokermr810
approved these changes
Nov 13, 2026
brokermr810
left a comment
Collaborator
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
The trade review chart in the backtest workbench draws the candles stored in
result.reviewCandles[symbol]and uses that snapshot'stimeframeas the chart period._build_review_candle_snapshotspicked 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
3mwas missing from the review frequency table.Related issue
Changes
app/services/strategy_v2/service.py_build_review_candle_snapshotsalways resamples at the strategy's driving frequency instead of calling_review_frequency_for_window.iloc[-max_bars:]tail crop keeps the most recent trades.("3m", "3min", 180)to_REVIEW_FREQUENCIES. Before this,_review_frequency_seconds("3m")fell back to 86400, so 3m strategies were reviewed on 1D candles._review_frequency_specand_review_window_fits._review_frequency_for_windowis unchanged and still picks the benchmark fetch frequency.tests/test_strategy_v2_service.py..._uses_the_finest_bounded_timeframe...test encoded the old coarsening, so I replaced it withtest_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
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.reviewCandlesshape (timeframeandcandles) is the same. History records that were saved before this change keep their stored timeframe.Test plan
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).tests/test_strategy_v2*.py tests/test_backtest*.py -m "not integration and not stress"gives 371 passed. Dependencies were installed as inbasic-ci.yml(requirements.lock --no-depsplusrequirements-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.pyandscripts/check_docs.pypass.StrategyV2BacktestService.runwith a 15mCrypto:BTC/USDTstrategy 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)
docs/api/openapi.yaml: not needed, no route or schema changesdocs/agent/agent-openapi.json: not neededScreenshots (if UI change)
The fix is backend only. The chart takes its period from
reviewCandles[symbol].timeframe, so it now matches the strategy timeframe.