import { NextResponse } from 'next/server'; export async function GET() { try { // Get current positions const baseUrl = process.env.INTERNAL_API_URL || 'http://localhost:3000'; const positionsResponse = await fetch(`${baseUrl}/api/drift/positions`); const positionsData = await positionsResponse.json(); // Get current price (you'd typically get this from an oracle) const currentPrice = 177.63; // Placeholder - should come from price feed const result = { timestamp: new Date().toISOString(), hasPosition: false, position: null, stopLossProximity: null, riskLevel: 'NONE', nextAction: 'No position to monitor', recommendation: 'START_TRADING' }; if (positionsData.success && positionsData.positions.length > 0) { const position = positionsData.positions[0]; result.hasPosition = true; result.position = { symbol: position.symbol, side: position.side, size: position.size, entryPrice: position.entryPrice, currentPrice: currentPrice, unrealizedPnl: position.unrealizedPnl, notionalValue: position.notionalValue }; // Calculate stop loss proximity (mock - you'd need actual SL from order data) let stopLossPrice; if (position.side === 'long') { stopLossPrice = position.entryPrice * 0.95; // 5% below entry } else { stopLossPrice = position.entryPrice * 1.05; // 5% above entry } const distanceToSL = Math.abs(currentPrice - stopLossPrice) / currentPrice; const proximityPercent = distanceToSL * 100; result.stopLossProximity = { stopLossPrice: stopLossPrice, currentPrice: currentPrice, distancePercent: proximityPercent.toFixed(2), isNear: proximityPercent < 2.0 // Within 2% = NEAR }; // Autonomous AI Risk Management if (proximityPercent < 1.0) { result.riskLevel = 'CRITICAL'; result.nextAction = 'AI EXECUTING: Emergency exit analysis - Considering position closure'; result.recommendation = 'AI_EMERGENCY_EXIT'; result.aiAction = 'EMERGENCY_ANALYSIS'; } else if (proximityPercent < 2.0) { result.riskLevel = 'HIGH'; result.nextAction = 'AI ACTIVE: Reassessing position - May adjust stop loss or exit'; result.recommendation = 'AI_POSITION_REVIEW'; result.aiAction = 'URGENT_REASSESSMENT'; } else if (proximityPercent < 5.0) { result.riskLevel = 'MEDIUM'; result.nextAction = 'AI MONITORING: Enhanced analysis - Preparing contingency plans'; result.recommendation = 'AI_ENHANCED_WATCH'; result.aiAction = 'ENHANCED_ANALYSIS'; } else if (proximityPercent < 10.0) { result.riskLevel = 'LOW'; result.nextAction = 'AI TRACKING: Standard monitoring - Position within normal range'; result.recommendation = 'AI_NORMAL_WATCH'; result.aiAction = 'STANDARD_MONITORING'; } else { result.riskLevel = 'SAFE'; result.nextAction = 'AI RELAXED: Position secure - Looking for new opportunities'; result.recommendation = 'AI_OPPORTUNITY_SCAN'; result.aiAction = 'OPPORTUNITY_SCANNING'; } } return NextResponse.json({ success: true, monitor: result }); } catch (error) { console.error('Position monitor error:', error); return NextResponse.json({ success: false, error: 'Failed to get position monitoring data', message: error.message }, { status: 500 }); } }