Phase 4: Advanced Order Management
- Add stop_loss and take_profit fields to Trade class with DB persistence
- Add SL/TP form fields in new trade popup with validation
- Implement SL/TP evaluation in PaperBroker.update()
- Auto-close positions when SL/TP thresholds are crossed
- Emit sltp_triggered events to frontend with notifications
- Add Time-in-Force dropdown (GTC, IOC, FOK) to trade form
- Add persistence for position_sltp tracking across state save/load
- Add unit tests for SL/TP functionality (5 new tests)
- Add Trade class SL/TP field tests (2 new tests)
Also includes Phase 3 manual trading broker hardening:
- Fix broker UI not wired on startup
- Fix cancelled orders vanishing from history
- Fix positions not live-updating P/L
- Fix paper pricing exchange-ambiguity
- Simplify paper trades to single synthetic market
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Key fixes:
- ExchangeInterface: Remove stale entries before creating new exchange connections
- LiveBroker: Optimize get_total_equity() to price only top 10 assets (was hanging on 462 testnet assets)
- LiveBroker: Add fiat currency skip list to avoid failed price lookups
- PythonGenerator: Fix market symbol regex to handle 2-5 char symbols (BTC/USDT)
New features:
- LiveStrategyInstance: Full live trading strategy execution
- Circuit breaker and position limits for live trading safety
- Restart-safe order reconciliation via client order IDs
Verified working:
- Live strategy starts on Binance testnet
- Orders placed and filled successfully
- Execution loop runs with active strategies
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1. Paper equity calculation (High):
- get_balance() now correctly includes position value (size * current_price)
- Previously only added unrealized_pnl, missing position principal
2. Buy affordability checks (High):
- place_order() now uses execution price including slippage for market orders
- Uses limit price for limit orders instead of current price
- Prevents orders that would drive cash negative
3. ExchangeInterface price stubs (High):
- get_trade_executed_price() now handles order_price=0.0 (market orders)
- Falls back to current price or entry price when order_price is zero
4. Determinism hash strengthening (Medium):
- Hash now includes full trade sequence with individual trade details
- Added test for different trade sequences with same totals
5. Live mode consistency (Medium):
- Factory now falls back to paper trading with warning instead of raising
- Consistent behavior between factory.py and Strategies.py
Added test: test_paper_broker_equity_calculation to verify fix#1
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Create brokers package with unified trading interface
- BaseBroker: Abstract base class defining broker contract
- BacktestBroker: Wraps Backtrader for backtesting mode
- PaperBroker: Simulated order execution for paper trading
- Factory function to create broker based on trading mode
- Comprehensive test suite for broker functionality
The broker abstraction enables strategies to work identically
across backtest, paper, and live trading modes.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>