feat: enhance paper trading with comprehensive AI analysis and learning insights

New Features:
- 📊 Detailed Market Analysis Panel (similar to pro trading interface)
  * Market sentiment, recommendation, resistance/support levels
  * Detailed trading setup with entry/exit points
  * Risk management with R:R ratios and confirmation triggers
  * Technical indicators (RSI, OBV, VWAP) analysis

- 🧠 AI Learning Insights Panel
  * Real-time learning status and success rates
  * Winner/Loser trade outcome tracking
  * AI reflection messages explaining what was learned
  * Current thresholds and pattern recognition data

- 🔮 AI Database Integration
  * Shows what AI learned from previous trades
  * Current confidence thresholds and risk parameters
  * Pattern recognition for symbol/timeframe combinations
  * Next trade adjustments based on learning

- 🎓 Intelligent Learning from Outcomes
  * Automatic trade outcome analysis (winner/loser)
  * AI generates learning insights from each trade result
  * Confidence adjustment based on trade performance
  * Pattern reinforcement or correction based on results

- Beautiful gradient panels with color-coded sections
- Clear winner/loser indicators with visual feedback
- Expandable detailed analysis view
- Real-time learning progress tracking

- Completely isolated paper trading (no real money risk)
- Real market data integration for authentic learning
- Safe practice environment with professional analysis tools

This provides a complete AI learning trading simulation where users can:
1. Get real market analysis with detailed reasoning
2. Execute safe paper trades with zero risk
3. See immediate feedback on trade outcomes
4. Learn from AI reflections and insights
5. Understand how AI adapts and improves over time
This commit is contained in:
mindesbunister
2025-08-02 17:56:02 +02:00
parent 33690f51fa
commit 416f72181e
28 changed files with 4665 additions and 45 deletions

View File

@@ -0,0 +1,125 @@
import { NextResponse } from 'next/server'
export async function GET() {
try {
console.log('🚀 Starting analysis-details API call...')
// Return mock data structure that matches what the automation page expects
const analysisData = {
success: true,
data: {
// Analysis details for the main display
symbol: 'SOLUSD',
recommendation: 'HOLD',
confidence: 75,
reasoning: 'Market conditions are neutral. No clear trend direction detected across timeframes.',
// Multi-timeframe analysis
timeframes: [
{
timeframe: '4h',
sessionId: 'session_4h_' + Date.now(),
totalTrades: 12,
winRate: 66.7,
totalPnL: 45.30
},
{
timeframe: '1h',
sessionId: 'session_1h_' + Date.now(),
totalTrades: 8,
winRate: 62.5,
totalPnL: 23.15
}
],
// Recent trades data
recentTrades: [
{
id: 'trade_' + Date.now(),
timestamp: new Date(Date.now() - 3600000).toISOString(),
symbol: 'SOLUSD',
side: 'BUY',
entryPrice: 175.50,
exitPrice: 177.25,
pnl: 12.50,
outcome: 'WIN',
confidence: 80,
reasoning: 'Strong support bounce with volume confirmation'
},
{
id: 'trade_' + (Date.now() - 1),
timestamp: new Date(Date.now() - 7200000).toISOString(),
symbol: 'SOLUSD',
side: 'SELL',
entryPrice: 178.00,
exitPrice: 176.75,
pnl: 8.75,
outcome: 'WIN',
confidence: 75,
reasoning: 'Resistance rejection with bearish momentum'
}
],
// AI Learning status
aiLearningStatus: {
isActive: false,
systemConfidence: 72,
totalDecisions: 45,
successRate: 64.4,
strengths: [
'Strong momentum detection',
'Good entry timing on reversals',
'Effective risk management'
],
weaknesses: [
'Needs improvement in ranging markets',
'Could better identify false breakouts'
],
recentInsights: [
'Better performance on 4H timeframe',
'High win rate on reversal trades'
]
},
// Current trade entry details
entry: {
price: 176.25,
buffer: "±0.25",
rationale: "Current market level"
},
stopLoss: {
price: 174.50,
rationale: "Technical support level"
},
takeProfits: {
tp1: { price: 178.00, description: "First resistance target" },
tp2: { price: 179.50, description: "Extended target" }
},
// Metadata
layoutsAnalyzed: ["AI Layout", "DIY Layout"],
timestamp: new Date().toISOString(),
processingTime: "~2.5 minutes",
analysisDetails: {
screenshotsCaptured: 2,
layoutsAnalyzed: 2,
timeframesAnalyzed: 2,
aiTokensUsed: "~4000 tokens",
analysisStartTime: new Date(Date.now() - 150000).toISOString(),
analysisEndTime: new Date().toISOString()
}
}
}
console.log('✅ Analysis details prepared successfully')
return NextResponse.json(analysisData)
} catch (error) {
console.error('❌ Error in analysis-details API:', error)
return NextResponse.json({
success: false,
error: 'Failed to fetch analysis details',
details: error.message
}, { status: 500 })
}
}

