Skip to content

fix(data): support unary expression negation - #2329

Open
mrlonely (mameikagou) wants to merge 1 commit into
microsoft:mainfrom
mameikagou:fix/unary-expression-negation
Open

fix(data): support unary expression negation#2329
mrlonely (mameikagou) wants to merge 1 commit into
microsoft:mainfrom
mameikagou:fix/unary-expression-negation

Conversation

@mameikagou

Copy link
Copy Markdown

Description

Qlib feature expressions currently raise TypeError for Python unary negation, including common forms such as -$close and -Std($close, 20). Downstream users have to rewrite these expressions as 0 - ... before passing them to D.features.

This adds Expression.__neg__ and represents unary negation with the existing Sub(0, expression) operator, so it keeps the same loading, rolling-window, and cache behavior as the established workaround.

Regression coverage verifies both a raw feature and a nested rolling expression against their existing 0 - ... equivalents.

Motivation and context

This was reproduced in an actual Qlib factor-return pipeline where negative volatility factors must currently be sanitized before evaluation. Supporting Python's standard unary operator removes that downstream compatibility rewrite and makes the expression DSL consistent with its existing binary arithmetic overloads.

Testing

  • pytest -q -s tests/ops — 7 passed, 2 subtests passed
  • Black 23.7.0, line length 120 — passed
  • Flake8 with the repository ignore set — passed
  • git diff --check — passed

Type of change

  • Bug fix
  • New feature
  • Documentation update

@mameikagou
mrlonely (mameikagou) marked this pull request as ready for review August 20, 2026 08:26
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