Repository navigation
Closed
1 / 11 of 1 issue completedLabels
enhancementNew feature or requestNew feature or requestpriority:highHigh priority tasks requiring immediate attentionHigh priority tasks requiring immediate attentionrisk-managementRisk management, position sizing, and portfolio protectionRisk management, position sizing, and portfolio protection
Description
Problem
Current system has directional accuracy (51.4% win rate, 0.856 Sharpe) but needs better profit maximization. Fixed stop losses leave money on the table during strong moves.
Dependencies & Relationships
Priority: P1 - HIGH (Q1 2026)
Depends on: Existing position management (✅ COMPLETE)
Blocks: None (enhancement, not blocker)
Enhances: VoterAgent profit capture, Position management
Integrates with: src/trading/position_manager.py, src/trading/order_manager.py
Solution
Implement dynamic trailing stop logic to maximize winning trades while protecting profits.
Core Algorithm
def trailing_stop_logic(entry_price, current_price, initial_stop=0.01):
"""Move stop to breakeven at +0.5%, trail by 0.5% after +1%"""
gain = (current_price - entry_price) / entry_price
if gain >= 0.01: # At 1% profit
return current_price * 0.995 # Trail by 0.5%
elif gain >= 0.005: # At 0.5% profit
return entry_price # Move to breakeven
else:
return entry_price * (1 - initial_stop) # Original stopImplementation Plan
1. Create TrailingStopManager
- Integrate with existing
PositionManager - Track stop levels for each position
- Update stops based on price movements
- Rate-limited API calls for stop order modifications
2. Integration Points
- Modify
trade_lifecycle.pyto use dynamic stops - Update
order_manager.pyfor stop order modifications - Enhance
position_manager.pywith trailing logic - Add configuration to
config_defaults/trading_config.py
3. Configuration Parameters
- Initial stop loss: 1% (configurable)
- Breakeven trigger: 0.5% profit
- Trailing distance: 0.5%
- Update frequency: real-time with rate limiting
Acceptance Criteria
- TrailingStopManager class implementation
- Integration with existing position management
- Configurable parameters via centralized config
- Backtesting validation showing improved profit capture
- Rate-limited real-time stop updates
- Comprehensive unit tests
- Documentation of algorithm and parameters
Risk Considerations
- Avoid over-optimization with too-tight trailing
- Maintain rate limiting for API calls (Alpaca: 200/min)
- Handle partial fills and position sizing correctly
- Account for bid-ask spread in stop placement
- Test in volatile market conditions
Expected Performance Impact
- Baseline: 0.856 Sharpe, 36.6% return, -10.10% max drawdown
- Target: Improved profit capture while maintaining drawdown control
- Metric: Measure before/after with backtesting on same period
Files to Modify
src/trading/position_manager.py- Add trailing stop trackingsrc/trading/trade_lifecycle.py- Integrate dynamic stopssrc/trading/order_manager.py- Stop order modification methodsconfig_defaults/trading_config.py- Add trailing stop configtests/trading/test_trailing_stops.py- New test file
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestpriority:highHigh priority tasks requiring immediate attentionHigh priority tasks requiring immediate attentionrisk-managementRisk management, position sizing, and portfolio protectionRisk management, position sizing, and portfolio protection
Activity
iAmGiG commentedon Nov 11, 2025
🎯 Cday Presentation Priority
Why this matters for demo: Shows sophisticated risk management that maximizes wins. Easy to explain: "System automatically locks in profits as the trade moves in our favor."
Implementation Shortcuts for Quick Win
Existing Code to Leverage
src/trading/position_manager.py- Already tracks positions with entry pricessrc/execution/alpaca_execution_manager.py- Has order modification capabilitiessrc/core/trading_orchestrator.py- Main integration pointMinimal Implementation Path
Option 1: Simple Breakeven Move (1-2 hours)
Option 2: Full Trailing (4-6 hours)
config_defaults/trading_config.pyDemo Script
Testing
Estimated Time: 2-6 hours depending on sophistication level
feat: Add feature agents for GitHub issues #321, #270, #320, #308, #324
docs: Add GitHub issue update templates for issues #321, #270, #320, #…
fix: Add missing modify_stop_order() method to AlpacaOrderManager
feat: Implement dynamic trailing stop logic (#321)
iAmGiG commentedon Nov 27, 2025
Implementation Complete ✅
Branch:
wt/trailing-stopspushed with full implementation.Changes Made:
src/trading/trailing_stop_manager.py(NEW)TrailingStopManagerclass with progressive stop logicStopStatedataclass for position trackingsrc/trading/order_manager.pyreplace_stop_order()method using cancel-replace pattern (Alpaca limitation)config_defaults/trading_config.pyTrailingStopConfigdataclassget_trailing_stop_config()methodsrc/trading/trade_lifecycle.pyupdate_stop()to use newreplace_stop_order()Progressive Stop Logic (validated in backtesting):
Tests:
Ready for:
feat: Issue #358 Phase 1 - Create externalized configuration files
iAmGiG commentedon Nov 27, 2025
Implementation Plan
Branch: feature/trailing-stops-321
Worktree: A:\Projects\AutoGen-Trader-wt-321
Current State
✅ TrailingStopManager implemented (392 lines, src/trading/trailing_stop_manager.py)
✅ Order replacement capability exists (OrderManager.replace_stop_order())
❌ NOT integrated - duplicate inline implementations exist in:
Work Required
Phase 1: trading_cycle.py (6-8 hours)
Phase 2: trade_lifecycle.py (6-8 hours)
Phase 3: Tests (4-6 hours)
Phase 4: Validation (2-3 hours)
Phase 5: Documentation (1-2 hours)
Expected Performance Impact
Key Files
Total Estimated: 20-23 hours with full testing
docs: Add work assignments and project roadmap
refactor: integrate TrailingStopManager into trading_cycle.py (#321)
refactor: integrate TrailingStopManager into trade_lifecycle.py (#321)
iAmGiG commentedon Nov 27, 2025
Implementation Progress Update
Completed Tasks
Phase 1: Integrate into trading_cycle.py
calculate_stop_adjustments()to useTrailingStopManager.calculate_new_stop()ffdfeacPhase 2: Integrate into trade_lifecycle.py
adjust_stop()to delegate to TrailingStopManager4cfa8c4Phase 3: Unit Tests
Remaining Tasks
Phase 4: Forward Testing Validation
Phase 5: Documentation
Integration Approach
Used TrailingStopManager for calculation logic only while keeping existing broker communication patterns:
trading_cycle.py: Usesbatch_modify_orders()via AlpacaOrderManagertrade_lifecycle.py: Usesreplace_stop_order()via OrderManagerThis avoids breaking changes while centralizing the progressive stop logic.
docs: add trailing stop configuration documentation (#321)
iAmGiG commentedon Nov 27, 2025
Issue #321 Complete - Ready for Merge
Implementation Summary
All phases completed successfully:
Commits on
feature/trailing-stops-321ffdfeac- refactor: integrate TrailingStopManager into trading_cycle.py (Implement Dynamic Trailing Stop Logic #321)4cfa8c4- refactor: integrate TrailingStopManager into trade_lifecycle.py (Implement Dynamic Trailing Stop Logic #321)686bd7c- docs: add trailing stop configuration documentation (Implement Dynamic Trailing Stop Logic #321)Key Changes
Code Deduplication: Both
trading_cycle.pyandtrade_lifecycle.pynow use the centralizedTrailingStopManagerfor progressive stop logic instead of duplicate inline implementations.Progressive Stop Thresholds:
Safety Features Preserved:
Ready for PR to
feature/developmentfeat: Integrate TrailingStopManager (#321) (#398)
docs: update WORK_ASSIGNMENTS.md - #321 complete
docs: Update project status - Phase 2 refactoring, #396 merged, dupli…
feat: Implement TradingPipeline - Complete daily workflow orchestrator (
docs: Update project status with #323 pipeline completion
iAmGiG commentedon Nov 28, 2025
Closed: TrailingStopManager implementation complete. Progressive stop logic (2%/4%/6% thresholds), rate-limited updates, integrated with trade_lifecycle.py and trading_cycle.py. Merged to feature/development.