Files
trading_bot_v3/app/api/enhanced-screenshot/route.js
mindesbunister 6ce4f364a9 feat: Automation-Enhanced Manual Analysis System
Multi-timeframe Intelligence Integration:
- Fixed route.js conflicts preventing multi-timeframe display (2h, 4h now show)
- API now returns multiTimeframeResults with real database sessions
- Multi-timeframe consensus: 4h (82% confidence), 2h (78% confidence)

- Enhanced screenshot API with automation insights context
- New /api/automation-insights endpoint for standalone intelligence
- Pattern recognition from successful automated trades
- Multi-timeframe consensus recommendations

- Historical win rates and profitability patterns (70% win rate, avg 1.9% profit)
- Market trend context from automated sessions (BULLISH consensus)
- Confidence levels based on proven patterns (80% avg confidence)
- Top performing patterns: BUY signals with 102% confidence

- automationContext passed to analysis services
- generateEnhancedRecommendation() with multi-timeframe logic
- Enhanced progress tracking with automation insights step
- Real database integration with prisma for trade patterns

- Resolved Next.js route file conflicts in analysis-details directory
- Multi-timeframe sessions properly grouped and returned
- Automation insights included in API responses
- Enhanced recommendation system with pattern analysis

- Manual analysis now has access to automated trading intelligence
- Multi-timeframe display working (1h, 2h, 4h timeframes)
- Data-driven recommendations based on historical performance
- Seamless integration between automated and manual trading systems
2025-07-20 21:46:22 +02:00

308 lines
12 KiB
JavaScript

