Key fixes:
- Add price validation to detect/correct doubled filled_price from broker
- Emit position_closed socket event to refresh UI after closing positions
- Fix statistics not updating when positions are closed
- Filter out zero-size positions from display
- Add USD/USDT toggle for balance display (click to switch)
Trade system improvements:
- Refactor Trade class P/L calculation with realized/unrealized tracking
- Add settle() method for proper position closing
- Improve trade_filled() to use current filled qty for averaging
- Add fallback to exchange balances when broker balance unavailable
Paper broker enhancements:
- Add exchange-qualified price storage for multi-exchange support
- Add price_source tracking for positions
- Improve state persistence
Tests:
- Add comprehensive tests for trade P/L calculations
- Add tests for paper broker price source tracking
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
High priority fixes:
- Wire time_in_force, stop_loss, take_profit to broker.place_order()
* Pass time_in_force from received_new_trade() to new_trade()
* Pass SL/TP/TIF from new_trade() to broker.place_order()
- Reconcile Trade ledger when SL/TP triggers
* Find all matching paper trades for symbol
* Settle trades at trigger price
* Move from active_trades to settled_trades
* Save to database
- Fix fresh schema missing SL/TP columns
* Add stop_loss and take_profit to CREATE TABLE statement
* Ensures first-run trade persistence works
Medium priority fixes:
- Hide SL/TP fields for SELL orders (inventory-only model)
* SL/TP only makes sense for BUY (opening positions)
* SELL closes existing positions, no SL/TP needed
* Added _updateSltpVisibility() method
* Side change listener shows/hides SL/TP row
* Removed SELL-side SL/TP validation
Tests:
- Added 2 integration tests for manual trading SL/TP path
- 353 tests pass (4 pre-existing failures unrelated)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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>