View File

@@ -0,0 +1,125 @@
import { NextResponse } from 'next/server'
export async function GET() {
try {
console.log('🚀 Starting analysis-details API call...')
// Return mock data structure that matches what the automation page expects
const analysisData = {
success: true,
data: {
// Analysis details for the main display
symbol: 'SOLUSD',
recommendation: 'HOLD',
confidence: 75,
reasoning: 'Market conditions are neutral. No clear trend direction detected across timeframes.',
// Multi-timeframe analysis
timeframes: [
{
timeframe: '4h',
sessionId: 'session_4h_' + Date.now(),
totalTrades: 12,
winRate: 66.7,
totalPnL: 45.30
},
{
timeframe: '1h',
sessionId: 'session_1h_' + Date.now(),
totalTrades: 8,
winRate: 62.5,
totalPnL: 23.15
}
],
// Recent trades data
recentTrades: [
{
id: 'trade_' + Date.now(),
timestamp: new Date(Date.now() - 3600000).toISOString(),
symbol: 'SOLUSD',
side: 'BUY',
entryPrice: 175.50,
exitPrice: 177.25,
pnl: 12.50,
outcome: 'WIN',
confidence: 80,
reasoning: 'Strong support bounce with volume confirmation'
},
{
id: 'trade_' + (Date.now() - 1),
timestamp: new Date(Date.now() - 7200000).toISOString(),
symbol: 'SOLUSD',
side: 'SELL',
entryPrice: 178.00,
exitPrice: 176.75,
pnl: 8.75,
outcome: 'WIN',
confidence: 75,
reasoning: 'Resistance rejection with bearish momentum'
}
],
// AI Learning status
aiLearningStatus: {
isActive: false,
systemConfidence: 72,
totalDecisions: 45,
successRate: 64.4,
strengths: [
'Strong momentum detection',
'Good entry timing on reversals',
'Effective risk management'
],
weaknesses: [
'Needs improvement in ranging markets',
'Could better identify false breakouts'
],
recentInsights: [
'Better performance on 4H timeframe',
'High win rate on reversal trades'
]
},
// Current trade entry details
entry: {
price: 176.25,
buffer: "±0.25",
rationale: "Current market level"
},
stopLoss: {
price: 174.50,
rationale: "Technical support level"
},
takeProfits: {
tp1: { price: 178.00, description: "First resistance target" },
tp2: { price: 179.50, description: "Extended target" }
},
// Metadata
layoutsAnalyzed: ["AI Layout", "DIY Layout"],
timestamp: new Date().toISOString(),
processingTime: "~2.5 minutes",
analysisDetails: {
screenshotsCaptured: 2,
layoutsAnalyzed: 2,
timeframesAnalyzed: 2,
aiTokensUsed: "~4000 tokens",
analysisStartTime: new Date(Date.now() - 150000).toISOString(),
analysisEndTime: new Date().toISOString()
}
}
}
console.log('✅ Analysis details prepared successfully')
return NextResponse.json(analysisData)
} catch (error) {
console.error('❌ Error in analysis-details API:', error)
return NextResponse.json({
success: false,
error: 'Failed to fetch analysis details',
details: error.message
}, { status: 500 })
}
}

