Integrate superior parallel screenshot system into main automation
BREAKING CHANGES: - Replace enhancedScreenshotService with superiorScreenshotService throughout system - Update trading presets to match actual strategy definitions: * Scalp: 5m, 15m (was 7 timeframes) * Day Trading: 1h, 4h (NEW) * Swing Trading: 4h, 1D (NEW) * Extended: All timeframes for comprehensive analysis - Auto-trading service now uses intelligent parallel capture - Enhanced-screenshot API restored with superior backend - AI analysis service updated for compatibility - Superior screenshot API supports all presets PERFORMANCE IMPROVEMENTS: - Parallel capture for ALL timeframes regardless of count - Intelligent preset detection based on timeframe patterns - No more hardcoded 7-timeframe limitation - Backwards compatibility maintained The system now uses the superior parallel approach for ANY timeframe selection, whether it's 2 timeframes (scalp/day/swing) or 8+ timeframes (extended). No more sequential delays - everything is parallel!
This commit is contained in:
@@ -17,16 +17,34 @@ export async function POST(request) {
|
||||
|
||||
console.log('📋 Superior Config:', config)
|
||||
|
||||
// Use the proven scalp preset parallel technique
|
||||
const SCALP_TIMEFRAMES = [
|
||||
{ name: '1m', tv: '1' },
|
||||
{ name: '3m', tv: '3' },
|
||||
{ name: '5m', tv: '5' },
|
||||
{ name: '15m', tv: '15' },
|
||||
{ name: '30m', tv: '30' },
|
||||
{ name: '1h', tv: '60' },
|
||||
{ name: '4h', tv: '240' }
|
||||
]
|
||||
// Define trading presets matching the main automation system
|
||||
const TRADING_PRESETS = {
|
||||
'scalp': [
|
||||
{ name: '5m', tv: '5' },
|
||||
{ name: '15m', tv: '15' }
|
||||
],
|
||||
'day-trading': [
|
||||
{ name: '1h', tv: '60' },
|
||||
{ name: '4h', tv: '240' }
|
||||
],
|
||||
'swing-trading': [
|
||||
{ name: '4h', tv: '240' },
|
||||
{ name: '1D', tv: '1D' }
|
||||
],
|
||||
'extended': [
|
||||
{ name: '1m', tv: '1' },
|
||||
{ name: '3m', tv: '3' },
|
||||
{ name: '5m', tv: '5' },
|
||||
{ name: '15m', tv: '15' },
|
||||
{ name: '30m', tv: '30' },
|
||||
{ name: '1h', tv: '60' },
|
||||
{ name: '4h', tv: '240' },
|
||||
{ name: '1D', tv: '1D' }
|
||||
]
|
||||
}
|
||||
|
||||
// Get timeframes for the selected preset
|
||||
const selectedTimeframes = TRADING_PRESETS[config.preset] || TRADING_PRESETS['scalp']
|
||||
|
||||
// For single timeframe compatibility
|
||||
if (body.timeframe) {
|
||||
@@ -69,11 +87,11 @@ export async function POST(request) {
|
||||
|
||||
// Multi-timeframe parallel capture
|
||||
const startTime = Date.now()
|
||||
console.log(`🔄 Starting parallel capture of ${SCALP_TIMEFRAMES.length} timeframes...`)
|
||||
console.log(`🔄 Starting parallel capture of ${selectedTimeframes.length} timeframes for ${config.preset} preset...`)
|
||||
|
||||
const capturePromises = SCALP_TIMEFRAMES.map(async (tf, index) => {
|
||||
const capturePromises = selectedTimeframes.map(async (tf, index) => {
|
||||
try {
|
||||
console.log(`📸 [${index + 1}/${SCALP_TIMEFRAMES.length}] Starting ${tf.name} (${tf.tv})...`)
|
||||
console.log(`📸 [${index + 1}/${selectedTimeframes.length}] Starting ${tf.name} (${tf.tv})...`)
|
||||
|
||||
const response = await fetch('http://localhost:9001/api/enhanced-screenshot', {
|
||||
method: 'POST',
|
||||
@@ -129,8 +147,8 @@ export async function POST(request) {
|
||||
|
||||
console.log('✅ SUPERIOR PARALLEL CAPTURE COMPLETED!')
|
||||
console.log(`⏱️ Duration: ${duration.toFixed(2)}s`)
|
||||
console.log(`📸 Screenshots: ${totalScreenshots}/${SCALP_TIMEFRAMES.length * config.layouts.length}`)
|
||||
console.log(`🎯 Success: ${successful.length}/${SCALP_TIMEFRAMES.length}`)
|
||||
console.log(`📸 Screenshots: ${totalScreenshots}/${selectedTimeframes.length * config.layouts.length}`)
|
||||
console.log(`🎯 Success: ${successful.length}/${selectedTimeframes.length}`)
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
@@ -140,10 +158,10 @@ export async function POST(request) {
|
||||
duration: duration,
|
||||
totalScreenshots: totalScreenshots,
|
||||
successfulTimeframes: successful.length,
|
||||
totalTimeframes: SCALP_TIMEFRAMES.length,
|
||||
successRate: ((successful.length / SCALP_TIMEFRAMES.length) * 100).toFixed(1),
|
||||
totalTimeframes: selectedTimeframes.length,
|
||||
successRate: ((successful.length / selectedTimeframes.length) * 100).toFixed(1),
|
||||
results: results,
|
||||
message: `Parallel capture completed: ${successful.length}/${SCALP_TIMEFRAMES.length} timeframes in ${duration.toFixed(2)}s`
|
||||
message: `Parallel capture completed: ${successful.length}/${selectedTimeframes.length} timeframes in ${duration.toFixed(2)}s`
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
@@ -162,12 +180,19 @@ export async function GET() {
|
||||
endpoints: {
|
||||
POST: '/api/superior-screenshot - Superior parallel analysis'
|
||||
},
|
||||
presets: {
|
||||
'scalp': '2 timeframes (5m, 15m) - Scalping strategy',
|
||||
'day-trading': '2 timeframes (1h, 4h) - Day trading strategy',
|
||||
'swing-trading': '2 timeframes (4h, 1D) - Swing trading strategy',
|
||||
'extended': '8 timeframes (1m-1D) - Comprehensive analysis'
|
||||
},
|
||||
features: [
|
||||
'Parallel multi-timeframe capture',
|
||||
'Scalp preset (7 timeframes)',
|
||||
'Intelligent preset detection',
|
||||
'Single timeframe compatibility',
|
||||
'Proven efficiency (100% success rate)',
|
||||
'API-managed browser sessions'
|
||||
'API-managed browser sessions',
|
||||
'No hardcoded 7-timeframe limitation'
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { enhancedScreenshotService } from './enhanced-screenshot-robust'
|
||||
import { superiorScreenshotService } from './superior-screenshot-service'
|
||||
import { aiAnalysisService } from './ai-analysis'
|
||||
import { automatedCleanupService } from './automated-cleanup-service'
|
||||
import aggressiveCleanup from './aggressive-cleanup'
|
||||
@@ -69,7 +69,7 @@ export class AutoTradingService {
|
||||
|
||||
// Force cleanup all browser sessions
|
||||
try {
|
||||
await enhancedScreenshotService.cleanup()
|
||||
await superiorScreenshotService.cleanup()
|
||||
await aggressiveCleanup.forceCleanup()
|
||||
console.log('✅ Trading service stopped and cleaned up')
|
||||
} catch (cleanupError) {
|
||||
@@ -80,60 +80,103 @@ export class AutoTradingService {
|
||||
private async runTradingCycle(config: TradingConfig): Promise<void> {
|
||||
console.log(`🔄 Running trading cycle ${this.currentCycle}...`)
|
||||
|
||||
// Process each timeframe sequentially to avoid resource conflicts
|
||||
for (const timeframe of config.timeframes) {
|
||||
if (!this.isRunning) break // Check if service was stopped
|
||||
try {
|
||||
console.log(`\n📊 Processing ${config.symbol} with ${config.timeframes.length} timeframes...`)
|
||||
|
||||
try {
|
||||
console.log(`\n📊 Processing ${config.symbol} ${timeframe}...`)
|
||||
|
||||
// Capture screenshots with robust cleanup
|
||||
const screenshots = await enhancedScreenshotService.captureWithLogin({
|
||||
symbol: config.symbol,
|
||||
timeframe: timeframe,
|
||||
layouts: config.layouts,
|
||||
analyze: false // We'll analyze separately
|
||||
})
|
||||
// Determine trading strategy based on timeframes for intelligent parallel capture
|
||||
const timeframeSet = new Set(config.timeframes)
|
||||
let preset: 'scalp' | 'day-trading' | 'swing-trading' | 'custom' = 'custom'
|
||||
|
||||
// Detect strategy based on timeframe patterns
|
||||
if (timeframeSet.has('5') && timeframeSet.has('15') && config.timeframes.length === 2) {
|
||||
preset = 'scalp'
|
||||
console.log('🎯 Detected: SCALP trading strategy')
|
||||
} else if (timeframeSet.has('60') && timeframeSet.has('240') && config.timeframes.length === 2) {
|
||||
preset = 'day-trading'
|
||||
console.log('🎯 Detected: DAY TRADING strategy')
|
||||
} else if (timeframeSet.has('240') && timeframeSet.has('1D') && config.timeframes.length === 2) {
|
||||
preset = 'swing-trading'
|
||||
console.log('🎯 Detected: SWING TRADING strategy')
|
||||
} else {
|
||||
preset = 'custom'
|
||||
console.log('🎯 Using: CUSTOM timeframe configuration')
|
||||
}
|
||||
|
||||
if (screenshots.length > 0) {
|
||||
console.log(`✅ Captured ${screenshots.length} screenshots for ${timeframe}`)
|
||||
// Capture screenshots using superior parallel technique
|
||||
let screenshots: any[] = []
|
||||
let allResults: any[] = []
|
||||
|
||||
if (preset === 'custom') {
|
||||
// Custom timeframes - use parallel capture with custom preset
|
||||
console.log('🚀 Using superior parallel capture with custom timeframes...')
|
||||
allResults = await superiorScreenshotService.captureParallel({
|
||||
symbol: config.symbol,
|
||||
preset: 'custom',
|
||||
timeframes: config.timeframes,
|
||||
layouts: config.layouts,
|
||||
analyze: false
|
||||
})
|
||||
} else {
|
||||
// Standard preset - use optimized preset method
|
||||
console.log(`🚀 Using superior parallel capture with ${preset} preset...`)
|
||||
allResults = await superiorScreenshotService.captureParallel({
|
||||
symbol: config.symbol,
|
||||
preset: preset,
|
||||
layouts: config.layouts,
|
||||
analyze: false
|
||||
})
|
||||
}
|
||||
|
||||
// Extract successful screenshots for analysis
|
||||
screenshots = allResults
|
||||
.filter(result => result.success && result.screenshots)
|
||||
.flatMap(result => result.screenshots || [])
|
||||
|
||||
if (screenshots.length > 0) {
|
||||
console.log(`✅ Superior parallel capture completed: ${screenshots.length} screenshots`)
|
||||
|
||||
// Process results by timeframe for detailed analysis
|
||||
for (const result of allResults) {
|
||||
if (!result.success) {
|
||||
console.error(`❌ Failed to capture ${result.timeframeName}: ${result.error}`)
|
||||
continue
|
||||
}
|
||||
|
||||
// Analyze screenshots
|
||||
if (!result.screenshots || result.screenshots.length === 0) {
|
||||
console.warn(`⚠️ No screenshots for ${result.timeframeName}`)
|
||||
continue
|
||||
}
|
||||
|
||||
try {
|
||||
console.log(`\n🤖 Analyzing ${result.timeframeName} (${result.screenshots.length} screenshots)...`)
|
||||
|
||||
let analysis = null
|
||||
if (screenshots.length === 1) {
|
||||
analysis = await aiAnalysisService.analyzeScreenshot(screenshots[0])
|
||||
} else if (screenshots.length > 1) {
|
||||
analysis = await aiAnalysisService.analyzeMultipleScreenshots(screenshots)
|
||||
if (result.screenshots.length === 1) {
|
||||
analysis = await aiAnalysisService.analyzeScreenshot(result.screenshots[0])
|
||||
} else if (result.screenshots.length > 1) {
|
||||
analysis = await aiAnalysisService.analyzeMultipleScreenshots(result.screenshots)
|
||||
}
|
||||
|
||||
if (analysis) {
|
||||
console.log(`✅ Analysis completed for ${timeframe}`)
|
||||
console.log(`✅ Analysis completed for ${result.timeframeName}`)
|
||||
console.log(`📈 Sentiment: ${analysis.marketSentiment || 'Unknown'}`)
|
||||
console.log(`🎯 Recommendation: ${analysis.recommendation}, Confidence: ${analysis.confidence}%`)
|
||||
|
||||
// Here you would implement your trading logic based on analysis
|
||||
await this.processAnalysisForTrading(analysis, config.symbol, timeframe)
|
||||
// Process trading logic based on analysis
|
||||
await this.processAnalysisForTrading(analysis, config.symbol, result.timeframeName)
|
||||
} else {
|
||||
console.warn(`⚠️ No analysis returned for ${timeframe}`)
|
||||
console.warn(`⚠️ No analysis returned for ${result.timeframeName}`)
|
||||
}
|
||||
} catch (analysisError) {
|
||||
console.error(`❌ Analysis failed for ${timeframe}:`, analysisError)
|
||||
console.error(`❌ Analysis failed for ${result.timeframeName}:`, analysisError)
|
||||
}
|
||||
} else {
|
||||
console.error(`❌ No screenshots captured for ${timeframe}`)
|
||||
}
|
||||
|
||||
// Small delay between timeframes to prevent overwhelming the system
|
||||
if (config.timeframes.indexOf(timeframe) < config.timeframes.length - 1) {
|
||||
console.log('⏳ Brief pause before next timeframe...')
|
||||
await new Promise(resolve => setTimeout(resolve, 5000))
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error(`❌ Error processing ${timeframe}:`, error)
|
||||
// Continue with next timeframe even if one fails
|
||||
} else {
|
||||
console.error(`❌ No screenshots captured using superior parallel technique`)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error(`❌ Error in trading cycle:`, error)
|
||||
}
|
||||
|
||||
console.log(`✅ Trading cycle ${this.currentCycle} completed`)
|
||||
|
||||
@@ -3,26 +3,33 @@
|
||||
* Uses the proven scalp preset parallel API technique for maximum efficiency
|
||||
*/
|
||||
|
||||
// Scalping-focused timeframes (most commonly used)
|
||||
// Trading strategy presets matching the main automation system
|
||||
const SCALP_TIMEFRAMES = [
|
||||
{ name: '1m', tv: '1', description: 'Ultra-short scalping' },
|
||||
{ name: '3m', tv: '3', description: 'Short scalping' },
|
||||
{ name: '5m', tv: '5', description: 'Standard scalping' },
|
||||
{ name: '15m', tv: '15', description: 'Swing scalping' },
|
||||
{ name: '30m', tv: '30', description: 'Longer scalping' },
|
||||
{ name: '1h', tv: '60', description: 'Context timeframe' },
|
||||
{ name: '4h', tv: '240', description: 'Trend context' }
|
||||
{ name: '15m', tv: '15', description: 'Swing scalping' }
|
||||
]
|
||||
|
||||
const DAY_TRADING_TIMEFRAMES = [
|
||||
{ name: '1h', tv: '60', description: 'Hourly analysis' },
|
||||
{ name: '4h', tv: '240', description: 'Four-hour trend' }
|
||||
]
|
||||
|
||||
const SWING_TRADING_TIMEFRAMES = [
|
||||
{ name: '4h', tv: '240', description: 'Four-hour trend' },
|
||||
{ name: '1D', tv: '1D', description: 'Daily analysis' }
|
||||
]
|
||||
|
||||
// Additional timeframes for comprehensive analysis
|
||||
const EXTENDED_TIMEFRAMES = [
|
||||
{ name: '1m', tv: '1', description: 'Ultra-short scalping' },
|
||||
{ name: '3m', tv: '3', description: 'Short scalping' },
|
||||
{ name: '2m', tv: '2', description: 'Very short scalping' },
|
||||
{ name: '10m', tv: '10', description: 'Medium scalping' },
|
||||
{ name: '30m', tv: '30', description: 'Longer scalping' },
|
||||
{ name: '45m', tv: '45', description: 'Extended scalping' },
|
||||
{ name: '2h', tv: '120', description: 'Medium trend' },
|
||||
{ name: '6h', tv: '360', description: 'Long trend' },
|
||||
{ name: '12h', tv: '720', description: 'Daily trend' },
|
||||
{ name: '1D', tv: '1D', description: 'Daily analysis' },
|
||||
{ name: '3D', tv: '3D', description: 'Multi-day trend' },
|
||||
{ name: '1W', tv: '1W', description: 'Weekly analysis' }
|
||||
]
|
||||
@@ -31,7 +38,7 @@ export interface SuperiorScreenshotConfig {
|
||||
symbol: string
|
||||
timeframes?: string[] // If not provided, uses SCALP_TIMEFRAMES
|
||||
layouts?: string[] // Default: ['ai', 'diy']
|
||||
preset?: 'scalp' | 'extended' | 'all' | 'custom'
|
||||
preset?: 'scalp' | 'day-trading' | 'swing-trading' | 'extended' | 'all' | 'custom'
|
||||
analyze?: boolean // Whether to run AI analysis
|
||||
apiUrl?: string // API endpoint (default: http://localhost:9001)
|
||||
}
|
||||
@@ -73,11 +80,17 @@ export class SuperiorScreenshotService {
|
||||
case 'scalp':
|
||||
timeframesToCapture = SCALP_TIMEFRAMES
|
||||
break
|
||||
case 'day-trading':
|
||||
timeframesToCapture = DAY_TRADING_TIMEFRAMES
|
||||
break
|
||||
case 'swing-trading':
|
||||
timeframesToCapture = SWING_TRADING_TIMEFRAMES
|
||||
break
|
||||
case 'extended':
|
||||
timeframesToCapture = [...SCALP_TIMEFRAMES, ...EXTENDED_TIMEFRAMES]
|
||||
timeframesToCapture = [...SCALP_TIMEFRAMES, ...DAY_TRADING_TIMEFRAMES, ...SWING_TRADING_TIMEFRAMES, ...EXTENDED_TIMEFRAMES]
|
||||
break
|
||||
case 'all':
|
||||
timeframesToCapture = [...SCALP_TIMEFRAMES, ...EXTENDED_TIMEFRAMES]
|
||||
timeframesToCapture = [...SCALP_TIMEFRAMES, ...DAY_TRADING_TIMEFRAMES, ...SWING_TRADING_TIMEFRAMES, ...EXTENDED_TIMEFRAMES]
|
||||
break
|
||||
case 'custom':
|
||||
if (config.timeframes) {
|
||||
@@ -214,7 +227,7 @@ export class SuperiorScreenshotService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Scalp preset - optimized for scalping strategies
|
||||
* Scalp preset - optimized for scalping strategies (5m, 15m)
|
||||
*/
|
||||
async captureScalpPreset(symbol: string, analyze: boolean = false): Promise<ScreenshotResult[]> {
|
||||
return this.captureParallel({
|
||||
@@ -225,6 +238,30 @@ export class SuperiorScreenshotService {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Day trading preset - moderate timeframes (1h, 4h)
|
||||
*/
|
||||
async captureDayTradingPreset(symbol: string, analyze: boolean = false): Promise<ScreenshotResult[]> {
|
||||
return this.captureParallel({
|
||||
symbol,
|
||||
preset: 'day-trading',
|
||||
layouts: ['ai', 'diy'],
|
||||
analyze
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Swing trading preset - longer timeframes (4h, 1D)
|
||||
*/
|
||||
async captureSwingTradingPreset(symbol: string, analyze: boolean = false): Promise<ScreenshotResult[]> {
|
||||
return this.captureParallel({
|
||||
symbol,
|
||||
preset: 'swing-trading',
|
||||
layouts: ['ai', 'diy'],
|
||||
analyze
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Extended preset - comprehensive timeframe analysis
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user