diff --git a/lib/trading/position-manager.ts b/lib/trading/position-manager.ts index a909f78..8d4e763 100644 --- a/lib/trading/position-manager.ts +++ b/lib/trading/position-manager.ts @@ -614,16 +614,36 @@ export class PositionManager { trade.tp1Hit = true trade.currentSize = positionSizeUSD - // CRITICAL: Use DATABASE entry price for breakeven (Drift recalculates after partial closes) - // Drift's position.entryPrice is the COST BASIS of remaining position, NOT original entry - // For a true breakeven SL, we need the ORIGINAL entry price from when position opened - const breakevenPrice = trade.entryPrice - console.log(`📊 Breakeven SL: Using original entry price $${breakevenPrice.toFixed(4)} (Drift shows $${position.entryPrice.toFixed(4)} for remaining position)`) + // ADX-based runner SL positioning (Nov 19, 2025) + // Strong trends get more room, weak trends protect capital + let runnerSlPercent: number + const adx = trade.adxAtEntry || 0 - // Move SL to TRUE breakeven after TP1 - trade.stopLossPrice = breakevenPrice + if (adx < 20) { + runnerSlPercent = 0 // Weak trend: breakeven, preserve capital + console.log(`🔒 ADX-based runner SL: ${adx.toFixed(1)} → 0% (breakeven - weak trend)`) + } else if (adx < 25) { + runnerSlPercent = -0.3 // Moderate trend: some room + console.log(`🔒 ADX-based runner SL: ${adx.toFixed(1)} → -0.3% (moderate trend)`) + } else { + runnerSlPercent = -0.55 // Strong trend: full retracement room + console.log(`🔒 ADX-based runner SL: ${adx.toFixed(1)} → -0.55% (strong trend)`) + } + + // CRITICAL: Use DATABASE entry price (Drift recalculates after partial closes) + const newStopLossPrice = this.calculatePrice( + trade.entryPrice, + runnerSlPercent, + trade.direction + ) + + console.log(`📊 Runner SL calculation: Entry $${trade.entryPrice.toFixed(4)} ${runnerSlPercent >= 0 ? '+' : ''}${runnerSlPercent}% = $${newStopLossPrice.toFixed(4)}`) + console.log(` (${this.config.takeProfit1SizePercent}% closed, ${100 - this.config.takeProfit1SizePercent}% remaining)`) + + // Move SL to ADX-based position after TP1 + trade.stopLossPrice = newStopLossPrice trade.slMovedToBreakeven = true - console.log(`🛡️ Stop loss moved to breakeven: $${trade.stopLossPrice.toFixed(4)}`) + console.log(`🛡️ Stop loss moved to: $${trade.stopLossPrice.toFixed(4)}`) // CRITICAL: Update on-chain orders to reflect new SL at breakeven try {