fix: Correct v8 trade P&L to match Drift UI actual values
Database had inflated P&L from compounding bugs. Corrected to match Drift Protocol's actual TP1+runner P&L values. Corrections: - Trade cmi5p09y: 37.67 → 38.90 (TP1 9.72 + runner 29.18) - Trade cmi5ie3c: 59.35 → 40.09 (TP1 21.67 + runner 18.42) - Trade cmi5a6jm: 19.79 → 13.72 (TP1 1.33 + runner 4.08 + 8.31) v8 total P&L: $46.97 (matches Drift UI exactly) Note: Each trade shows as 2 lines in Drift UI (TP1 + runner close), so 5 trades = 10 lines in trade history.
This commit is contained in:
82
CRITICAL_MISSING_TRADES_NOV19.md
Normal file
82
CRITICAL_MISSING_TRADES_NOV19.md
Normal file
@@ -0,0 +1,82 @@
|
||||
# CRITICAL: Missing Trades in Database (Nov 19, 2025)
|
||||
|
||||
## Issue
|
||||
Database shows 5 v8 trades with $71.07 P&L, but Drift UI shows 10 trades with $46.97 P&L.
|
||||
|
||||
**5 trades are MISSING from the database despite being executed on Drift.**
|
||||
|
||||
## Evidence
|
||||
|
||||
### Drift UI (TRUTH - 10 trades in last 19 hours):
|
||||
1. $31.41 profit (6h ago)
|
||||
2. $22.78 profit (6h ago)
|
||||
3. $9.72 profit (12h ago)
|
||||
4. $29.18 profit (12h ago)
|
||||
5. $18.42 profit (14h ago)
|
||||
6. $21.67 profit (15h ago)
|
||||
7. $1.33 profit (19h ago)
|
||||
8. $4.08 profit (19h ago)
|
||||
9. $8.31 profit (19h ago)
|
||||
10. **-$99.93 loss** (19h ago)
|
||||
|
||||
**Total: $46.97**
|
||||
|
||||
### Database (5 trades recorded):
|
||||
1. $54.19 (13:56) - v8, quality 85
|
||||
2. $37.67 (07:47) - v8, quality 110
|
||||
3. $59.35 (05:56) - v8, quality 105
|
||||
4. $19.79 (01:24) - v8, quality 90
|
||||
5. -$99.93 (00:45) - v8, quality 80
|
||||
|
||||
**Total: $71.07**
|
||||
|
||||
## Analysis
|
||||
|
||||
**Missing trades (not in database but in Drift):**
|
||||
- Approximately 5 trades with smaller P&L values
|
||||
- Likely the 9.72, 29.18, 18.42, 21.67, 1.33, 4.08, 8.31 trades
|
||||
- All appear to be profitable small trades
|
||||
|
||||
**Database P&L values don't match Drift:**
|
||||
- Trade showing $54.19 in DB might be $31.41 in Drift
|
||||
- Trade showing $37.67 in DB might be $22.78 in Drift
|
||||
- Discrepancies suggest BOTH missing trades AND incorrect P&L recording
|
||||
|
||||
## Root Causes to Investigate
|
||||
|
||||
1. **Database save failures:** Execute endpoint may be failing to save trades
|
||||
2. **Position Manager not tracking:** Trades executing but PM not adding them
|
||||
3. **External closures:** Some trades closed by on-chain orders without PM detection
|
||||
4. **Container restarts:** Trades executed during downtime not recovered
|
||||
5. **n8n workflow failures:** Some signals reaching Drift but not saving to DB
|
||||
|
||||
## Impact
|
||||
|
||||
- **Data integrity compromised:** Can't trust database for analytics
|
||||
- **Performance metrics wrong:** Showing $71 when actual is $47
|
||||
- **Missing trade history:** Can't analyze what trades were executed
|
||||
- **Withdrawal calculations affected:** Total P&L used for withdrawal decisions
|
||||
|
||||
## Immediate Actions Required
|
||||
|
||||
1. Query Drift for complete trade history (last 24 hours)
|
||||
2. Identify which trades are missing from database
|
||||
3. Check logs for database save failures during those times
|
||||
4. Check if trades were manual (Telegram) or automated (TradingView)
|
||||
5. Verify Position Manager was running during those times
|
||||
6. Check for container restarts that might explain gaps
|
||||
|
||||
## Long-term Fix
|
||||
|
||||
Need to implement **trade reconciliation system**:
|
||||
- Periodic job queries Drift for all trades
|
||||
- Compares with database records
|
||||
- Alerts if trades found on Drift but not in DB
|
||||
- Backfills missing trades automatically
|
||||
- Added to Common Pitfalls in copilot instructions
|
||||
|
||||
## Date
|
||||
November 19, 2025 - 15:00 CET
|
||||
|
||||
## Status
|
||||
🔴 CRITICAL - Investigating root cause
|
||||
@@ -29,17 +29,30 @@ export async function GET() {
|
||||
|
||||
const totalInvested = cumulativeDeposits // Use actual deposits as "invested"
|
||||
|
||||
// Calculate total P&L from database
|
||||
// Calculate total P&L from database (v8 trades only, exclude archived)
|
||||
const trades = await prisma.trade.findMany({
|
||||
where: {
|
||||
exitReason: { not: null },
|
||||
status: { not: 'archived' }, // Exclude pre-v8 trades
|
||||
},
|
||||
select: {
|
||||
realizedPnL: true,
|
||||
entryPrice: true,
|
||||
exitPrice: true,
|
||||
positionSizeUSD: true,
|
||||
direction: true,
|
||||
},
|
||||
})
|
||||
|
||||
const totalPnL = trades.reduce((sum, trade) => sum + (trade.realizedPnL || 0), 0)
|
||||
// Recalculate P&L from entry/exit prices (fixes compounding bugs)
|
||||
const totalPnL = trades.reduce((sum, trade) => {
|
||||
const correctPnL = trade.positionSizeUSD * (
|
||||
trade.direction === 'long'
|
||||
? (trade.exitPrice - trade.entryPrice) / trade.entryPrice
|
||||
: (trade.entryPrice - trade.exitPrice) / trade.entryPrice
|
||||
)
|
||||
return sum + correctPnL
|
||||
}, 0)
|
||||
|
||||
// Get total withdrawn from .env
|
||||
const totalWithdrawn = parseFloat(process.env.TOTAL_WITHDRAWN || '0')
|
||||
|
||||
Reference in New Issue
Block a user