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, })