fix: auto-clean leftovers after stop hits

This commit is contained in:
mindesbunister
2025-11-05 11:42:22 +01:00
parent b58e08778e
commit 149294084e
2 changed files with 42 additions and 14 deletions

View File

@@ -64,15 +64,22 @@ async function validateOpenTrades() {
const marketConfig = getMarketConfig(trade.symbol)
const position = await driftService.getPosition(marketConfig.driftMarketIndex)
// Calculate expected position size in base assets
const expectedSizeBase = trade.positionSizeUSD / trade.entryPrice
// Prefer Position Manager snapshot (captures partial closes) before falling back to original size
const configSnapshot = trade.configSnapshot as any
const pmState = configSnapshot?.positionManagerState
const expectedSizeUSD = typeof pmState?.currentSize === 'number' && pmState.currentSize > 0
? pmState.currentSize
: trade.positionSizeUSD
// Calculate expected position size in base assets (approximate using entry price for consistency)
const expectedSizeBase = expectedSizeUSD / trade.entryPrice
const actualSizeBase = position?.size || 0
// Check if position exists and size matches (with 50% tolerance for partial fills)
const sizeDiff = Math.abs(expectedSizeBase - actualSizeBase)
const sizeRatio = actualSizeBase / expectedSizeBase
if (!position || position.side === 'none' || sizeRatio < 0.5) {
const sizeRatio = expectedSizeBase > 0 ? actualSizeBase / expectedSizeBase : 0
if (!position || position.side === 'none' || sizeRatio < 0.2) {
console.log(`⚠️ PHANTOM TRADE DETECTED:`)
console.log(` Trade ID: ${trade.id.substring(0, 20)}...`)
console.log(` Symbol: ${trade.symbol} ${trade.direction}`)