feat: implement comprehensive AI decision display and reasoning panel

Major Features Added:
- Complete AI decision tracking system with detailed reasoning display
- Prominent gradient-styled AI reasoning panel on automation-v2 page
- Test AI decision generator with realistic trading scenarios
- Enhanced decision transparency showing entry/exit logic and leverage calculations

- Fixed orphaned order cleanup to preserve reduce-only SL/TP orders
- Integrated AI leverage calculator with 100x capability (up from 10x limit)
- Added lastDecision property to automation status for UI display
- Enhanced position monitoring with better cleanup triggers

- Beautiful gradient-styled AI Trading Analysis panel
- Color-coded confidence levels and recommendation displays
- Detailed breakdown of entry strategy, stop loss logic, and take profit targets
- Real-time display of AI leverage reasoning with safety buffer explanations
- Test AI button for demonstration of decision-making process

- SL/TP orders now execute properly (fixed cleanup interference)
- AI calculates sophisticated leverage (8.8x-42.2x vs previous 1x hardcoded)
- Complete decision audit trail with execution details
- Risk management transparency with liquidation safety calculations

- Why This Decision? - Prominent reasoning section
- Entry & Exit Strategy - Price levels with color coding
- AI Leverage Decision - Detailed calculation explanations
- Execution status with success/failure indicators
- Transaction IDs and comprehensive trade details

All systems now provide full transparency of AI decision-making process.
This commit is contained in:
mindesbunister
2025-07-26 22:41:55 +02:00
parent 30eb869ca4
commit 167d7ff5bc
23 changed files with 3233 additions and 52 deletions

View File

@@ -0,0 +1,104 @@
// Test to demonstrate the complete AI decision display functionality
async function demonstrateDecisionDisplay() {
console.log('🧪 Demonstrating Complete AI Decision Display\n');
// Import the automation system
const { simpleAutomation } = require('./lib/simple-automation.js');
// Mock a realistic trading analysis with AI leverage calculation
const mockAnalysis = {
recommendation: 'STRONG BUY',
confidence: 87,
reasoning: 'RSI showing oversold recovery (34→52), MACD bullish crossover confirmed, volume spike +180% above average. Price broke resistance at $185.80 with strong momentum. All timeframes aligned bullish.',
summary: '5 of 6 technical indicators bullish. High probability upward continuation expected.',
stopLoss: 184.20,
takeProfit: 189.75,
entry: { price: 186.45 },
currentPrice: 186.45,
stopLossPercent: '1.2% below entry'
};
// Simulate automation config
simpleAutomation.config = {
selectedTimeframes: ['5m', '15m', '1h'],
symbol: 'SOLUSD',
mode: 'LIVE',
enableTrading: true,
tradingAmount: 50
};
console.log('📊 Simulating AI Analysis Decision...');
// Test the decision logic
const shouldExecute = simpleAutomation.shouldExecuteTrade(mockAnalysis);
console.log(`✅ Trade Decision: ${shouldExecute ? 'EXECUTE' : 'SKIP'}`);
if (shouldExecute) {
console.log('\n💰 Simulating Trade Execution...');
// Manually create execution details (simulating successful trade)
if (simpleAutomation.lastDecision) {
simpleAutomation.lastDecision.executed = true;
simpleAutomation.lastDecision.executionDetails = {
side: 'BUY',
amount: 50,
leverage: 8.7, // AI calculated leverage
currentPrice: 186.45,
stopLoss: 184.20,
takeProfit: 189.75,
aiReasoning: 'AI calculated 8.7x leverage based on 1.2% stop loss distance, $50 position size, and 10% safety buffer. Liquidation price: $165.20 (11.4% below entry). Risk assessment: MODERATE',
txId: 'demo_tx_' + Date.now(),
aiStopLossPercent: '1.2% below entry'
};
}
}
// Show what the API status will return
const status = simpleAutomation.getStatus();
console.log('\n🔍 API Status Response (/api/automation/status):');
console.log(JSON.stringify({
isActive: status.isActive,
symbol: status.symbol,
mode: status.mode,
lastDecision: status.lastDecision
}, null, 2));
console.log('\n📱 UI Display Preview (automation-v2 page):');
if (status.lastDecision) {
const decision = status.lastDecision;
console.log('┌─ Last Decision Panel ─────────────────────────────────┐');
console.log(`│ Status: ${decision.executed ? '✅ EXECUTED' : '❌ NOT EXECUTED'}`);
console.log(`│ Time: ${new Date(decision.timestamp).toLocaleTimeString()}`);
console.log(`│ │`);
console.log(`│ Recommendation: ${decision.recommendation.padEnd(20)}`);
console.log(`│ Confidence: ${decision.confidence}% (min: ${decision.minConfidenceRequired}%) │`);
console.log(`│ │`);
console.log(`│ Reasoning: │`);
console.log(`${decision.reasoning.substring(0, 50)}... │`);
if (decision.executed && decision.executionDetails) {
const exec = decision.executionDetails;
console.log(`│ │`);
console.log(`│ 💰 Execution Details: │`);
console.log(`│ Side: ${exec.side} Amount: $${exec.amount} Leverage: ${exec.leverage}x │`);
console.log(`│ Entry: $${exec.currentPrice} SL: $${exec.stopLoss} TP: $${exec.takeProfit}`);
console.log(`│ │`);
console.log(`│ 🧠 AI Leverage Decision: │`);
console.log(`${exec.aiReasoning.substring(0, 50)}... │`);
}
console.log('└────────────────────────────────────────────────────────┘');
}
console.log('\n✅ Demonstration Complete!');
console.log('📝 When you run automation on the automation-v2 page:');
console.log(' 1. AI will analyze charts and make decisions');
console.log(' 2. All decisions will be stored with reasoning');
console.log(' 3. Leverage calculations will show AI thinking');
console.log(' 4. Stop loss and take profit levels will be displayed');
console.log(' 5. Execution details will show actual trade results');
console.log('\n🎯 The "Last Decision" panel will show all this information in real-time!');
}
demonstrateDecisionDisplay().catch(console.error);