From 528a0f4f4342be345b4780bbd27c31eb53a594ab Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Sun, 16 Nov 2025 01:44:57 +0100 Subject: [PATCH] fix: Use Drift's actual entry price for breakeven SL Problem: After TP1, SL moved to 'breakeven' but used database entry price, which can differ from Drift's actual fill price by /bin/bash.10-0.15. Example: - DB stored: $139.18291 entry - Drift actual: $139.07 entry - SL set to: $139.18 (DB value) Solution: Query position.entryPrice from Drift SDK when setting breakeven SL. Drift SDK calculates entry from on-chain data (quoteAssetAmount / baseAssetAmount) which is more accurate than database stored value. Code change (lib/trading/position-manager.ts line ~511): - Before: trade.stopLossPrice = trade.entryPrice - After: trade.stopLossPrice = position.entryPrice || trade.entryPrice Impact: TRUE breakeven protection - no slippage losses from price discrepancies. Related: Common Pitfall #33 (orphaned position restoration with wrong entry price) --- lib/trading/position-manager.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/trading/position-manager.ts b/lib/trading/position-manager.ts index dae28a4..23d5036 100644 --- a/lib/trading/position-manager.ts +++ b/lib/trading/position-manager.ts @@ -508,8 +508,13 @@ export class PositionManager { trade.tp1Hit = true trade.currentSize = positionSizeUSD - // Move SL to breakeven after TP1 - trade.stopLossPrice = trade.entryPrice + // CRITICAL: Query Drift for ACTUAL entry price (more accurate than database) + // The position.entryPrice from Drift SDK is calculated from on-chain data + const actualEntryPrice = position.entryPrice || trade.entryPrice + console.log(`📊 Breakeven calculation: DB entry=$${trade.entryPrice.toFixed(4)}, Drift entry=$${actualEntryPrice.toFixed(4)}`) + + // Move SL to TRUE breakeven after TP1 + trade.stopLossPrice = actualEntryPrice trade.slMovedToBreakeven = true console.log(`🛡️ Stop loss moved to breakeven: $${trade.stopLossPrice.toFixed(4)}`)