From 7d0d38a8b0734871e4dfd993b9b770e8b2b6d5e5 Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Wed, 3 Dec 2025 08:16:27 +0100 Subject: [PATCH] critical: Fix Bug #1 - Smart Entry using wrong signal price MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PROBLEM: Smart Entry showed 'Signal Price: $70.80' when actual SOL price was ~$139.70 Calculated 'Pullback: -97.38%' when actual price change was <1% Smart Entry queue completely broken due to wrong price ROOT CAUSE: TradingView webhook (or n8n workflow) sends pricePosition percentage (73.77) as signalPrice instead of actual dollar price ($139.70) Code used body.signalPrice directly without validation EVIDENCE: Webhook payload: "pricePosition": 73.7704918033, "signalPrice": 73.7704918033 Identical values = pricePosition mapped incorrectly to signalPrice Percentage value (0-100) treated as dollar price = 100× too low FIXES: 1. Added detection: If signalPrice < $10, log warning (likely percentage) 2. Changed signalPrice source: Use currentPrice from Pyth (NOT body.signalPrice) 3. At signal time: priceChange = 0, pullbackMagnitude = 0 (no pullback yet) 4. Queue with correct price: Smart Entry timer gets current market price 5. Added comments explaining bug and fix IMPACT: Smart Entry will now use correct signal price ($130-150 for SOL) Pullback calculations will be accurate (0.15-0.5% range, not 97%) Queue will work correctly (wait for actual dips/bounces) Next signal will validate fix in production logs TESTING REQUIRED: - Wait for next signal (LONG or SHORT) - Verify log: 'Signal Price: $XXX.XX (using current market price)' - Verify log: 'Current Price: $XXX.XX (same as signal)' - Verify: No more -97% pullback calculations - Verify: Smart Entry queues correctly if no pullback yet FILES CHANGED: - app/api/trading/execute/route.ts lines 485-555 (rewritten Smart Entry logic) LOCATION: - Line 495: Added currentPrice null check - Line 502: Added percentage detection warning - Line 507: Changed to use currentPrice as signalPrice - Line 509-511: Set priceChange/pullback to 0 at signal time - Line 517: Queue with corrected signalPrice RELATED: - Bug #2: Leverage thresholds (FIXED separately, commit 58f812f) - Bug #3: Missing Telegram entry notifications (pending investigation) --- app/api/trading/execute/route.ts | 74 +++++++++++++++++++------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/app/api/trading/execute/route.ts b/app/api/trading/execute/route.ts index e219513..841ed06 100644 --- a/app/api/trading/execute/route.ts +++ b/app/api/trading/execute/route.ts @@ -489,40 +489,54 @@ export async function POST(request: NextRequest): Promise 0 - const pullbackMagnitude = Math.abs(priceChange) - - const pullbackMin = parseFloat(process.env.SMART_ENTRY_PULLBACK_MIN || '0.15') - const pullbackMax = parseFloat(process.env.SMART_ENTRY_PULLBACK_MAX || '0.50') - - console.log(` Signal Price: $${body.signalPrice.toFixed(2)}`) - console.log(` Current Price: $${currentPrice.toFixed(2)} (${priceChange >= 0 ? '+' : ''}${priceChange.toFixed(2)}%)`) - - if (isPullbackDirection && pullbackMagnitude >= pullbackMin && pullbackMagnitude <= pullbackMax) { - // Already at favorable entry - execute immediately! - console.log(`✅ Smart Entry: Already at favorable level (${pullbackMagnitude.toFixed(2)}% pullback)`) - console.log(` Executing immediately - no need to wait`) - } else if (!isPullbackDirection || pullbackMagnitude < pullbackMin) { - // Not favorable yet - queue for smart entry - console.log(`⏳ Smart Entry: Queuing signal for optimal entry timing`) - console.log(` Waiting for ${body.direction === 'long' ? 'dip' : 'bounce'} of ${pullbackMin}-${pullbackMax}%`) + if (!currentPrice) { + console.warn(`⚠️ Smart Entry: No current price available, skipping timing check`) + } else { + // CRITICAL: Detect if body.signalPrice looks like percentage (< $10) + const signalPriceIsPercentage = body.signalPrice && body.signalPrice < 10 + if (signalPriceIsPercentage) { + console.warn(`⚠️ signalPrice (${body.signalPrice.toFixed(2)}) looks like percentage, using current price instead`) + } - // Queue the signal with full context - const queuedSignal = smartEntryTimer.queueSignal({ - symbol: driftSymbol, - direction: body.direction, - signalPrice: body.signalPrice, - atr: body.atr, - adx: body.adx, - rsi: body.rsi, - volumeRatio: body.volumeRatio, - pricePosition: body.pricePosition, + // FIXED: Use current price as both signal and entry price (not body.signalPrice) + const signalPrice = currentPrice + + const priceChange = 0 // At signal time, price change is always 0 + const isPullbackDirection = false // No pullback yet + const pullbackMagnitude = 0 + + const pullbackMin = parseFloat(process.env.SMART_ENTRY_PULLBACK_MIN || '0.15') + const pullbackMax = parseFloat(process.env.SMART_ENTRY_PULLBACK_MAX || '0.50') + + console.log(` Signal Price: $${signalPrice.toFixed(2)} (using current market price)`) + console.log(` Current Price: $${currentPrice.toFixed(2)} (same as signal)`) + + if (isPullbackDirection && pullbackMagnitude >= pullbackMin && pullbackMagnitude <= pullbackMax) { + // Already at favorable entry - execute immediately! + console.log(`✅ Smart Entry: Already at favorable level (${pullbackMagnitude.toFixed(2)}% pullback)`) + console.log(` Executing immediately - no need to wait`) + } else if (!isPullbackDirection || pullbackMagnitude < pullbackMin) { + // Not favorable yet - queue for smart entry + console.log(`⏳ Smart Entry: Queuing signal for optimal entry timing`) + console.log(` Waiting for ${body.direction === 'long' ? 'dip' : 'bounce'} of ${pullbackMin}-${pullbackMax}%`) + + // Queue the signal with CORRECTED signal price (current market price) + const queuedSignal = smartEntryTimer.queueSignal({ + symbol: driftSymbol, + direction: body.direction, + signalPrice: signalPrice, // FIXED: Use current price, not body.signalPrice + atr: body.atr, + adx: body.adx, + rsi: body.rsi, + volumeRatio: body.volumeRatio, + pricePosition: body.pricePosition, indicatorVersion: body.indicatorVersion, qualityScore: qualityResult.score, })