diff --git a/app/api/analysis-optimized/route.js b/app/api/analysis-optimized/route.js index fc3c545..ada11b5 100644 --- a/app/api/analysis-optimized/route.js +++ b/app/api/analysis-optimized/route.js @@ -25,48 +25,13 @@ export async function POST(request) { symbol, timeframes: targetTimeframes, layouts, - automationMode + automationMode, + mode }) - // If this is automation mode, integrate with automation service - if (automationMode) { - console.log('🔄 Automation Mode: Integrating with automation service...') - - // Import automation service for background processing - const { automationService } = await import('../../../lib/automation-service-simple') - - // Create automation config - const automationConfig = { - userId: 'default-user', - symbol: symbol || 'SOLUSD', - timeframe: targetTimeframes[0] || '15', // Primary timeframe for database - selectedTimeframes: targetTimeframes, - mode: mode, // Use the mode passed from frontend - dexProvider: dexProvider, - tradingAmount: tradingAmount, - balancePercentage: balancePercentage, - maxLeverage: 3, // Required field for automation - riskPercentage: 2, // Required field for automation - maxDailyTrades: 5, - useOptimizedAnalysis: true // Flag to use our optimized batch processing - } - - const success = await automationService.startAutomation(automationConfig) - - if (success) { - return NextResponse.json({ - success: true, - message: 'Optimized automation started successfully', - mode: 'automation' - }) - } else { - return NextResponse.json({ - success: false, - error: 'Failed to start optimized automation' - }, { status: 500 }) - } - } - + // ALWAYS use batch processing first - even for automation mode + // Then integrate with automation service if needed + // Generate unique session ID for progress tracking const sessionId = `optimized_analysis_${Date.now()}_${Math.random().toString(36).substr(2, 9)}` console.log('🔍 Created optimized session ID:', sessionId) @@ -244,6 +209,38 @@ export async function POST(request) { console.log(`💰 Trade executed: ${tradeResult.direction}`) } + // If this is automation mode, NOW start the automation service with the batch analysis results + if (automationMode) { + console.log('🔄 Starting automation service with batch analysis results...') + + try { + // Import automation service for background processing + const { automationService } = await import('../../../lib/automation-service-simple') + + // Create automation config + const automationConfig = { + userId: 'default-user', + symbol: symbol || 'SOLUSD', + timeframe: targetTimeframes[0] || '15', // Primary timeframe for database + selectedTimeframes: targetTimeframes, + mode: mode, // Use the mode passed from frontend + dexProvider: dexProvider, + tradingAmount: tradingAmount, + balancePercentage: balancePercentage, + maxLeverage: 3, // Required field for automation + riskPercentage: 2, // Required field for automation + maxDailyTrades: 5, + useOptimizedAnalysis: true // Flag to use our optimized batch processing + } + + const automationSuccess = await automationService.startAutomation(automationConfig) + console.log('🤖 Automation service started:', automationSuccess) + } catch (automationError) { + console.error('⚠️ Failed to start automation service:', automationError) + // Don't fail the whole request - batch analysis still succeeded + } + } + return NextResponse.json(result) } catch (error) {