fix: TopkDropoutStrategy honor overridden get_risk_degree() for buy sizing#2277
Open
zhaow-de wants to merge 1 commit into
Open
fix: TopkDropoutStrategy honor overridden get_risk_degree() for buy sizing#2277zhaow-de wants to merge 1 commit into
zhaow-de wants to merge 1 commit into
Conversation
…izing TopkDropoutStrategy.generate_trade_decision sized new buys with the raw `self.risk_degree` attribute instead of `self.get_risk_degree(trade_step)`, so a subclass overriding get_risk_degree() for market timing (the documented mechanism: "Dynamically risk_degree will result in Market timing.") had zero effect on the book. This brings it in line with WeightStrategyBase and EnhancedIndexingStrategy, which already call the getter. Add a regression test that gates gross exposure to cash via an overridden get_risk_degree() and asserts the strategy builds no stock position.
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.
Description
TopkDropoutStrategy.generate_trade_decisionsized new buys with the raw attributeself.risk_degreeinstead of the getterself.get_risk_degree(trade_step)(qlib/contrib/strategy/signal_strategy.py:266):This was the only use of
risk_degreeinTopkDropoutStrategy, and it bypassed the getter. The change makes Topk consultget_risk_degree()for buy sizing, matchingWeightStrategyBase(line 365) and its subclassEnhancedIndexingStrategy(line 482), which already callself.get_risk_degree(...). It is behavior-preserving for the default case, sinceBaseSignalStrategy.get_risk_degree()returnsself.risk_degreeby default.Motivation and Context
Related issue: closes #2276.
get_risk_degree()is documented as the dynamic market-timing hook ("Dynamically risk_degree will result in Market timing.",signal_strategy.py:69). Because Topk read the raw attribute instead of the getter, a subclass overridingget_risk_degree()to return a time-varying multiplier (the documented way to implement market timing) had zero effect on a TopkDropout book — the override was never consulted for position sizing. Two strategies overriding the hook would therefore produce identical trades/returns. This fix makes the documented override mechanism actually work forTopkDropoutStrategy.How Has This Been Tested?
pytest qlib/tests/test_all_pipeline.pyunder upper directory ofqlib.Added a regression test at
tests/backtest/test_topk_risk_degree.py:test_gate_to_cash_via_get_risk_degree— aTopkDropoutStrategysubclass that overridesget_risk_degree()to return0.0(gate to cash) now builds no stock position. Fails before this change (it bought stock using the raw0.95default), passes after.test_base_strategy_does_buy— sanity check that the default strategy still buys.Screenshots of Test Results (if appropriate):
tests/test_all_pipeline.py, default-risk-degreeTopkDropoutStrategybacktest — confirms the default path is unchanged):tests/backtest/test_topk_risk_degree.py):Types of changes