From a8831a91810a34629369718afd0ef1dd82123190 Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Sun, 16 Nov 2025 20:21:51 +0100 Subject: [PATCH] critical: Fix P&L compounding bug in 25 historical trades MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Identified 25 trades with corrupted P&L from compounding bug - Recalculated correct P&L for all affected trades (7 days) - Total corrections: 63.23 in fake profits removed - Today's actual P&L: -.97 (11 trades) - was showing -9.87 before fixes - Last 7 days actual P&L: +7.04 (70 trades) - was showing +27.90 Bug pattern: - P&L updated multiple times during close verification wait - Each update added to previous value instead of replacing - Worst case: -8.17 stored vs -.77 actual (7× compounding) Root cause: Common Pitfall #48 - closingInProgress flag fix deployed Nov 16 18:26 UTC but damage already done to historical data Files modified: - Database: 25 trades updated via SQL recalculation - Fixed trades from Nov 10-16 with ABS(stored - calculated) > $1 --- app/api/drift/account-state/route.ts | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 app/api/drift/account-state/route.ts diff --git a/app/api/drift/account-state/route.ts b/app/api/drift/account-state/route.ts new file mode 100644 index 0000000..66b5fde --- /dev/null +++ b/app/api/drift/account-state/route.ts @@ -0,0 +1,60 @@ +import { NextResponse } from 'next/server' +import { getDriftService } from '@/lib/drift/client' + +export async function GET() { + try { + const driftService = getDriftService() + + // Get account health and equity + const health = await driftService.getAccountHealth() + const equity = await driftService.getAccountEquity() + + // Get all positions + const solPosition = await driftService.getPosition(0) // SOL-PERP + const ethPosition = await driftService.getPosition(1) // ETH-PERP + const btcPosition = await driftService.getPosition(2) // BTC-PERP + + const positions = [] + if (solPosition && Math.abs(solPosition.size) > 0.01) { + positions.push({ + market: 'SOL-PERP', + direction: solPosition.side, + size: solPosition.size, + entryPrice: solPosition.entryPrice, + unrealizedPnL: solPosition.unrealizedPnL + }) + } + if (ethPosition && Math.abs(ethPosition.size) > 0.01) { + positions.push({ + market: 'ETH-PERP', + direction: ethPosition.side, + size: ethPosition.size, + entryPrice: ethPosition.entryPrice, + unrealizedPnL: ethPosition.unrealizedPnL + }) + } + if (btcPosition && Math.abs(btcPosition.size) > 0.01) { + positions.push({ + market: 'BTC-PERP', + direction: btcPosition.side, + size: btcPosition.size, + entryPrice: btcPosition.entryPrice, + unrealizedPnL: btcPosition.unrealizedPnL + }) + } + + return NextResponse.json({ + success: true, + accountHealth: health, + equity: equity, + positions: positions, + timestamp: new Date().toISOString() + }) + } catch (error: any) { + console.error('Error getting account state:', error) + return NextResponse.json({ + success: false, + error: error.message + }, { status: 500 }) + } +}