feat(strategy): add MACD alignment requirement for buy signals#334
feat(strategy): add MACD alignment requirement for buy signals#334TheodorStorm merged 1 commit intodevelopfrom
Conversation
Add configurable option to require MACD histogram to be above a threshold before allowing buy trades. This prevents buying during declining momentum conditions (falling knife scenarios). Changes: - Add `require_macd_alignment` and `macd_alignment_threshold` config params - Implement MACD alignment check in SignalScorer.calculate_score() - When enabled and histogram < threshold, buy signals downgrade to hold - Add comprehensive unit tests for MACD alignment logic - Update .env.example with documentation Post-mortem analysis showed all 4 losing trades had negative MACD histogram (-37 to -85) while RSI/Bollinger indicated oversold. Requiring MACD alignment would have blocked every losing trade in that session. Feature is disabled by default (opt-in) with threshold of -0.5. Closes #331 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
PR Review: feat(strategy): add MACD alignment requirement for buy signalsOverall Assessment: ✅ APPROVEThis is a well-implemented, targeted improvement to the trading strategy that addresses a documented post-mortem finding. The code is clean, well-tested, and follows the existing patterns in the codebase. 🟢 Strengths1. Post-Mortem Driven Development
2. Conservative Default Configuration
3. Well-Structured Implementation
4. Comprehensive Test Coverage
🟡 Minor Observations (Low Priority)1. Metadata key naming convention metadata["_macd_alignment_blocked"] = 1 if macd_alignment_blocked else 0Other metadata uses underscored prefix consistently. This matches the pattern - looks good. 2. Conservative behavior on missing MACD data (Line 1237-1243) Suggestion (optional): Consider incrementing a counter/metric when MACD data is missing to catch any data pipeline issues early. Not blocking merge. 3. Test robustness if result.score >= macd_alignment_scorer.threshold:
assert result.action == "buy"This is acceptable given the stochastic nature of the test data generation, but doesn't guarantee the condition is actually exercised. The seed-based approach ( 🔒 Security ConsiderationsNo security concerns. The changes:
⚡ Performance ConsiderationsMinimal performance impact:
✅ Checklist
📝 SummaryThis PR adds a valuable safety feature to prevent buying during declining momentum conditions. The implementation is clean, well-tested, and follows all codebase conventions. The opt-in design ensures no disruption to existing deployments. Recommendation: Merge when CI passes. Review by Claude Opus 4.5 |
Summary
Changes
require_macd_alignmentandmacd_alignment_thresholdconfig parameterscalculate_score()- when enabled and histogram < threshold, buy signals downgrade to holdContext
From post-mortem analysis (2026-01-09):
Test plan
python3 -m pytest tests/ --ignore=tests/test_coinbase_integration_live.py -vCloses #331
🤖 Generated with Claude Code