Fix automated trading display calculations

Fixed position size calculation: 00 investment now shows 00 position (was 04.76)
 Fixed token amount display: Now shows correct tokens (~0.996) for 00 investment (was 2.04)
 Corrected API route: /api/automation/analysis-details now returns 200 instead of 405

Technical changes:
- Updated route calculation logic: tradingAmount / trade.price for correct token amounts
- Fixed displayPositionSize to show intended investment amount
- Used Docker Compose v2 for container management
- Resolved Next.js module export issues

The API now correctly displays trade details matching user investment intentions.
This commit is contained in:
mindesbunister
2025-07-20 22:32:16 +02:00
parent 6ce4f364a9
commit 55cea00e5e
22 changed files with 1180 additions and 189 deletions

View File

@@ -83,8 +83,6 @@ export class EnhancedScreenshotService {
// Check login status and login if needed
const isLoggedIn = await layoutSession.checkLoginStatus()
console.log(`🔍 ${layout.toUpperCase()}: Login status check result: ${isLoggedIn}`)
if (!isLoggedIn) {
console.log(`🔐 Logging in to ${layout} session...`)
if (sessionId && index === 0) {
@@ -104,12 +102,55 @@ export class EnhancedScreenshotService {
progressTracker.updateStep(sessionId, 'navigation', 'active', `Navigating to ${config.symbol} chart...`)
}
// Use the new navigateToLayout method instead of manual URL construction
console.log(`🌐 ${layout.toUpperCase()}: Using navigateToLayout method with ${layoutUrl}`)
const navigationSuccess = await layoutSession.navigateToLayout(layoutUrl, config.symbol, config.timeframe)
// Navigate directly to the specific layout URL with symbol and timeframe
const directUrl = `https://www.tradingview.com/chart/${layoutUrl}/?symbol=${config.symbol}&interval=${config.timeframe}`
console.log(`🌐 ${layout.toUpperCase()}: Navigating directly to ${directUrl}`)
// Get page from the session
const page = (layoutSession as any).page
if (!page) {
throw new Error(`Failed to get page for ${layout} session`)
}
// Navigate directly to the layout URL with retries and progressive timeout strategy
let navigationSuccess = false
for (let attempt = 1; attempt <= 3; attempt++) {
try {
console.log(`🔄 ${layout.toUpperCase()}: Navigation attempt ${attempt}/3`)
// Progressive waiting strategy: first try domcontentloaded, then networkidle if that fails
const waitUntilStrategy = attempt === 1 ? 'domcontentloaded' : 'networkidle0'
const timeoutDuration = attempt === 1 ? 30000 : (60000 + (attempt - 1) * 30000)
console.log(`📋 ${layout.toUpperCase()}: Using waitUntil: ${waitUntilStrategy}, timeout: ${timeoutDuration}ms`)
await page.goto(directUrl, {
waitUntil: waitUntilStrategy,
timeout: timeoutDuration
})
// If we used domcontentloaded, wait a bit more for dynamic content
if (waitUntilStrategy === 'domcontentloaded') {
console.log(`${layout.toUpperCase()}: Waiting additional 5s for dynamic content...`)
await new Promise(resolve => setTimeout(resolve, 5000))
}
navigationSuccess = true
break
} catch (navError: any) {
console.warn(`⚠️ ${layout.toUpperCase()}: Navigation attempt ${attempt} failed:`, navError?.message || navError)
if (attempt === 3) {
throw new Error(`Failed to navigate to ${layout} layout after 3 attempts: ${navError?.message || navError}`)
}
// Progressive backoff
const waitTime = 2000 * attempt
console.log(`${layout.toUpperCase()}: Waiting ${waitTime}ms before retry...`)
await new Promise(resolve => setTimeout(resolve, waitTime))
}
}
if (!navigationSuccess) {
throw new Error(`Failed to navigate to ${layout} layout ${layoutUrl}`)
throw new Error(`Failed to navigate to ${layout} layout`)
}
console.log(`${layout.toUpperCase()}: Successfully navigated to layout`)
@@ -142,12 +183,9 @@ export class EnhancedScreenshotService {
// Strategy 2: Look for chart elements manually
try {
console.log(`🔍 ${layout.toUpperCase()}: Checking for chart elements manually...`)
const page = (layoutSession as any).page
if (page) {
await page.waitForSelector('.layout__area--center', { timeout: 15000 })
console.log(`${layout.toUpperCase()}: Chart area found via selector`)
chartLoadSuccess = true
}
await page.waitForSelector('.layout__area--center', { timeout: 15000 })
console.log(`${layout.toUpperCase()}: Chart area found via selector`)
chartLoadSuccess = true
} catch (selectorError: any) {
console.warn(`⚠️ ${layout.toUpperCase()}: Chart selector check failed:`, selectorError?.message || selectorError)
}
@@ -370,8 +408,27 @@ export class EnhancedScreenshotService {
)
)
// Wait for all cleanup operations to complete
await Promise.allSettled(cleanupPromises)
// Give browsers time to fully close
await new Promise(resolve => setTimeout(resolve, 3000))
console.log('✅ All parallel browser sessions cleaned up')
// Force kill any remaining browser processes
try {
const { exec } = require('child_process')
const { promisify } = require('util')
const execAsync = promisify(exec)
console.log('🔧 Force killing any remaining browser processes...')
await execAsync('pkill -f "chromium.*--remote-debugging-port" 2>/dev/null || true')
await execAsync('pkill -f "chrome.*--remote-debugging-port" 2>/dev/null || true')
console.log('✅ Remaining browser processes terminated')
} catch (processKillError) {
console.error('Error force killing browser processes:', processKillError)
}
}
async healthCheck(): Promise<{ status: 'healthy' | 'error', message?: string }> {