feat: Complete live trading decisions visibility system
LIVE TRADING ANALYSIS PANEL - Real-time decision tracking - Live decisions API endpoint (/api/automation/live-decisions) - Complete automation-v2 page with enhanced AI trading analysis - Real-time visibility into AI's trading decisions and reasoning - Block reason display showing why trades are prevented - Execution details with entry, SL, TP, leverage, and reasoning - Auto-refreshing decision history (30-second intervals) - Enhanced risk management integration MANDATORY RISK MANAGEMENT SYSTEM - Mandatory risk manager with strict validation - Emergency position protection system - Stop loss direction validation (below entry for BUY, above for SELL) - Integration with automation system for real-time blocking AUTOMATION PAGE ENHANCEMENT - All original automation-v2 features preserved - Multi-timeframe selection with presets - Trading configuration controls - Account balance and position monitoring - Enhanced AI Learning Panel integration - Live status indicators and feedback COMPREHENSIVE TESTING - Live decisions API testing harness - Risk management validation tests - Sample decision data for development The system now provides complete transparency into: - ✅ Trade execution decisions with full reasoning - ✅ Risk management blocks with specific reasons - ✅ AI analysis and confidence levels - ✅ Real-time decision tracking and history - ✅ Entry, stop loss, take profit details - ✅ Leverage calculations and risk assessment Tested and working on development container (port 9001:3000)
This commit is contained in:
59
app/api/automation/live-decisions/route.js
Normal file
59
app/api/automation/live-decisions/route.js
Normal file
@@ -0,0 +1,59 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
|
||||
// In-memory store for live trading decisions and risk management blocks
|
||||
let liveDecisions = []
|
||||
let maxDecisions = 10 // Keep last 10 decisions
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
// Return the most recent decisions with full context
|
||||
const response = {
|
||||
success: true,
|
||||
decisions: liveDecisions,
|
||||
latest: liveDecisions[0] || null,
|
||||
timestamp: new Date().toISOString()
|
||||
}
|
||||
|
||||
return NextResponse.json(response)
|
||||
} catch (error) {
|
||||
console.error('❌ Live decisions API error:', error)
|
||||
return NextResponse.json(
|
||||
{ success: false, error: error.message },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request) {
|
||||
try {
|
||||
const decision = await request.json()
|
||||
|
||||
// Add timestamp if not provided
|
||||
if (!decision.timestamp) {
|
||||
decision.timestamp = new Date().toISOString()
|
||||
}
|
||||
|
||||
// Add to the beginning of the array (most recent first)
|
||||
liveDecisions.unshift(decision)
|
||||
|
||||
// Keep only the last maxDecisions
|
||||
if (liveDecisions.length > maxDecisions) {
|
||||
liveDecisions = liveDecisions.slice(0, maxDecisions)
|
||||
}
|
||||
|
||||
console.log('📊 Live decision recorded:', {
|
||||
type: decision.type,
|
||||
action: decision.action,
|
||||
blocked: decision.blocked,
|
||||
confidence: decision.confidence
|
||||
})
|
||||
|
||||
return NextResponse.json({ success: true, total: liveDecisions.length })
|
||||
} catch (error) {
|
||||
console.error('❌ Live decisions POST error:', error)
|
||||
return NextResponse.json(
|
||||
{ success: false, error: error.message },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,45 @@ export async function POST(request) {
|
||||
)
|
||||
}
|
||||
|
||||
// 🛡️ MANDATORY RISK MANAGEMENT VALIDATION - NO TRADE WITHOUT SL/TP
|
||||
if (!stopLoss && !takeProfit) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: 'RISK MANAGEMENT REQUIRED: Both stop-loss and take-profit are missing',
|
||||
details: 'Every trade must have proper risk management. Provide at least stop-loss or take-profit.',
|
||||
riskManagementFailed: true
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
if (!stopLoss) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: 'STOP-LOSS REQUIRED: No stop-loss provided',
|
||||
details: 'Every trade must have a stop-loss to limit potential losses.',
|
||||
riskManagementFailed: true
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
if (!takeProfit) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: 'TAKE-PROFIT REQUIRED: No take-profit provided',
|
||||
details: 'Every trade must have a take-profit to secure gains.',
|
||||
riskManagementFailed: true
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
console.log(`✅ RISK MANAGEMENT VALIDATION PASSED - SL: $${stopLoss}, TP: $${takeProfit}`);
|
||||
|
||||
if (!useRealDEX) {
|
||||
// Simulation mode
|
||||
console.log('🎮 Executing SIMULATED Drift perpetual trade')
|
||||
|
||||
Reference in New Issue
Block a user