fix: guard calendar[index+1] peek in get_step_time for last bar (#2278)#2304
Open
Vedantvijayhumbe wants to merge 1 commit into
Open
fix: guard calendar[index+1] peek in get_step_time for last bar (#2278)#2304Vedantvijayhumbe wants to merge 1 commit into
Vedantvijayhumbe wants to merge 1 commit into
Conversation
…osoft#2278) When end_time falls on the final calendar entry and no future calendar is available, calendar_index + 1 is out of bounds, crashing the backtest with an opaque IndexError. Guard the peek: if calendar_index + 1 < len(self._calendar) use the next bar as before; otherwise compute the right endpoint as left + one freq-period, mirroring the day_start + Timedelta(days=1) idiom already used in get_data_cal_range. epsilon_change is applied to whichever right is chosen, so the closed-interval convention is preserved on both paths. Added tests/backtest/test_calendar_boundary.py covering: - last-bar step does not raise - day and minute fallback values - pre-boundary steps are unchanged - full loop terminates with the correct step count
Author
|
@zhaow-de can you help me with the license/cla |
Author
|
@microsoft-github-policy-service agree |
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.
fix(backtest): resolve IndexError at the right calendar boundary (#2278)
Description
When
end_timefalls on the final calendar entry and no future calendar is available,calendar_index + 1evaluates out of bounds. This results in the backtest crashing with an opaqueIndexError.This PR guards the calendar peek in
TradeCalendarManager.get_step_time. The new logic operates as follows:calendar_index + 1 < len(self._calendar), it uses the next bar as before.left + one freq-period, which mirrors theday_start + Timedelta(days=1)idiom already used inget_data_cal_range.epsilon_changeis then applied to whichever right endpoint is chosen, ensuring the closed-interval convention is preserved across both execution paths.Motivation and Context
Resolves Issue: Fixes #2278
Previously, backtests traversing the entire dataset would fail on the very last trading step. This change ensures that backtests can safely process the final bar without breaking frequency assumptions or missing final portfolio calculations.
How Has This Been Tested?
pytest qlib/tests/test_all_pipeline.pyunder upper directory ofqlib.Specific Tests Added:
Added
tests/backtest/test_calendar_boundary.pywhich explicitly covers:IndexError.Types of changes