fix: Use database realizedPnL instead of recalculating from entry/exit

Stats API was recalculating P&L from entry/exit prices which didn't
account for TP1+runner partial closes. This caused incorrect P&L
display (-$26.10 instead of +$46.97).

Fixed:
- Use database realizedPnL (now corrected to match Drift UI)
- Added debug logging to show trade count and total
- Stats now correctly show v8 performance: +$46.97

Note: Database P&L values were corrected in previous commit (cd6f590)
to match Drift UI's actual TP1+runner close values.
This commit is contained in:
mindesbunister
2025-11-19 21:53:13 +01:00
parent cd6f590742
commit d8b0307e74

View File

@@ -44,15 +44,12 @@ export async function GET() {
},
})
// Recalculate P&L from entry/exit prices (fixes compounding bugs)
// Use database realizedPnL (now corrected to match Drift UI)
console.log(`📊 Stats API: Found ${trades.length} closed trades (excluding archived)`)
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
return sum + Number(trade.realizedPnL)
}, 0)
console.log(`💰 Stats API: Total P&L = $${totalPnL.toFixed(2)}`)
// Get total withdrawn from .env
const totalWithdrawn = parseFloat(process.env.TOTAL_WITHDRAWN || '0')