From ebbe2ee5d7d24b4087f6af58ddeb5958c0f34edc Mon Sep 17 00:00:00 2001 From: Akshay Kumar Date: Thu, 20 Aug 2026 22:49:08 +0530 Subject: [PATCH] fix(backtest): clamp right endpoint at calendar boundary to prevent IndexError --- qlib/backtest/utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/qlib/backtest/utils.py b/qlib/backtest/utils.py index 4210c9548a8..ba5e69e4a1d 100644 --- a/qlib/backtest/utils.py +++ b/qlib/backtest/utils.py @@ -128,7 +128,16 @@ def get_step_time(self, trade_step: int | None = None, shift: int = 0) -> Tuple[ if trade_step is None: trade_step = self.get_trade_step() calendar_index = self.start_index + trade_step - shift - return self._calendar[calendar_index], epsilon_change(self._calendar[calendar_index + 1]) + if calendar_index + 1 < len(self._calendar): + right_time = self._calendar[calendar_index + 1] + else: + delta = ( + self._calendar[calendar_index] - self._calendar[calendar_index - 1] + if calendar_index > 0 + else pd.Timedelta(days=1) + ) + right_time = self._calendar[calendar_index] + delta + return self._calendar[calendar_index], epsilon_change(right_time) def get_data_cal_range(self, rtype: str = "full") -> Tuple[int, int]: """