/** * Risk Check API Endpoint * * Called by n8n workflow before executing trade * POST /api/trading/check-risk */ import { NextRequest, NextResponse } from 'next/server' import { getMergedConfig } from '@/config/trading' import { getInitializedPositionManager } from '@/lib/trading/position-manager' import { getLastTradeTime, getTradesInLastHour, getTodayPnL } from '@/lib/database/trades' export interface RiskCheckRequest { symbol: string direction: 'long' | 'short' } export interface RiskCheckResponse { allowed: boolean reason?: string details?: string } export async function POST(request: NextRequest): Promise> { try { // Verify authorization const authHeader = request.headers.get('authorization') const expectedAuth = `Bearer ${process.env.API_SECRET_KEY}` if (!authHeader || authHeader !== expectedAuth) { return NextResponse.json( { allowed: false, reason: 'Unauthorized', }, { status: 401 } ) } const body: RiskCheckRequest = await request.json() console.log('🔍 Risk check for:', body) const config = getMergedConfig() // Check for existing positions on the same symbol const positionManager = await getInitializedPositionManager() const existingTrades = Array.from(positionManager.getActiveTrades().values()) const existingPosition = existingTrades.find(trade => trade.symbol === body.symbol) if (existingPosition) { // Check if it's the SAME direction (duplicate - block it) if (existingPosition.direction === body.direction) { console.log('🚫 Risk check BLOCKED: Duplicate position (same direction)', { symbol: body.symbol, existingDirection: existingPosition.direction, requestedDirection: body.direction, existingEntry: existingPosition.entryPrice, }) return NextResponse.json({ allowed: false, reason: 'Duplicate position', details: `Already have ${existingPosition.direction} position on ${body.symbol} (entry: $${existingPosition.entryPrice})`, }) } // OPPOSITE direction - this is a signal flip/reversal (ALLOW IT) console.log('🔄 Risk check: Signal flip detected', { symbol: body.symbol, existingDirection: existingPosition.direction, newDirection: body.direction, note: 'Will close existing and open opposite', }) return NextResponse.json({ allowed: true, reason: 'Signal flip', details: `Signal reversed from ${existingPosition.direction} to ${body.direction} - will flip position`, }) } // 1. Check daily drawdown limit const todayPnL = await getTodayPnL() if (todayPnL < config.maxDailyDrawdown) { console.log('🚫 Risk check BLOCKED: Daily drawdown limit reached', { todayPnL: todayPnL.toFixed(2), maxDrawdown: config.maxDailyDrawdown, }) return NextResponse.json({ allowed: false, reason: 'Daily drawdown limit', details: `Today's P&L ($${todayPnL.toFixed(2)}) has reached max drawdown limit ($${config.maxDailyDrawdown})`, }) } // 2. Check trades per hour limit const tradesInLastHour = await getTradesInLastHour() if (tradesInLastHour >= config.maxTradesPerHour) { console.log('🚫 Risk check BLOCKED: Hourly trade limit reached', { tradesInLastHour, maxTradesPerHour: config.maxTradesPerHour, }) return NextResponse.json({ allowed: false, reason: 'Hourly trade limit', details: `Already placed ${tradesInLastHour} trades in the last hour (max: ${config.maxTradesPerHour})`, }) } // 3. Check cooldown period const lastTradeTime = await getLastTradeTime() if (lastTradeTime && config.minTimeBetweenTrades > 0) { const timeSinceLastTrade = Date.now() - lastTradeTime.getTime() const cooldownMs = config.minTimeBetweenTrades * 60 * 1000 // Convert minutes to milliseconds if (timeSinceLastTrade < cooldownMs) { const remainingMs = cooldownMs - timeSinceLastTrade const remainingMinutes = Math.ceil(remainingMs / 60000) console.log('🚫 Risk check BLOCKED: Cooldown period active', { lastTradeTime: lastTradeTime.toISOString(), timeSinceLastTradeMs: timeSinceLastTrade, cooldownMs, remainingMinutes, }) return NextResponse.json({ allowed: false, reason: 'Cooldown period', details: `Must wait ${remainingMinutes} more minute(s) before next trade (cooldown: ${config.minTimeBetweenTrades} min)`, }) } } console.log(`✅ Risk check PASSED: All checks passed`, { todayPnL: todayPnL.toFixed(2), tradesLastHour: tradesInLastHour, cooldownPassed: lastTradeTime ? 'yes' : 'no previous trades', }) return NextResponse.json({ allowed: true, details: 'All risk checks passed', }) } catch (error) { console.error('❌ Risk check error:', error) return NextResponse.json( { allowed: false, reason: 'Risk check failed', details: error instanceof Error ? error.message : 'Unknown error', }, { status: 500 } ) } }