Skip to content

Fix/rl alignment assertion error 1972#2306

Open
Vedantvijayhumbe wants to merge 3 commits into
microsoft:mainfrom
Vedantvijayhumbe:fix/rl-alignment-assertion-error-1972
Open

Fix/rl alignment assertion error 1972#2306
Vedantvijayhumbe wants to merge 3 commits into
microsoft:mainfrom
Vedantvijayhumbe:fix/rl-alignment-assertion-error-1972

Conversation

@Vedantvijayhumbe

Copy link
Copy Markdown

Description

Motivation and Context

How Has This Been Tested?

  • Pass the test by running: pytest qlib/tests/test_all_pipeline.py under upper directory of qlib.
  • If you are adding a new feature, test on your own test scripts.

Screenshots of Test Results (if appropriate):

  1. Pipeline test:
  2. Your own tests:

Types of changes

  • Fix bugs
  • Add new feature
  • Update documentation

Vedant and others added 3 commits July 19, 2026 17:15
…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
…x alignment

Fixes microsoft#1972

## Problem

In , two bare  statements
guarded the requirement that  and
 must each be divisible by :

    assert data_config["source"]["default_start_time_index"] % data_granularity == 0
    assert data_config["source"]["default_end_time_index"]   % data_granularity == 0

When a user supplied a misaligned value (e.g.
with ), they received a bare  with no
message, making it impossible to understand what went wrong or how to fix it.

## Changes

### qlib/rl/contrib/_utils.py  (new)
Extracts the validation logic into a standalone
function that lives in a module with **zero** heavy dependencies
(no tianshou, torch, gym).  This allows the function to be unit-tested in
any CI environment regardless of which optional packages are installed.

### qlib/rl/contrib/train_onpolicy.py
- Imports  from  and calls it instead of
  the bare asserts.
- Adds a comprehensive NumPy-style docstring to  that
  explicitly documents the alignment constraint with examples, a
  block, and a  section.

### docs/component/rl/quickstart.rst
- Fixes two wrong config key names in the example YAML
  ( → ,
    → ,
    → ).
- Adds a  admonition explaining the alignment rule and
  providing concrete examples for different granularities.
- Fixes the  invocation commands (removed spurious  suffix).

### tests/rl/test_train_onpolicy_alignment.py  (new)
14 unit tests covering valid and invalid configurations.  Tests verify that:
- Valid index/granularity pairs do not raise.
- Misaligned indices raise  (not ).
- Error messages contain the actual values, the modulo remainder, and a
  suggested correction.
-  errors are reported before
  errors when both are wrong.
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.

1 participant