Enhanced trade analysis display and fixed automation persistence

- Enhanced frontend trade display with comprehensive analysis details
  * Added trigger analysis showing original trade signals and confidence
  * Added current metrics for active trades (P&L, time in trade, price changes)
  * Added exit analysis for completed trades (accuracy, actual vs expected R/R)
  * Added detailed trade context explaining analysis-trade relationships

- Fixed automation session persistence after server restarts
  * Modified getStatus() to check database first instead of in-memory state
  * Added auto-restart functionality when active session exists but automation stopped
  * Improved session tracking and state management

- Enhanced API response structure
  * Added triggerAnalysis, currentMetrics, exitMetrics to trade objects
  * Added analysisContext explaining signal changes (BUY → HOLD scenarios)
  * Added comprehensive trade quality assessment and performance tracking

Features:
 Detailed analysis-trade correlation display
 Real-time P&L tracking for active trades
 Analysis accuracy assessment for completed trades
 Automation session persistence across server restarts
 Enhanced trade information with meaningful context
This commit is contained in:
mindesbunister
2025-07-18 23:31:19 +02:00
parent 34a29c6056
commit 64579c231c
3 changed files with 301 additions and 55 deletions

View File

@@ -728,12 +728,7 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r.
async getStatus(): Promise<AutomationStatus | null> {
try {
// If automation is not running in memory, return null regardless of database state
if (!this.isRunning || !this.config) {
return null
}
// Get the latest active automation session from database
// Get the latest active automation session from database first
const session = await prisma.automationSession.findFirst({
where: { status: 'ACTIVE' },
orderBy: { createdAt: 'desc' }
@@ -743,8 +738,18 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r.
return null
}
// If we have a session but automation is not running in memory,
// it means the server was restarted but the session is still active
const isActiveInMemory = this.isRunning && this.config !== null
// Auto-restart automation if session exists but not running in memory
if (!isActiveInMemory) {
console.log('🔄 Found active session but automation not running, attempting auto-restart...')
await this.autoRestartFromSession(session)
}
return {
isActive: this.isRunning,
isActive: this.isRunning && this.config !== null,
mode: session.mode as 'SIMULATION' | 'LIVE',
symbol: session.symbol,
timeframe: session.timeframe,
@@ -764,6 +769,29 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r.
}
}
private async autoRestartFromSession(session: any): Promise<void> {
try {
const settings = session.settings || {}
const config: AutomationConfig = {
userId: session.userId,
mode: session.mode,
symbol: session.symbol,
timeframe: session.timeframe,
tradingAmount: settings.tradingAmount || 100,
maxLeverage: settings.maxLeverage || 3,
stopLossPercent: settings.stopLossPercent || 2,
takeProfitPercent: settings.takeProfitPercent || 6,
maxDailyTrades: settings.maxDailyTrades || 5,
riskPercentage: settings.riskPercentage || 2
}
await this.startAutomation(config)
console.log('✅ Automation auto-restarted successfully')
} catch (error) {
console.error('Failed to auto-restart automation:', error)
}
}
async getLearningInsights(userId: string): Promise<{
totalAnalyses: number
avgAccuracy: number