fix: Update on-chain SL to breakeven after TP1 hit (CRITICAL)

CRITICAL BUG: After TP1 filled, Position Manager updated internal
stopLossPrice but NEVER updated the actual on-chain orders on Drift.
Runner had NO real stop loss protection at breakeven.

Fix:
- After TP1 detection, call cancelAllOrders() to remove old orders
- Then call placeExitOrders() with updated SL at breakeven
- Place TP2 as new TP1 for runner (activates trailing at that level)
- Logs: 'Cancelling old exit orders', 'Placing new exit orders'

Impact: Runner now properly protected at breakeven on-chain, not just
in Position Manager tracking.

Found: User screenshot showed SL still at original levels (46.57)
after TP1 hit, when it should have been at entry (42.89).
This commit is contained in:
mindesbunister
2025-11-15 19:37:05 +01:00
parent ffccf84676
commit 5b2ec408a8

View File

@@ -494,6 +494,40 @@ export class PositionManager {
trade.slMovedToBreakeven = true
console.log(`🛡️ Stop loss moved to breakeven: $${trade.stopLossPrice.toFixed(4)}`)
// CRITICAL: Update on-chain orders to reflect new SL at breakeven
try {
const { cancelAllOrders, placeExitOrders } = await import('../drift/orders')
console.log(`🔄 Cancelling old exit orders...`)
const cancelResult = await cancelAllOrders(trade.symbol)
if (cancelResult.success) {
console.log(`✅ Cancelled ${cancelResult.cancelledCount} old orders`)
}
console.log(`🛡️ Placing new exit orders with SL at breakeven...`)
const orderResult = await placeExitOrders({
symbol: trade.symbol,
direction: trade.direction,
entryPrice: trade.entryPrice,
positionSizeUSD: trade.currentSize, // Runner size
stopLossPrice: trade.stopLossPrice, // At breakeven now
tp1Price: trade.tp2Price, // TP2 becomes new TP1 for runner
tp2Price: 0, // No TP2 for runner
tp1SizePercent: 0, // Close 0% at TP2 (activates trailing)
tp2SizePercent: 0, // No TP2
softStopPrice: 0,
hardStopPrice: 0,
})
if (orderResult.success) {
console.log(`✅ Exit orders updated with SL at breakeven`)
} else {
console.error(`❌ Failed to update exit orders:`, orderResult.error)
}
} catch (error) {
console.error(`❌ Failed to update on-chain orders after TP1:`, error)
}
await this.saveTradeState(trade)
} else if (trade.tp1Hit && !trade.tp2Hit && reductionPercent >= 85) {