fix: auto-clean leftovers after stop hits
This commit is contained in:
@@ -498,15 +498,31 @@ export class PositionManager {
|
||||
}
|
||||
|
||||
// Position exists but size mismatch (partial close by TP1?)
|
||||
if (position.size < trade.currentSize * 0.95) { // 5% tolerance
|
||||
console.log(`⚠️ Position size mismatch: expected ${trade.currentSize}, got ${position.size}`)
|
||||
const onChainBaseSize = Math.abs(position.size)
|
||||
const onChainSizeUSD = onChainBaseSize * currentPrice
|
||||
const trackedSizeUSD = trade.currentSize
|
||||
|
||||
if (trackedSizeUSD > 0 && onChainSizeUSD < trackedSizeUSD * 0.95) { // 5% tolerance
|
||||
const expectedBaseSize = trackedSizeUSD / currentPrice
|
||||
console.log(`⚠️ Position size mismatch: tracking $${trackedSizeUSD.toFixed(2)} (~${expectedBaseSize.toFixed(4)} units) but on-chain shows $${onChainSizeUSD.toFixed(2)} (${onChainBaseSize.toFixed(4)} units)`)
|
||||
|
||||
// CRITICAL: If mismatch is extreme (>50%), this is a phantom trade
|
||||
const sizeRatio = (position.size * currentPrice) / trade.currentSize
|
||||
const sizeRatio = trackedSizeUSD > 0 ? onChainSizeUSD / trackedSizeUSD : 0
|
||||
if (sizeRatio < 0.5) {
|
||||
const tradeAgeSeconds = (Date.now() - trade.entryTime) / 1000
|
||||
const probablyPartialRunner = trade.tp1Hit || tradeAgeSeconds > 60
|
||||
|
||||
if (probablyPartialRunner) {
|
||||
console.log(`🛠️ Detected stray remainder (${(sizeRatio * 100).toFixed(1)}%) after on-chain exit - forcing market close`)
|
||||
trade.currentSize = onChainSizeUSD
|
||||
await this.saveTradeState(trade)
|
||||
await this.executeExit(trade, 100, 'manual', currentPrice)
|
||||
return
|
||||
}
|
||||
|
||||
console.log(`🚨 EXTREME SIZE MISMATCH (${(sizeRatio * 100).toFixed(1)}%) - Closing phantom trade`)
|
||||
console.log(` Expected: $${trade.currentSize.toFixed(2)}`)
|
||||
console.log(` Actual: $${(position.size * currentPrice).toFixed(2)}`)
|
||||
console.log(` Expected: $${trackedSizeUSD.toFixed(2)}`)
|
||||
console.log(` Actual: $${onChainSizeUSD.toFixed(2)}`)
|
||||
|
||||
// Close as phantom trade
|
||||
try {
|
||||
@@ -534,10 +550,15 @@ export class PositionManager {
|
||||
return
|
||||
}
|
||||
|
||||
// Update current size to match reality (convert base asset size to USD using current price)
|
||||
trade.currentSize = position.size * currentPrice
|
||||
trade.tp1Hit = true
|
||||
await this.saveTradeState(trade)
|
||||
// Update current size to match reality and run TP1 adjustments if needed
|
||||
trade.currentSize = onChainSizeUSD
|
||||
if (!trade.tp1Hit) {
|
||||
trade.tp1Hit = true
|
||||
await this.handlePostTp1Adjustments(trade, 'on-chain TP1 size sync')
|
||||
} else {
|
||||
await this.saveTradeState(trade)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user