From bf52a44b317834cd63a24f40a2f5f413a57188ae Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Thu, 18 Dec 2025 09:56:42 +0100 Subject: [PATCH] Fix instant-reversal filter: Use direct Prisma query instead of incorrect array filter --- app/api/trading/check-risk/route.ts | 31 ++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/app/api/trading/check-risk/route.ts b/app/api/trading/check-risk/route.ts index 140d2ac..01d0fb7 100644 --- a/app/api/trading/check-risk/route.ts +++ b/app/api/trading/check-risk/route.ts @@ -377,16 +377,29 @@ export async function POST(request: NextRequest): Promise - t.symbol === body.symbol && - t.exitReason === 'SL' && - t.holdTimeSeconds !== null && - t.holdTimeSeconds <= 300 // 5 minutes = 1 candle - ) + const { getPrismaClient } = await import('@/lib/database/trades') + const prisma = getPrismaClient() - if (symbolRecentTrades.length > 0) { - const lastFastSL = symbolRecentTrades[0] + const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000) + const recentFastSLTrades = await prisma.trade.findMany({ + where: { + symbol: body.symbol, + exitReason: 'SL', + exitTime: { + gte: oneHourAgo, + }, + holdTimeSeconds: { + lte: 300, // 5 minutes = 1 candle + }, + }, + orderBy: { + exitTime: 'desc', + }, + take: 1, + }) + + if (recentFastSLTrades.length > 0) { + const lastFastSL = recentFastSLTrades[0] const timeSinceLastSL = Date.now() - new Date(lastFastSL.exitTime!).getTime() const cooldownMs = 15 * 60 * 1000 // 15 minute cooldown after instant reversal