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)
This commit is contained in:
@@ -508,8 +508,13 @@ export class PositionManager {
|
|||||||
trade.tp1Hit = true
|
trade.tp1Hit = true
|
||||||
trade.currentSize = positionSizeUSD
|
trade.currentSize = positionSizeUSD
|
||||||
|
|
||||||
// Move SL to breakeven after TP1
|
// CRITICAL: Query Drift for ACTUAL entry price (more accurate than database)
|
||||||
trade.stopLossPrice = trade.entryPrice
|
// 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
|
trade.slMovedToBreakeven = true
|
||||||
console.log(`🛡️ Stop loss moved to breakeven: $${trade.stopLossPrice.toFixed(4)}`)
|
console.log(`🛡️ Stop loss moved to breakeven: $${trade.stopLossPrice.toFixed(4)}`)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user