From 0909846ac5bdeba9606436b4a9c6d32684bc55cc Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Mon, 15 Dec 2025 22:44:20 +0100 Subject: [PATCH] debug: Add comprehensive logging to Position Manager checkTradeConditions (Bug #77 recurrence) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CRITICAL INVESTIGATION (Dec 15, 2025): - Monitoring loop runs every 2s: "🔍 Price check: SOL-PERP @ $124.47 (1 trades)" ✓ - But NO condition checks execute: No TP1/TP2/SL detection, no executeExit calls ❌ - Impact: ,000+ losses - 96% data loss ($32.98 actual vs $1.23 recorded) Added debug logging: - STARTCHK: Function entry (price, entry, check count) - DRIFT: Position size and existence from Drift API - AGE: Trade age in seconds vs 30s threshold Purpose: Identify WHERE checkTradeConditions() returns early before reaching condition checks at line 1497+ Hypothesis: Either Drift returns size=0 OR trade age check fails, causing early return at line 711 --- lib/trading/position-manager.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/trading/position-manager.ts b/lib/trading/position-manager.ts index 1debb08..4227c62 100644 --- a/lib/trading/position-manager.ts +++ b/lib/trading/position-manager.ts @@ -688,6 +688,9 @@ export class PositionManager { trade.lastUpdateTime = Date.now() trade.priceCheckCount++ + // BUG #77 RECURRENCE FIX (Dec 15, 2025): CRITICAL debugging to find why condition checks never execute + console.log(`🔍 STARTCHK: ${trade.symbol} @ $${currentPrice.toFixed(2)} | Entry: $${trade.entryPrice.toFixed(2)} | Checks: ${trade.priceCheckCount}`) + // CRITICAL: First check if on-chain position still exists // (may have been closed by TP/SL orders without us knowing) try { @@ -700,8 +703,11 @@ export class PositionManager { const marketConfig = getMarketConfig(trade.symbol) const position = await driftService.getPosition(marketConfig.driftMarketIndex) + console.log(`🔍 DRIFT: Position ${trade.symbol} | size=${position?.size || 'null'} | exists=${position !== null}`) + // Calculate trade age in seconds const tradeAgeSeconds = (Date.now() - trade.entryTime) / 1000 + console.log(`🔍 AGE: ${trade.symbol} age=${tradeAgeSeconds.toFixed(1)}s | threshold=30s`) if (position === null || position.size === 0) { // IMPORTANT: Skip "external closure" detection for NEW trades (<30 seconds old)