fix(strategy): cap HTF adjustment when short-term indicators show consensus#347
fix(strategy): cap HTF adjustment when short-term indicators show consensus#347TheodorStorm merged 4 commits intodevelopfrom
Conversation
…sensus When RSI and Bollinger both signal overbought/oversold unanimously, cap the HTF bias adjustment to ±10 instead of ±30. This prevents HTF from completely overriding timely exit/entry signals when short-term indicators agree. Consensus detection thresholds: - Bearish/overbought: RSI <= -15 AND BB <= -20 - Bullish/oversold: RSI >= 15 AND BB >= 20 Example from Trade #239: - RSI: -20 (overbought), BB: -30 (27% above upper band) - Both unanimously signaled overbought - HTF applied +30, converting -45 to -7 - With cap of ±10, score would be ~-25 (still a sell signal) Closes #342 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
PR Review: fix(strategy): cap HTF adjustment when short-term indicators show consensusSummaryThis PR addresses issue #342 where the Higher Timeframe (HTF) bias was overriding short-term indicator consensus, causing missed trading opportunities. The fix caps HTF adjustments to ±10 when both RSI and Bollinger indicators unanimously signal overbought/oversold conditions. 🟢 Low Priority Issues1. Code Duplication - Three Nearly Identical Capping BlocksThe HTF adjustment capping logic is duplicated in three places:
Observation: The two blocks inside the extreme fear override branches cap the adjustment but then Suggestion: Consider extracting this into a helper function: def _cap_htf_adjustment_for_consensus(
htf_adjustment: int,
short_term_consensus: bool,
rsi_score: int,
bb_score: int,
cap: int = 10
) -> int:
"""Cap HTF adjustment when short-term indicators show consensus."""
if short_term_consensus and abs(htf_adjustment) > cap:
logger.info(
"htf_adjustment_capped",
reason="short_term_consensus",
original=htf_adjustment,
capped=max(-cap, min(cap, htf_adjustment)),
rsi_score=rsi_score,
bb_score=bb_score,
)
return max(-cap, min(cap, htf_adjustment))
return htf_adjustmentImpact: Low - the code is correct, this is a maintainability improvement. 2. Missing Test Coverage for New BehaviorThe PR adds significant new logic but there are no new tests for the Required Tests:
Impact: Low for merge, but tests should be added before this behavior is relied upon in production. 3. Magic Number: 10 for Cap ValueThe cap value of # At class level or module level
SHORT_TERM_CONSENSUS_HTF_CAP = 10Impact: Low - this is a minor maintainability improvement. ✅ Positive Observations
VerdictApprove with suggestions. The logic is correct and addresses the issue well. The code quality issues are minor (duplication, magic number, missing tests) and can be addressed in follow-up work. Recommended action:
Review by claude[bot] |
- Extract HTF consensus capping into reusable helper function
`_cap_htf_adjustment_for_consensus()` to reduce code duplication
- Add `SHORT_TERM_CONSENSUS_HTF_CAP` constant (value: 10) for the
cap value previously hardcoded in multiple places
- Add comprehensive test suite for short-term consensus HTF capping:
- Unit tests for helper function covering positive/negative caps,
edge cases at boundaries, and custom cap values
- Integration tests for bearish/bullish consensus detection
- Test for extreme fear path with consensus
All review issues addressed:
- Issue 1 (code duplication): Extracted to helper function
- Issue 2 (missing tests): Added 13 new tests
- Issue 3 (magic number): Extracted to named constant
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Review feedback addressed. Please re-review. |
🔍 PR Review: fix(strategy): cap HTF adjustment when short-term indicators show consensusSummaryThis PR addresses issue #342 by capping the HTF (Higher Timeframe) bias adjustment when short-term indicators (RSI and Bollinger) show unanimous consensus on overbought/oversold conditions. The implementation is well-structured and follows good software engineering practices. ✅ Code Quality & Best PracticesStrengths:
Suggestions:
🧪 Test CoverageExcellent test coverage added (240 new lines):
Minor observation:
🔒 Security ConcernsNo security issues identified. This PR modifies trading signal calculation logic but:
⚡ Performance ConsiderationsNo performance concerns. The changes:
🏗️ Architecture ReviewConsensus detection logic is sound: short_term_consensus = (
(rsi_score <= -15 and bb_score <= -20) or # Both bearish/overbought
(rsi_score >= 15 and bb_score >= 20) # Both bullish/oversold
)The thresholds are reasonable:
This ensures only strong consensus triggers the cap. Potential edge case to consider:
📋 Checklist
🎯 VerdictOverall: Approve with minor suggestions 👍 This is a well-implemented fix for a real trading issue (Trade #239 example). The code is clean, well-tested, and follows the repository's patterns. The refactoring commit addressed previous review feedback appropriately. Priority of suggestions:
No blocking issues identified. |
- Avoid redundant cap function call in extreme_fear path by wrapping the second _cap_htf_adjustment_for_consensus call in a conditional - Improve integration tests for consensus detection by using mocked indicator functions instead of synthetic price data that may not produce the expected indicator scores - Add explicit assertions to catch when test data doesn't produce expected conditions instead of silently passing Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Review feedback addressed. Please re-review. |
PR Review: fix(strategy): cap HTF adjustment when short-term indicators show consensusSummaryThis PR addresses issue #342 where HTF (Higher Timeframe) bias adjustments were overriding short-term indicator consensus, causing missed trading opportunities. The fix caps HTF adjustments to ±10 when RSI and Bollinger bands both unanimously signal overbought/oversold conditions. ✅ What's Good1. Well-Designed Solution
2. Clean Implementation
3. Comprehensive Test Coverage
4. Documentation
🟡 Suggestions (Non-blocking)1. Consider Adding a Test for Zero HTF Adjustment def test_helper_zero_adjustment(self):
"""Test zero HTF adjustment is not modified."""
result, was_capped = _cap_htf_adjustment_for_consensus(
htf_adjustment=0,
short_term_consensus=True,
rsi_score=20,
bb_score=20,
)
assert result == 0
assert was_capped is False2. Consider Logging When Consensus is Detected (Even if No Capping) 3. Test if result.score > 0:
# ...
if rsi >= 15 and bb >= 20:
assert htf >= -10, f"HTF should be capped to -10 in extreme fear, got {htf}"The test might silently pass even if the score isn't positive or consensus isn't detected. Consider either:
🟢 Verified
Conclusion🟢 Approve - This is a well-implemented fix with good test coverage. The suggestions above are minor improvements that can be addressed in follow-up PRs if desired. The fix addresses a real issue (HTF overriding unanimous short-term signals) without breaking existing behavior for non-consensus cases. The cap value of ±10 is reasonable - it still allows some HTF influence while preserving the short-term signal direction. Review by claude[bot] |
- Add test_helper_zero_adjustment to cover zero HTF adjustment edge case - Fix test_extreme_fear_path_with_consensus to use mocks instead of conditional assertions, ensuring test coverage is always exercised Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Review feedback addressed. Please re-review. |
PR Review: fix(strategy): cap HTF adjustment when short-term indicators show consensusOverviewThis PR addresses issue #342 by capping the HTF (Higher Timeframe) adjustment when RSI and Bollinger indicators both unanimously signal overbought/oversold conditions. The goal is to prevent HTF from completely overriding timely exit/entry signals during short-term indicator consensus. ✅ Strengths1. Well-structured code organization
2. Comprehensive test coverage
3. Clear problem statement and documentation
4. Correct logic flow
🟢 Low Priority Observations1. Return value unused in all call sites htf_adjustment, _ = _cap_htf_adjustment_for_consensus(...)This is fine for now since logging inside the function provides observability, but consider if the boolean is actually needed in the API. No action required - noted for future consideration. 2. Consider adding a metrics counter
|
| Aspect | Rating | Notes |
|---|---|---|
| Code Quality | ✅ Good | Clean, well-documented, follows existing patterns |
| Logic Correctness | ✅ Good | Capping applied in all HTF paths correctly |
| Test Coverage | ✅ Good | 13+ new tests with edge cases |
| Security | ✅ N/A | No security implications |
| Performance | ✅ Minimal | One extra boolean check per score calculation |
Recommendation: Approve 👍
This is a well-implemented, surgical fix that addresses a specific issue without over-engineering. The code is maintainable and thoroughly tested.
🤖 Review by claude[bot]
Summary
Problem
Trade #239 example:
Solution
When both RSI and Bollinger show consensus:
Cap the HTF adjustment to ±10 instead of ±30. With the example above, score would be ~-25 (still a sell signal).
Trade-offs
Test plan
python3 -m pytest tests/ --ignore=tests/test_coinbase_integration_live.py -vCloses #342
🤖 Generated with Claude Code