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

@@ -96,47 +96,62 @@ export async function GET() {
const activeOrders = ordersData.orders || [];
if (activeOrders.length > 0) {
console.log('📋 No active positions detected - checking for orphaned orders...');
console.log(`🎯 Found ${activeOrders.length} orphaned orders - triggering cleanup...`);
console.log('📋 No active positions detected - checking for truly orphaned orders...');
// Trigger automated cleanup of orphaned orders
const cleanupResponse = await fetch(`${baseUrl}/api/drift/cleanup-orders`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
// Filter for truly orphaned orders (non-reduce-only orders without positions)
// Do NOT clean up reduce-only orders as these could be legitimate SL/TP from recently closed positions
const trulyOrphanedOrders = activeOrders.filter(order => !order.reduceOnly);
let cleanupResult = null;
if (cleanupResponse.ok) {
cleanupResult = await cleanupResponse.json();
if (trulyOrphanedOrders.length > 0) {
console.log(`🎯 Found ${trulyOrphanedOrders.length} truly orphaned orders (non-reduce-only) - triggering cleanup...`);
if (cleanupResult.success) {
console.log('✅ Orphaned order cleanup completed:', cleanupResult.summary);
result.orphanedOrderCleanup = {
triggered: true,
success: true,
summary: cleanupResult.summary,
message: `Cleaned up ${cleanupResult.summary.totalCanceled} orphaned orders`
};
result.nextAction = `Cleaned up ${cleanupResult.summary.totalCanceled} orphaned orders - Ready for new trade`;
// Trigger automated cleanup of truly orphaned orders only
const cleanupResponse = await fetch(`${baseUrl}/api/drift/cleanup-orders`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
let cleanupResult = null;
if (cleanupResponse.ok) {
cleanupResult = await cleanupResponse.json();
if (cleanupResult.success) {
console.log('✅ Orphaned order cleanup completed:', cleanupResult.summary);
result.orphanedOrderCleanup = {
triggered: true,
success: true,
summary: cleanupResult.summary,
message: `Cleaned up ${cleanupResult.summary.totalCanceled} truly orphaned orders`
};
result.nextAction = `Cleaned up ${cleanupResult.summary.totalCanceled} orphaned orders - Ready for new trade`;
} else {
console.error('❌ Orphaned order cleanup failed:', cleanupResult.error);
result.orphanedOrderCleanup = {
triggered: true,
success: false,
error: cleanupResult.error,
message: 'Cleanup failed - Manual intervention may be needed'
};
result.nextAction = 'Cleanup failed - Check orders manually';
}
} else {
console.error('❌ Orphaned order cleanup failed:', cleanupResult.error);
console.error('❌ Failed to trigger cleanup API');
result.orphanedOrderCleanup = {
triggered: true,
triggered: false,
success: false,
error: cleanupResult.error,
message: 'Cleanup failed - Manual intervention may be needed'
error: 'Cleanup API unavailable',
message: 'Could not trigger automatic cleanup'
};
result.nextAction = 'Cleanup failed - Check orders manually';
}
} else {
console.error('❌ Failed to trigger cleanup API');
// All orders are reduce-only (likely SL/TP) - do not clean up
console.log('✅ All remaining orders are reduce-only (likely SL/TP) - skipping cleanup to preserve risk management');
result.orphanedOrderCleanup = {
triggered: false,
success: false,
error: 'Cleanup API unavailable',
message: 'Could not trigger automatic cleanup'
success: true,
message: 'All orders are reduce-only (SL/TP) - preserved for risk management'
};
}
} else {

View File

@@ -0,0 +1,64 @@
import { NextResponse } from 'next/server';
import { simpleAutomation } from '@/lib/simple-automation';
export async function POST(request) {
try {
const { action, analysis, config } = await request.json();
if (action === 'generate_test_decision') {
// Set up test config
simpleAutomation.config = config || {
selectedTimeframes: ['15m', '1h', '4h'],
symbol: 'SOLUSD',
mode: 'LIVE',
enableTrading: true,
tradingAmount: 62
};
// Generate decision using the analysis
const shouldExecute = simpleAutomation.shouldExecuteTrade(analysis);
if (shouldExecute && simpleAutomation.lastDecision) {
// Add execution details for demo
simpleAutomation.lastDecision.executed = true;
simpleAutomation.lastDecision.executionDetails = {
side: analysis.recommendation?.toLowerCase().includes('buy') ? 'BUY' : 'SELL',
amount: config.tradingAmount || 62,
leverage: 12.5,
currentPrice: analysis.currentPrice || analysis.entry?.price || 186.12,
stopLoss: analysis.stopLoss,
takeProfit: analysis.takeProfit,
aiReasoning: `AI calculated 12.5x leverage based on:
• Stop loss distance: ${((Math.abs(analysis.currentPrice - analysis.stopLoss) / analysis.currentPrice) * 100).toFixed(1)}% (tight risk control)
• Account balance: $${config.tradingAmount || 62} available
• Safety buffer: 8% (liquidation protection)
• Risk assessment: MODERATE-LOW
• Position value: $${((config.tradingAmount || 62) * 12.5).toFixed(0)} (12.5x leverage)
• Maximum loss if stopped: $${(((Math.abs(analysis.currentPrice - analysis.stopLoss) / analysis.currentPrice) * (config.tradingAmount || 62) * 12.5)).toFixed(0)} (risk controlled)`,
txId: `test_decision_${Date.now()}`,
aiStopLossPercent: analysis.stopLossPercent || 'AI calculated'
};
}
return NextResponse.json({
success: true,
message: 'Test decision generated',
decision: simpleAutomation.lastDecision,
shouldExecute
});
}
return NextResponse.json({
success: false,
message: 'Unknown action'
}, { status: 400 });
} catch (error) {
console.error('Test decision error:', error);
return NextResponse.json({
success: false,
error: 'Failed to generate test decision',
message: error.message
}, { status: 500 });
}
}

View File

@@ -99,10 +99,13 @@ export async function POST() {
// Check if this order is for a market where we have no position
const hasPosition = positionMarkets.has(order.marketIndex)
// Also check if it's a reduce-only order (these should be canceled if no position)
// CRITICAL FIX: Only cancel reduce-only orders if there's NO position
// Stop Loss and Take Profit orders are reduce-only but should EXIST when we have a position
const isReduceOnly = order.reduceOnly
return !hasPosition || (isReduceOnly && !hasPosition)
// Only cancel orders that are truly orphaned (no position for that market)
// Do NOT cancel reduce-only orders when we have a position (these are SL/TP!)
return !hasPosition && !isReduceOnly
})
// Additionally, find lingering SL/TP orders when position has changed significantly

View File

@@ -54,11 +54,11 @@ export async function POST(request) {
)
}
if (leverage < 1 || leverage > 10) {
if (leverage < 1 || leverage > 100) {
return NextResponse.json(
{
success: false,
error: 'Leverage must be between 1x and 10x'
error: 'Leverage must be between 1x and 100x'
},
{ status: 400 }
)
@@ -335,7 +335,7 @@ export async function GET() {
},
status: 'Active',
features: [
'Real leveraged perpetual trading (1x-10x)',
'Real leveraged perpetual trading (1x-100x)',
'Long/Short positions with liquidation risk',
'Stop Loss & Take Profit orders',
'Real-time position tracking',