View File

@@ -0,0 +1,258 @@
import { NextResponse } from 'next/server'
export async function POST(request) {
try {
const { symbol, timeframe, layouts, currentBalance } = await request.json()
console.log('🛡️ Enhanced Anti-Chasing Analysis Started')
console.log(`📊 Request: ${symbol} ${timeframe} [${layouts?.join(', ')}]`)
console.log(`💰 Account Balance: $${currentBalance}`)
// Validate inputs
if (!symbol || !timeframe) {
return NextResponse.json({
success: false,
error: 'Symbol and timeframe are required'
}, { status: 400 })
}
// Dynamic imports to handle TypeScript files
const { EnhancedAntiChasingAI } = await import('../../../lib/enhanced-anti-chasing-ai')
const { EnhancedRiskManager } = await import('../../../lib/enhanced-risk-manager')
try {
// Capture fresh screenshots for analysis
console.log('📸 Capturing fresh screenshots...')
const screenshotResponse = await fetch(`${process.env.NEXTAUTH_URL || 'http://localhost:3000'}/api/enhanced-screenshot`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
symbol,
timeframe,
layouts: layouts || ['ai', 'diy'],
analyze: false // We'll do our own analysis
})
})
if (!screenshotResponse.ok) {
const errorText = await screenshotResponse.text()
console.error('❌ Screenshot capture failed:', errorText)
return NextResponse.json({
success: false,
error: 'Failed to capture fresh screenshots',
details: errorText
}, { status: 500 })
}
const screenshotData = await screenshotResponse.json()
const screenshots = screenshotData.screenshots || []
if (screenshots.length === 0) {
return NextResponse.json({
success: false,
error: 'No screenshots captured',
suggestion: 'Check screenshot service configuration'
}, { status: 404 })
}
console.log(`📸 Captured ${screenshots.length} fresh screenshots for analysis`)
// Initialize AI and Risk Manager instances
const antiChasingAI = new EnhancedAntiChasingAI()
const riskManager = new EnhancedRiskManager({ currentBalance })
// Perform anti-chasing analysis
let analysis
if (screenshots.length === 1) {
analysis = await antiChasingAI.analyzeWithAntiChasing(screenshots[0])
} else {
analysis = await antiChasingAI.analyzeMultipleWithAntiChasing(screenshots)
}
if (!analysis) {
return NextResponse.json({
success: false,
error: 'Failed to analyze screenshots with anti-chasing AI'
}, { status: 500 })
}
console.log('🧠 Anti-chasing analysis completed:')
console.log(` Recommendation: ${analysis.recommendation}`)
console.log(` Confidence: ${analysis.confidence}%`)
console.log(` Momentum Status: ${analysis.momentumStatus?.type}`)
console.log(` Entry Quality: ${analysis.entryQuality?.score}/100`)
// If we have a trading signal, perform risk assessment
let riskAssessment = null
let tradeAllowed = false
let riskDecision = null
if (analysis.recommendation !== 'HOLD' && analysis.entry && analysis.stopLoss) {
console.log('🛡️ Performing risk assessment...')
const recentLosses = await riskManager.getRecentLossCount()
const riskParams = {
symbol,
direction: analysis.recommendation === 'BUY' ? 'LONG' : 'SHORT',
entryPrice: analysis.entry.price,
stopLoss: analysis.stopLoss.price,
takeProfit: analysis.takeProfits?.tp1?.price || (
analysis.recommendation === 'BUY'
? analysis.entry.price * 1.02
: analysis.entry.price * 0.98
),
timeframe,
currentBalance: currentBalance || 127, // Default to current balance
recentLosses
}
const tradeDecision = await riskManager.shouldAllowTrade(riskParams)
riskAssessment = tradeDecision.riskAssessment
tradeAllowed = tradeDecision.allowed
riskDecision = {
allowed: tradeDecision.allowed,
reason: tradeDecision.reason
}
// Record the risk decision
await riskManager.recordTradeDecision(
tradeAllowed ? 'APPROVED' : 'REJECTED',
tradeDecision.reason,
riskAssessment
)
console.log('🛡️ Risk assessment completed:')
console.log(` Trade Allowed: ${tradeAllowed}`)
console.log(` Reason: ${tradeDecision.reason}`)
console.log(` Recommended Size: $${riskAssessment.recommendedSize}`)
}
// Enhanced response with anti-chasing insights
return NextResponse.json({
success: true,
data: {
analysis,
riskAssessment,
tradeDecision: riskDecision,
antiChasingInsights: {
momentumStatus: analysis.momentumStatus,
entryQuality: analysis.entryQuality,
timeframeAlignment: analysis.timeframeAlignment,
riskWarnings: riskAssessment?.riskWarnings || []
},
recommendations: {
shouldTrade: tradeAllowed,
positionSize: riskAssessment?.recommendedSize,
stopLoss: analysis.stopLoss?.price,
takeProfit: analysis.takeProfits?.tp1?.price,
riskReward: analysis.riskToReward,
timeframeAdvice: `This is a ${riskAssessment?.timeframeRisk || 'UNKNOWN'} risk timeframe`
}
},
metadata: {
timestamp: new Date().toISOString(),
screenshotsAnalyzed: screenshots.length,
analysisModel: 'enhanced-anti-chasing-ai',
riskModel: 'enhanced-risk-manager'
}
})
} catch (error) {
console.error('❌ Error in enhanced analysis:', error)
return NextResponse.json({
success: false,
error: 'Failed to perform enhanced anti-chasing analysis',
details: error instanceof Error ? error.message : 'Unknown error'
}, { status: 500 })
}
} catch (error) {
console.error('❌ API Error:', error)
return NextResponse.json({
success: false,
error: 'Internal server error',
details: error instanceof Error ? error.message : 'Unknown error'
}, { status: 500 })
}
}
export async function GET(request) {
try {
const url = new URL(request.url)
const symbol = url.searchParams.get('symbol') || 'SOLUSD'
const timeframe = url.searchParams.get('timeframe') || '240'
const balance = parseFloat(url.searchParams.get('balance') || '127')
console.log('🛡️ Enhanced Anti-Chasing Analysis (GET)')
console.log(`📊 Query: ${symbol} ${timeframe}`)
console.log(`💰 Balance: $${balance}`)
// For GET requests, we'll analyze the most recent screenshots
const screenshotsDir = '/app/screenshots'
try {
const fs = await import('fs/promises')
const path = await import('path')
const files = await fs.readdir(screenshotsDir)
const recentFiles = files
.filter(f => f.includes(symbol) && f.includes(timeframe))
.sort((a, b) => b.localeCompare(a))
.slice(0, 1) // Just take the most recent one for GET
if (recentFiles.length === 0) {
return NextResponse.json({
success: false,
error: 'No recent screenshots available',
suggestion: 'Capture new screenshots using POST /api/enhanced-screenshot'
})
}
const analysis = await enhancedAntiChasingAI.analyzeWithAntiChasing(
path.join(screenshotsDir, recentFiles[0])
)
if (!analysis) {
return NextResponse.json({
success: false,
error: 'Analysis failed'
})
}
// Simplified response for GET requests
return NextResponse.json({
success: true,
data: {
recommendation: analysis.recommendation,
confidence: analysis.confidence,
momentumStatus: analysis.momentumStatus?.type,
entryQuality: analysis.entryQuality?.score,
timeframeRisk: analysis.entryQuality?.riskLevel,
reasoning: analysis.reasoning,
warnings: analysis.entryQuality?.missingConfirmations || []
},
metadata: {
timestamp: new Date().toISOString(),
screenshot: recentFiles[0],
model: 'enhanced-anti-chasing-ai'
}
})
} catch (error) {
console.error('❌ GET analysis error:', error)
return NextResponse.json({
success: false,
error: 'Failed to analyze recent screenshots'
})
}
} catch (error) {
console.error('❌ GET API Error:', error)
return NextResponse.json({
success: false,
error: 'Internal server error'
})
}
}

