diff --git a/lib/startup/init-position-manager.ts b/lib/startup/init-position-manager.ts index 0bd3f64..5d0efce 100644 --- a/lib/startup/init-position-manager.ts +++ b/lib/startup/init-position-manager.ts @@ -261,9 +261,9 @@ async function restoreOrdersIfMissing( await prisma.trade.update({ where: { id: trade.id }, data: { - takeProfit1OrderTx: result.signatures?.[0], - takeProfit2OrderTx: result.signatures?.[1], - stopLossOrderTx: result.signatures?.[2], + tp1OrderTx: result.signatures?.[0], + tp2OrderTx: result.signatures?.[1], + slOrderTx: result.signatures?.[2], } }) } else { diff --git a/lib/trading/position-manager.ts b/lib/trading/position-manager.ts index 8d4e763..9797ae6 100644 --- a/lib/trading/position-manager.ts +++ b/lib/trading/position-manager.ts @@ -782,13 +782,15 @@ export class PositionManager { // Position Manager detected it, causing zero P&L bug // HOWEVER: If this was a phantom trade (extreme size mismatch), set P&L to 0 - // Use full position size for P&L calculation since we don't know if TP1 filled - // The exit reason logic will determine TP1 vs SL based on profit amount - const sizeForPnL = trade.originalPositionSize + // CRITICAL: Determine size for P&L calculation based on TP1 status + // If TP1 already hit, we're closing the RUNNER only (currentSize) + // If TP1 not hit, we're closing the FULL position (originalPositionSize) + const sizeForPnL = trade.tp1Hit ? trade.currentSize : trade.originalPositionSize - // Check if this was a phantom trade by looking at the last known on-chain size - // If last on-chain size was <50% of expected, this is a phantom - const wasPhantom = trade.currentSize > 0 && (trade.currentSize / trade.positionSize) < 0.5 + // Check if this was a phantom trade by looking at ORIGINAL size mismatch + // Phantom = position opened but size was <50% of expected FROM THE START + // DO NOT flag runners after TP1 as phantom! + const wasPhantom = !trade.tp1Hit && trade.currentSize > 0 && (trade.currentSize / trade.positionSize) < 0.5 console.log(`📊 External closure detected - Position size tracking:`) console.log(` Original size: $${trade.positionSize.toFixed(2)}`)