Implemented 3 critical risk checks in /api/trading/check-risk: 1. Daily Drawdown Check - Blocks trades if today's P&L < maxDailyDrawdown - Prevents catastrophic daily losses - Currently: -0 limit (configurable via MAX_DAILY_DRAWDOWN) 2. Hourly Trade Limit - Blocks trades if tradesInLastHour >= maxTradesPerHour - Prevents overtrading / algorithm malfunction - Currently: 20 trades/hour (configurable via MAX_TRADES_PER_HOUR) 3. Cooldown Period - Blocks trades if timeSinceLastTrade < minTimeBetweenTrades - Enforces breathing room between trades - Uses minutes (not seconds) thanks to previous commit - Currently: 0 min = disabled (configurable via MIN_TIME_BETWEEN_TRADES) Added database helper functions: - getLastTradeTime() - Returns timestamp of most recent trade - getTradesInLastHour() - Counts trades in last 60 minutes - getTodayPnL() - Sums realized P&L since midnight All checks include detailed logging with values and thresholds. Risk check called by n8n workflow before every trade execution.
162 lines
5.3 KiB
TypeScript
162 lines
5.3 KiB
TypeScript
/**
|
|
* 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<NextResponse<RiskCheckResponse>> {
|
|
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 }
|
|
)
|
|
}
|
|
}
|