View File

@@ -10,6 +10,16 @@ export async function POST(request) {
const body = await request.json()
console.log('🔍 Enhanced Screenshot API request:', body)
// PAPER_TRADING PROTECTION: Block requests that could trigger automation
if (body.paperTrading || body.enhancedPrompts) {
console.log('🚨 PAPER_TRADING PROTECTION: Blocking request that could trigger automation')
return NextResponse.json({
success: false,
error: 'PAPER_TRADING_BLOCK: This API cannot be used from paper trading to prevent real trade execution',
safety: true
}, { status: 403 })
}
const config = {
symbol: body.symbol || 'SOLUSD',
timeframe: body.timeframe || '240',
@@ -140,14 +150,25 @@ export async function POST(request) {
} finally {
// CRITICAL: Always run cleanup in finally block
console.log('🧹 FINALLY BLOCK: Running superior screenshot service cleanup...')
console.log('🧹 FINALLY BLOCK: Running screenshot service cleanup...')
try {
// Force cleanup all browser sessions (API-managed, no action needed)
await superiorScreenshotService.cleanup()
console.log('✅ FINALLY BLOCK: Superior screenshot service cleanup completed')
// Import aggressive cleanup for browser process cleanup
const aggressiveCleanup = (await import('../../../lib/aggressive-cleanup')).default
await aggressiveCleanup.forceCleanup()
console.log('✅ FINALLY BLOCK: Aggressive cleanup completed')
// Also run aggressive cleanup to ensure no processes remain
// Also run process cleanup to ensure no orphaned browsers
const { exec } = await import('child_process')
const { promisify } = await import('util')
const execAsync = promisify(exec)
try {
await execAsync('pkill -f "chromium|chrome" || true')
console.log('✅ FINALLY BLOCK: Browser process cleanup completed')
} catch (cleanupError) {
console.log('⚠️ FINALLY BLOCK: Browser process cleanup had minor issues:', cleanupError.message)
}
const { automatedCleanupService } = await import('../../../lib/automated-cleanup-service')
await automatedCleanupService.forceCleanup()
console.log('✅ FINALLY BLOCK: Automated cleanup completed')

View File

@@ -2,31 +2,95 @@ import { NextResponse } from 'next/server'
// PAPER_TRADING_ONLY: This API is completely isolated from live trading
// ISOLATED_MODE: No real trading connections or automation triggers allowed
// SAFETY: This API cannot execute real trades or trigger automation systems
export async function POST(request) {
try {
console.log('🛡️ SAFE PAPER TRADING API: Starting REAL analysis (paper trading only)...')
const body = await request.json()
const { symbol = 'SOLUSD', timeframe = '60', mode, paperTrading, isolatedMode } = body
// SAFETY CHECK: Ensure this is paper trading only
if (mode !== 'PAPER_ONLY' || !paperTrading || !isolatedMode) {
return NextResponse.json({
success: false,
error: 'SAFETY VIOLATION: This API only supports isolated paper trading'
error: 'SAFETY VIOLATION: This API only supports isolated paper trading',
safetyBlock: true
}, { status: 403 })
}
const analysis = {
console.log(`📊 Getting REAL market analysis for ${symbol} ${timeframe}m (paper trading only)...`)
// STEP 1: Capture real market screenshots
const { EnhancedScreenshotService } = await import('../../../lib/enhanced-screenshot')
const screenshotService = new EnhancedScreenshotService()
console.log('🔄 Capturing real market screenshots...')
const screenshots = await screenshotService.captureWithLogin({
symbol,
timeframe,
recommendation: Math.random() > 0.5 ? 'BUY' : 'SELL',
confidence: Math.round(70 + Math.random() * 20),
entry: { price: 160 + Math.random() * 20 },
mockData: true,
paperTrading: true
layouts: ['ai', 'diy'],
sessionId: `paper_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
})
if (!screenshots || screenshots.length === 0) {
throw new Error('Failed to capture market screenshots')
}
return NextResponse.json({ success: true, analysis })
console.log(`✅ Captured ${screenshots.length} real market screenshots`)
// STEP 2: Analyze screenshots with AI
const { aiAnalysisService } = await import('../../../lib/ai-analysis')
console.log('🤖 Analyzing screenshots with AI...')
let analysis
if (screenshots.length === 1) {
analysis = await aiAnalysisService.analyzeScreenshot(screenshots[0])
} else {
analysis = await aiAnalysisService.analyzeMultipleScreenshots(screenshots)
}
if (!analysis) {
throw new Error('Failed to get real market analysis')
}
console.log('✅ Real market analysis complete - REAL DATA, NO TRADING RISK')
return NextResponse.json({
success: true,
analysis: {
...analysis,
// Override safety flags for paper trading
paperTrading: true,
isolated: true,
noRealTrading: true,
realData: true,
source: 'REAL_MARKET_ANALYSIS',
// Remove any mock data flags
mockData: false,
reasoning: `PAPER TRADING - REAL MARKET ANALYSIS:\n\n${analysis.reasoning || 'Real market analysis completed'}\n\n⚠️ SAFETY: This is paper trading only - no real trades will be executed.`
},
safety: {
paperTrading: true,
isolated: true,
noRealTrading: true,
realData: true,
source: 'REAL_MARKET_ANALYSIS'
},
screenshots: screenshots.length,
timestamp: new Date().toISOString()
})
} catch (error) {
return NextResponse.json({ success: false, error: error.message }, { status: 500 })
console.error('❌ Safe paper trading API error:', error)
// NO FALLBACK TO MOCK DATA - Only real data allowed
return NextResponse.json({
success: false,
error: `Real analysis failed: ${error.message}`,
details: 'Paper trading requires real market data. Please try again.',
realDataOnly: true
}, { status: 500 })
}
}

View File

@@ -52,6 +52,17 @@ export async function GET() {
const basePortfolioValue = 1000
const portfolioValue = basePortfolioValue + totalPnL
// Get REAL available coins from Drift positions
const driftResponse = await fetch(`${process.env.APP_URL || 'http://localhost:3000'}/api/drift/positions`);
let availableCoins = ['SOL']; // fallback
if (driftResponse.ok) {
const positionsData = await driftResponse.json();
if (positionsData.success && positionsData.positions) {
availableCoins = positionsData.positions.map((pos: any) => pos.symbol || 'SOL');
}
}
return NextResponse.json({
status: 'connected',
service: 'trading_bot',
@@ -65,18 +76,8 @@ export async function GET() {
activeTrades: activeTrades.length,
completedTrades: completedTrades.length,
winRate: winRate,
availableCoins: availableCoins,
// Get REAL available coins from Drift positions
const driftResponse = await fetch(`${process.env.APP_URL || 'http://localhost:3000'}/api/drift/positions`);
let availableCoins = ['SOL']; // fallback
if (driftResponse.ok) {
const positionsData = await driftResponse.json();
if (positionsData.success && positionsData.positions) {
availableCoins = positionsData.positions.map((pos: any) => pos.symbol || 'SOL');
}
}
// Market prices will be fetched separately
marketPrices: []
})