From 785b09eeed9ddd682f7b40dffa09bfda666ff2bd Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Wed, 3 Dec 2025 20:08:46 +0100 Subject: [PATCH] critical: Fix Bug 1 (revenge external closures) & Bug 5 (validated entry bypass) Bug 1 Fix - Revenge System External Closures: - External closure handler now checks if SL stop-out with quality 85+ - Calls stopHuntTracker.recordStopHunt() after database save - Enables revenge trading for on-chain order fills (not just Position Manager closes) - Added null safety for trade.signalQualityScore (defaults to 0) - Location: lib/trading/position-manager.ts line ~999 Bug 5 Fix - Execute Endpoint Validated Entry Bypass: - Added isValidatedEntry check before quality threshold rejection - Smart Validation Queue signals (quality 50-89) now execute successfully - Logs show bypass reason and validation details (delay, original quality) - Only affects signals with validatedEntry=true flag from queue - Location: app/api/trading/execute/route.ts line ~228 User Clarification: - TradingView price issue (4.47) was temporary glitch, not a bug - Only Bug 1 (revenge) and Bug 5 (execute rejection) needed fixing - Both fixes implemented and TypeScript errors resolved --- app/api/trading/execute/route.ts | 22 +++++++++++++++++----- lib/trading/position-manager.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/app/api/trading/execute/route.ts b/app/api/trading/execute/route.ts index 841ed06..c4a0442 100644 --- a/app/api/trading/execute/route.ts +++ b/app/api/trading/execute/route.ts @@ -225,9 +225,20 @@ export async function POST(request: NextRequest): Promise pullbackMax) { - // Pullback too large - might be reversal, execute with caution - console.log(`⚠️ Smart Entry: Pullback too large (${pullbackMagnitude.toFixed(2)}% > ${pullbackMax}%)`) - console.log(` Possible reversal - executing at current price with caution`) + } else if (pullbackMagnitude > pullbackMax) { + // Pullback too large - might be reversal, execute with caution + console.log(`⚠️ Smart Entry: Pullback too large (${pullbackMagnitude.toFixed(2)}% > ${pullbackMax}%)`) + console.log(` Possible reversal - executing at current price with caution`) + } } } diff --git a/lib/trading/position-manager.ts b/lib/trading/position-manager.ts index 3885a97..5a02c12 100644 --- a/lib/trading/position-manager.ts +++ b/lib/trading/position-manager.ts @@ -995,6 +995,33 @@ export class PositionManager { maxAdversePrice: trade.maxAdversePrice, }) console.log(`💾 External closure recorded: ${exitReason} at $${currentPrice} | P&L: $${totalRealizedPnL.toFixed(2)}`) + + // CRITICAL FIX (Dec 3, 2025): Check revenge eligibility for external closures + // Bug Fix: External closures (on-chain SL orders) weren't checking if quality 85+ for revenge + // Solution: After DB save, check if this was a quality 85+ SL stop-out and record for revenge + const qualityScore = trade.signalQualityScore || 0 + if (exitReason === 'SL' && qualityScore >= 85) { + console.log(`🔍 Quality ${qualityScore} SL stop-out (external) - checking revenge eligibility...`) + try { + const { getStopHuntTracker } = await import('./stop-hunt-tracker') + const stopHuntTracker = getStopHuntTracker() + await stopHuntTracker.recordStopHunt({ + originalTradeId: trade.id, + symbol: trade.symbol, + direction: trade.direction, + stopHuntPrice: currentPrice, + originalEntryPrice: trade.entryPrice, + originalQualityScore: qualityScore, + originalADX: trade.adxAtEntry || 0, + originalATR: trade.atrAtEntry || 0, + stopLossAmount: Math.abs(totalRealizedPnL) + }) + console.log(`🎯 Stop hunt recorded (external closure) - revenge window active for 4 hours`) + } catch (revengeError) { + console.error('⚠️ Failed to record stop hunt for revenge:', revengeError) + // Don't fail external closure if revenge recording fails + } + } } catch (dbError) { console.error('❌ Failed to save external closure:', dbError) }