critical: Fix container restart killing positions + phantom detection

Two critical bugs caused by container restart:

1. **Startup order restore failure:**
   - Wrong field names: takeProfit1OrderTx → tp1OrderTx
   - Caused: Prisma error, orders not restored, position unprotected
   - Impact: Container restart left position with NO TP/SL backup

2. **Phantom detection killing runners:**
   - Bug: Flagged runners after TP1 as phantom trades
   - Logic: (currentSize / positionSize) < 0.5
   - Example: $3,317 runner / $8,325 original = 40% = PHANTOM!
   - Result: Set P&L to $0.00 on profitable runner exit

Fixes:
- Use correct DB field names (tp1OrderTx, tp2OrderTx, slOrderTx)
- Phantom detection only checks BEFORE TP1 hit
- Runner P&L calculated on currentSize, not originalPositionSize
- If TP1 hit, we're closing the RUNNER (currentSize)
- If TP1 not hit, we're closing FULL position (originalPositionSize)

Real Impact (Nov 19, 2025):
- SHORT $138.355 → Runner trailing at $136.72 (peak)
- Container restart → Orders failed to restore
 Closed with $0.00 P&L
- Actual profit from Drift: ~$54.41 (TP1 + runner combined)

Prevention:
- Next restart will restore orders correctly
- Runners will calculate P&L properly
- No more premature closures from schema errors
This commit is contained in:
mindesbunister
2025-11-19 15:03:15 +01:00
parent b2cb6a3ecd
commit eccecf7aaa
2 changed files with 11 additions and 9 deletions

View File

@@ -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 {