gogo2/ENHANCED_PNL_TRACKING_SUMMARY.md
Dobromir Popov 392dbb4b61 wip
2025-05-26 23:04:52 +03:00

109 lines
4.0 KiB
Markdown

# Enhanced PnL Tracking & Position Color Coding Summary
## Overview
Enhanced the trading dashboard with comprehensive PnL tracking, position flipping capabilities, and color-coded position display for better visual identification.
## Key Enhancements
### 1. Position Flipping with PnL Tracking
- **Automatic Position Flipping**: When receiving opposite signals (BUY while SHORT, SELL while LONG), the system now:
- Closes the current position and calculates PnL
- Immediately opens a new position in the opposite direction
- Logs both the close and open actions separately
### 2. Enhanced PnL Calculation
- **Realized PnL**: Calculated when positions are closed
- Long PnL: `(exit_price - entry_price) * size`
- Short PnL: `(entry_price - exit_price) * size`
- **Unrealized PnL**: Real-time calculation for open positions
- **Fee Tracking**: Comprehensive fee tracking for all trades
### 3. Color-Coded Position Display
- **LONG Positions**:
- `[LONG]` indicator with green (success) color when profitable
- Yellow (warning) color when losing
- **SHORT Positions**:
- `[SHORT]` indicator with red (danger) color when profitable
- Blue (info) color when losing
- **No Position**: Gray (muted) color with "No Position" text
### 4. Enhanced Trade Logging
- **Detailed Logging**: Each trade includes:
- Entry/exit prices
- Position side (LONG/SHORT)
- Calculated PnL
- Position action (OPEN_LONG, CLOSE_LONG, OPEN_SHORT, CLOSE_SHORT)
- **Flipping Notifications**: Special logging for position flips
### 5. Improved Dashboard Display
- **Recent Decisions**: Now shows PnL information for closed trades
- **Entry/Exit Info**: Displays entry price for closed positions
- **Real-time Updates**: Position display updates with live unrealized PnL
## Test Results
### Trade Sequence Tested:
1. **BUY @ $3000** → OPENED LONG
2. **SELL @ $3050** → CLOSED LONG (+$5.00 PnL)
3. **SELL @ $3040** → OPENED SHORT
4. **BUY @ $3020** → CLOSED SHORT (+$2.00 PnL) & FLIPPED TO LONG
5. **SELL @ $3010** → CLOSED LONG (-$1.00 PnL)
### Final Results:
- **Total Realized PnL**: $6.00
- **Total Trades**: 6 (3 opens, 3 closes)
- **Closed Trades with PnL**: 3
- **Position Flips**: 1 (SHORT → LONG)
## Technical Implementation
### Key Methods Enhanced:
- `_process_trading_decision()`: Added position flipping logic
- `_create_decisions_list()`: Added PnL display for closed trades
- `_calculate_unrealized_pnl()`: Real-time PnL calculation
- Dashboard callback: Enhanced position display with color coding
### Data Structure:
```python
# Trade Record Example
{
'action': 'SELL',
'symbol': 'ETH/USDT',
'price': 3050.0,
'size': 0.1,
'confidence': 0.80,
'timestamp': datetime.now(timezone.utc),
'position_action': 'CLOSE_LONG',
'entry_price': 3000.0,
'pnl': 5.00,
'fees': 0.0
}
```
### Position Display Format:
```
[LONG] 0.1 @ $3020.00 | P&L: $0.50 # Green if profitable
[SHORT] 0.1 @ $3040.00 | P&L: $-0.50 # Red if profitable for short
No Position # Gray when no position
```
## Windows Compatibility
- **ASCII Indicators**: Used `[LONG]` and `[SHORT]` instead of Unicode emojis
- **No Unicode Characters**: Ensures compatibility with Windows console (cp1252)
- **Color Coding**: Uses Bootstrap CSS classes for consistent display
## Benefits
1. **Clear PnL Visibility**: Immediate feedback on trade profitability
2. **Position Awareness**: Easy identification of current position and P&L status
3. **Trade History**: Complete record of all position changes with PnL
4. **Real-time Updates**: Live unrealized PnL for open positions
5. **Scalping Friendly**: Supports rapid position changes with automatic flipping
## Usage
The enhanced PnL tracking works automatically with the existing dashboard. No additional configuration required. All trades are tracked with full PnL calculation and position management.
## Future Enhancements
- Risk management alerts based on PnL thresholds
- Daily/weekly PnL summaries
- Position size optimization based on PnL history
- Advanced position management (partial closes, scaling in/out)