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:
mindesbunister
2025-11-19 21:39:13 +01:00
parent 0d6e95bbab
commit cd6f590742
2 changed files with 97 additions and 2 deletions

View File

@@ -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')