Fix: Calculate P&L correctly for external closures
- Save currentSize before it becomes 0 in external closure detection - Use sizeBeforeClosure for P&L calculation instead of trade.currentSize - Prevents /bin/bash.00 P&L for TP2 exits when position closes externally - Ensures win/loss analytics counts TP trades correctly
This commit is contained in:
@@ -282,6 +282,9 @@ export class PositionManager {
|
||||
// Position closed externally (by on-chain TP/SL order)
|
||||
console.log(`⚠️ Position ${trade.symbol} was closed externally (by on-chain order)`)
|
||||
|
||||
// Save currentSize before it becomes 0
|
||||
const sizeBeforeClosure = trade.currentSize
|
||||
|
||||
// Determine exit reason based on price
|
||||
let exitReason: 'TP1' | 'TP2' | 'SL' | 'SOFT_SL' | 'HARD_SL' = 'SL'
|
||||
|
||||
@@ -304,14 +307,14 @@ export class PositionManager {
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate final P&L
|
||||
// Calculate final P&L using size BEFORE closure
|
||||
const profitPercent = this.calculateProfitPercent(
|
||||
trade.entryPrice,
|
||||
currentPrice,
|
||||
trade.direction
|
||||
)
|
||||
const accountPnL = profitPercent * trade.leverage
|
||||
const realizedPnL = (trade.currentSize * accountPnL) / 100
|
||||
const realizedPnL = (sizeBeforeClosure * accountPnL) / 100
|
||||
|
||||
// Update database
|
||||
const holdTimeSeconds = Math.floor((Date.now() - trade.entryTime) / 1000)
|
||||
|
||||
Reference in New Issue
Block a user