diff --git a/lib/automation-service-simple.ts b/lib/automation-service-simple.ts index 7c46269..cc4146b 100644 --- a/lib/automation-service-simple.ts +++ b/lib/automation-service-simple.ts @@ -37,6 +37,9 @@ export interface AutomationStatus { nextScheduled?: Date errorCount: number lastError?: string + nextAnalysisIn?: number // Seconds until next analysis + analysisInterval?: number // Analysis interval in seconds + currentCycle?: number // Current automation cycle } export class AutomationService { @@ -184,6 +187,26 @@ export class AutomationService { try { console.log(`🔍 Running automation cycle for ${this.config.symbol} ${this.config.timeframe}`) + // Update next scheduled time in database for timer display + const intervalMs = this.getIntervalFromTimeframe(this.config.timeframe) + const nextScheduled = new Date(Date.now() + intervalMs) + + try { + await prisma.automationSession.updateMany({ + where: { + userId: this.config.userId, + status: 'ACTIVE' + }, + data: { + nextScheduled: nextScheduled, + lastAnalysis: new Date() + } + }) + console.log(`⏰ Next analysis scheduled for: ${nextScheduled.toLocaleTimeString()}`) + } catch (dbError) { + console.error('Failed to update next scheduled time:', dbError) + } + // Step 1: Check daily trade limit const todayTrades = await this.getTodayTradeCount(this.config.userId) if (todayTrades >= this.config.maxDailyTrades) { @@ -1132,6 +1155,16 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r. console.log('🔄 Found active session but automation not running, attempting auto-restart...') await this.autoRestartFromSession(session) } + + // Calculate next analysis timing + const analysisInterval = 300 // 5 minutes in seconds + let nextAnalysisIn = 0 + + if (this.isRunning && session.nextScheduled) { + const nextScheduledTime = new Date(session.nextScheduled).getTime() + const currentTime = Date.now() + nextAnalysisIn = Math.max(0, Math.floor((nextScheduledTime - currentTime) / 1000)) + } return { isActive: this.isRunning && this.config !== null, @@ -1146,7 +1179,10 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r. lastError: session.lastError || undefined, lastAnalysis: session.lastAnalysis || undefined, lastTrade: session.lastTrade || undefined, - nextScheduled: session.nextScheduled || undefined + nextScheduled: session.nextScheduled || undefined, + nextAnalysisIn: nextAnalysisIn, + analysisInterval: analysisInterval, + currentCycle: session.totalTrades || 0 } } catch (error) { console.error('Failed to get automation status:', error) diff --git a/prisma/prisma/dev.db b/prisma/prisma/dev.db index 94c9b8a..6f5c64c 100644 Binary files a/prisma/prisma/dev.db and b/prisma/prisma/dev.db differ