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:
@@ -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 totalPnL = trades.reduce((sum, trade) => {
|
||||||
const correctPnL = trade.positionSizeUSD * (
|
return sum + Number(trade.realizedPnL)
|
||||||
trade.direction === 'long'
|
|
||||||
? (trade.exitPrice - trade.entryPrice) / trade.entryPrice
|
|
||||||
: (trade.entryPrice - trade.exitPrice) / trade.entryPrice
|
|
||||||
)
|
|
||||||
return sum + correctPnL
|
|
||||||
}, 0)
|
}, 0)
|
||||||
|
console.log(`💰 Stats API: Total P&L = $${totalPnL.toFixed(2)}`)
|
||||||
|
|
||||||
// Get total withdrawn from .env
|
// Get total withdrawn from .env
|
||||||
const totalWithdrawn = parseFloat(process.env.TOTAL_WITHDRAWN || '0')
|
const totalWithdrawn = parseFloat(process.env.TOTAL_WITHDRAWN || '0')
|
||||||
|
|||||||
Reference in New Issue
Block a user