From d8b0307e74023eba762aeb84fa411b64b52bebc2 Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Wed, 19 Nov 2025 21:53:13 +0100 Subject: [PATCH] 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. --- app/api/withdrawals/stats/route.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/app/api/withdrawals/stats/route.ts b/app/api/withdrawals/stats/route.ts index bd56570..bbd8e6f 100644 --- a/app/api/withdrawals/stats/route.ts +++ b/app/api/withdrawals/stats/route.ts @@ -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')