Skip to content

fix(factors): align close and volume indices before ratio in _amihud_illiquidity - #385

Open
huuan26 wants to merge 1 commit into
OpenByteInc:mainfrom
huuan26:fix/amihud-illiquidity-index-alignment
Open

huuan26 wants to merge 1 commit into
OpenByteInc:mainfrom
huuan26:fix/amihud-illiquidity-index-alignment

Conversation

@huuan26

@huuan26 huuan26 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix close/volume index misalignment and ValueError: operands could not be broadcast together crashes in _amihud_illiquidity when frame["close"] contains NaN or non-finite values, and handle zero-volume or non-positive traded-value bars cleanly without emitting runtime divide-by-zero warnings.

Previously, _amihud_illiquidity dropped missing entries from close and volume independently (close = _numeric_series(...) vs. volume = _numeric_series(...)). Whenever a bar had a valid volume and a missing close (or trailing NaN close at the end of a rolling cross-sectional window), taking the last period elements from each vector misaligned timestamps or caused unequal-length broadcasting errors that could abort cross-factor correlation checks inside FactorResearchEngine.run.

Changes

  • backend_api_python/app/services/factors/registry.py: in _amihud_illiquidity, coerce close and volume with non-finite values mapped to NaN, filter both series through the same valid_close = raw_close.notna().to_numpy() mask, align prev_close, curr_close, and curr_volume on identical valid-close bar slices, compute abs_returns / np.where(traded_value > 0.0, traded_value, np.nan) under np.errstate(divide="ignore", invalid="ignore"), and return float("nan") if no finite daily ratio remains.
  • backend_api_python/tests/test_factor_registry.py: add test_amihud_illiquidity_aligns_close_and_volume_with_missing_or_zero_bars covering trailing NaN closes, internal NaN closes paired with distinct volumes, all-zero volumes, and insufficient valid closes (factor.insufficientHistory).
  • backend_api_python/tests/test_factor_research.py: add test_factor_research_correlation_survives_nan_close_in_amihud_illiquidity regression test confirming FactorResearchEngine.run retains amihud_illiquidity in factorCorrelation when a constituent has a NaN close on a research date.

Test plan

  • Tested locally with docker compose up -d --build (not run; pure unit/factor calculation fix)
  • Backend logs show no errors
  • Relevant pytest tests pass:
    • pytest tests/test_factor_registry.py tests/test_factor_research.py tests/test_qlib_factor_adapter.py -v (59 passed, 1 skipped; test_factor_research_builds_talib_081_panel_with_selected_parameters skipped locally because optional talib is not installed)
    • coverage run --branch --include="app/services/factors/registry.py" -m pytest tests/test_factor_registry.py tests/test_factor_research.py -q (28 passed, 1 skipped; zero missed statements/branches in _amihud_illiquidity)
    • ruff check app scripts tests, python -m py_compile run.py, python -m compileall -q app scripts tests
    • python scripts/backend_quality_check.py, python scripts/check_requirements_lock.py, python scripts/check_docs.py, python scripts/check_version.py, python scripts/check_mojibake.py

API documentation (if routes/schemas changed)

  • No API routes or OpenAPI schemas modified.

…illiquidity

Signed-off-by: huuan26 <phanbuihuuan261103@gmail.com>
@huuan26
huuan26 requested a review from brokermr810 as a code owner November 15, 2026 08:22
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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