Handle backtest calendar right boundary#2282
Open
Kevin-Li-2025 wants to merge 1 commit into
Open
Conversation
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
Fixes #2278.
TradeCalendarManager.get_step_time()forms each step interval as[calendar[i], epsilon_change(calendar[i + 1])]. When a provider has no future calendar extension and a backtest ends on the final available calendar bar, the last step has nocalendar[i + 1]and raises an opaqueIndexError.This PR keeps the normal next-calendar path unchanged, but when the next calendar bar is unavailable it infers the right endpoint from the manager frequency (
calendar[i] + freq) and then applies the existingepsilon_changeclosed-interval convention. For a daily final bar, the last interval becomes2020-01-02 00:00:00through2020-01-02 23:59:59instead of crashing.Tests
PYTHONPYCACHEPREFIX=/private/tmp/qlib-pycache /usr/bin/python3 -m py_compile qlib/backtest/utils.py tests/backtest/test_trade_calendar_manager.pygit diff --check/opt/homebrew/bin/python3.11 -m ruff check tests/backtest/test_trade_calendar_manager.pyqlib/backtest/utils.py, and verifies both the normal next-calendar path and the final-calendar fallbackNote: direct local
pytest -q tests/backtest/test_trade_calendar_manager.pyis blocked in this unbuilt source checkout by missing compiled extensions (qlib.data._libs.rolling). The added test is a normal repository test and should run after CI builds the extension.