import { NextResponse } from 'next/server'
import { enhancedScreenshotService } from '../../../lib/enhanced-screenshot'
import { aiAnalysisService } from '../../../lib/ai-analysis'
import { progressTracker } from '../../../lib/progress-tracker'
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
// 🧠 Generate enhanced recommendations based on automation insights
function generateEnhancedRecommendation(automationContext) {
if (!automationContext) return null
const { multiTimeframeSignals, topPatterns, marketContext } = automationContext
// Multi-timeframe consensus
const signals = multiTimeframeSignals.filter(s => s.decision)
const bullishSignals = signals.filter(s => s.decision === 'BUY').length
const bearishSignals = signals.filter(s => s.decision === 'SELL').length
// Pattern strength
const avgWinRate = signals.length > 0 ?
signals.reduce((sum, s) => sum + (s.winRate || 0), 0) / signals.length : 0
// Profitability insights
const avgProfit = topPatterns.length > 0 ?
topPatterns.reduce((sum, p) => sum + Number(p.profitPercent || 0), 0) / topPatterns.length : 0
let recommendation = '🤖 AUTOMATION-ENHANCED: '
if (bullishSignals > bearishSignals) {
recommendation += `BULLISH CONSENSUS (${bullishSignals}/${signals.length} timeframes)`
if (avgWinRate > 60) recommendation += ` ✅ Strong pattern (${avgWinRate.toFixed(1)}% win rate)`
if (avgProfit > 3) recommendation += ` 💰 High profit potential (~${avgProfit.toFixed(1)}%)`
} else if (bearishSignals > bullishSignals) {
recommendation += `BEARISH CONSENSUS (${bearishSignals}/${signals.length} timeframes)`
} else {
recommendation += 'NEUTRAL - Mixed signals across timeframes'
}
return recommendation
}
export async function POST(request) {
try {
const body = await request.json()
const { symbol, layouts, timeframe, timeframes, selectedLayouts, analyze = true } = body
console.log('📊 Enhanced screenshot request:', { symbol, layouts, timeframe, timeframes, selectedLayouts })
// Generate unique session ID for progress tracking
const sessionId = `analysis_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
console.log('🔍 Created session ID:', sessionId)
// 🧠 LEVERAGE AUTOMATION INSIGHTS FOR MANUAL ANALYSIS
console.log('🤖 Gathering automation insights to enhance manual analysis...')
let automationContext = null
try {
const targetSymbol = symbol || 'SOLUSD'
// Get recent automation sessions for context
const sessions = await prisma.automationSession.findMany({
where: {
userId: 'default-user',
symbol: targetSymbol,
lastAnalysisData: { not: null }
},
orderBy: { createdAt: 'desc' },
take: 3
})
// Get top performing trades for pattern recognition
const successfulTrades = await prisma.trade.findMany({
where: {
userId: 'default-user',
symbol: targetSymbol,
status: 'COMPLETED',
profit: { gt: 0 }
},
orderBy: { profit: 'desc' },
take: 5
})
// Get recent market context
const allTrades = await prisma.trade.findMany({
where: {
userId: 'default-user',
symbol: targetSymbol,
status: 'COMPLETED'
},
orderBy: { createdAt: 'desc' },
take: 10
})
const recentPnL = allTrades.reduce((sum, t) => sum + (t.profit || 0), 0)
const winningTrades = allTrades.filter(t => (t.profit || 0) > 0)
const winRate = allTrades.length > 0 ? (winningTrades.length / allTrades.length * 100) : 0
automationContext = {
multiTimeframeSignals: sessions.map(s => ({
timeframe: s.timeframe,
decision: s.lastAnalysisData?.decision,
confidence: s.lastAnalysisData?.confidence,
sentiment: s.lastAnalysisData?.sentiment,
winRate: s.winRate,
totalPnL: s.totalPnL,
totalTrades: s.totalTrades
})),
topPatterns: successfulTrades.map(t => ({
side: t.side,
profit: t.profit,
confidence: t.confidence,
entryPrice: t.price,
exitPrice: t.exitPrice,
profitPercent: t.exitPrice ? ((t.exitPrice - t.price) / t.price * 100).toFixed(2) : null
})),
marketContext: {
recentPnL,
winRate: winRate.toFixed(1),
totalTrades: allTrades.length,
avgProfit: allTrades.length > 0 ? (recentPnL / allTrades.length).toFixed(2) : 0,
trend: sessions.length > 0 ? sessions[0].lastAnalysisData?.sentiment : 'NEUTRAL'
}
}
console.log('🧠 Automation insights gathered:', {
timeframes: automationContext.multiTimeframeSignals.length,
patterns: automationContext.topPatterns.length,
winRate: automationContext.marketContext.winRate + '%'
})
} catch (error) {
console.error('⚠️ Could not gather automation insights:', error.message)
automationContext = null
}
// Create progress tracking session with initial steps
const initialSteps = [
{
id: 'init',
title: 'Initializing Enhanced Analysis',
description: 'Starting AI-powered trading analysis with automation insights...',
status: 'pending'
},
{
id: 'insights',
title: 'Automation Intelligence',
description: 'Gathering multi-timeframe signals and profitable patterns...',
status: automationContext ? 'completed' : 'warning'
},
{
id: 'auth',
title: 'TradingView Authentication',
description: 'Logging into TradingView accounts',
status: 'pending'
},
{
id: 'navigation',
title: 'Chart Navigation',
description: 'Navigating to chart layouts',
status: 'pending'
},
{
id: 'loading',
title: 'Chart Data Loading',
description: 'Waiting for chart data and indicators',
status: 'pending'
},
{
id: 'capture',
title: 'Screenshot Capture',
description: 'Capturing high-quality screenshots',
status: 'pending'
},
{
id: 'analysis',
title: 'Enhanced AI Analysis',
description: 'Analyzing screenshots with automation-enhanced AI insights',
status: 'pending'
}
]
// Create the progress session
console.log('🔍 Creating progress session with steps:', initialSteps.length)
progressTracker.createSession(sessionId, initialSteps)
console.log('🔍 Progress session created successfully')
// Prepare configuration for screenshot service
const config = {
symbol: symbol || 'BTCUSD',
timeframe: timeframe || timeframes?.[0] || '60', // Use single timeframe, fallback to first of array, then default
layouts: layouts || selectedLayouts || ['ai'],
sessionId, // Pass session ID for progress tracking
automationContext, // 🧠 Pass automation insights to enhance analysis
credentials: {
email: process.env.TRADINGVIEW_EMAIL,
password: process.env.TRADINGVIEW_PASSWORD
}
}
console.log('🔧 Using config:', config)
let screenshots = []
let analysis = null
// Perform AI analysis if requested
if (analyze) {
try {
console.log('🤖 Starting automated capture and analysis...')
const result = await aiAnalysisService.captureAndAnalyzeWithConfig(config, sessionId)
screenshots = result.screenshots
analysis = result.analysis
console.log('✅ Automated capture and analysis completed')
} catch (analysisError) {
console.error('❌ Automated capture and analysis failed:', analysisError)
// Fall back to screenshot only
screenshots = await enhancedScreenshotService.captureWithLogin(config, sessionId)
}
} else {
// Capture screenshots only
screenshots = await enhancedScreenshotService.captureWithLogin(config, sessionId)
}
console.log('📸 Final screenshots:', screenshots)
// ⚠️ DISABLED: Don't cleanup browsers immediately after screenshots
// This was interrupting ongoing analysis processes
// Cleanup will happen automatically via periodic cleanup or manual trigger
// try {
// console.log('🧹 Triggering browser cleanup after screenshot completion...')
// await enhancedScreenshotService.cleanup()
// console.log('✅ Browser cleanup completed after screenshots')
// } catch (cleanupError) {
// console.error('Error in browser cleanup after screenshots:', cleanupError)
// }
const result = {
success: true,
sessionId, // Return session ID for progress tracking
timestamp: Date.now(),
symbol: config.symbol,
layouts: config.layouts,
timeframes: [config.timeframe],
screenshots: screenshots.map(path => ({
layout: config.layouts[0], // For now, assume one layout
timeframe: config.timeframe,
url: `/screenshots/${path.split('/').pop()}`,
timestamp: Date.now()
})),
analysis: analysis,
// 🧠 ENHANCED: Include automation insights in response
automationInsights: automationContext ? {
multiTimeframeConsensus: automationContext.multiTimeframeSignals.length > 0 ?
automationContext.multiTimeframeSignals[0].decision : null,
avgConfidence: automationContext.multiTimeframeSignals.length > 0 ?
(automationContext.multiTimeframeSignals.reduce((sum, s) => sum + (s.confidence || 0), 0) / automationContext.multiTimeframeSignals.length).toFixed(1) : null,
marketTrend: automationContext.marketContext.trend,
winRate: automationContext.marketContext.winRate + '%',
profitablePattern: automationContext.topPatterns.length > 0 ?
`${automationContext.topPatterns[0].side} signals with avg ${automationContext.topPatterns.reduce((sum, p) => sum + Number(p.profitPercent || 0), 0) / automationContext.topPatterns.length}% profit` : null,
recommendation: generateEnhancedRecommendation(automationContext)
} : null,
message: `Successfully captured ${screenshots.length} screenshot(s)${analysis ? ' with automation-enhanced AI analysis' : ''}${automationContext ? ' leveraging multi-timeframe insights' : ''}`
}
// ⚠️ DISABLED: Don't run post-analysis cleanup after every screenshot
// This was killing browser processes during ongoing analysis
// Cleanup should only happen after the ENTIRE automation cycle is complete
// try {
// const { default: aggressiveCleanup } = await import('../../../lib/aggressive-cleanup')
// // Run cleanup in background, don't block the response
// aggressiveCleanup.runPostAnalysisCleanup().catch(console.error)
// } catch (cleanupError) {
// console.error('Error triggering post-analysis cleanup:', cleanupError)
// }
return NextResponse.json(result)
} catch (error) {
console.error('Enhanced screenshot API error:', error)
// ⚠️ DISABLED: Don't cleanup browsers on error during analysis
// This can interrupt ongoing processes that might recover
// try {
// console.log('🧹 Triggering browser cleanup after API error...')
// await enhancedScreenshotService.cleanup()
// console.log('✅ Browser cleanup completed after API error')
// } catch (cleanupError) {
// console.error('Error in browser cleanup after API error:', cleanupError)
// }
return NextResponse.json(
{
success: false,
error: 'Analysis failed',
message: error.message
},
{ status: 500 }
)
}
}
export async function GET() {
return NextResponse.json({
message: 'Enhanced Screenshot API - use POST method for analysis',
endpoints: {
POST: '/api/enhanced-screenshot - Run analysis with parameters'
}
})
}