Fix instant-reversal filter: Use direct Prisma query instead of incorrect array filter

This commit is contained in:
mindesbunister
2025-12-18 09:56:42 +01:00
parent f1c4548dca
commit bf52a44b31

View File

@@ -377,16 +377,29 @@ export async function POST(request: NextRequest): Promise<NextResponse<RiskCheck
// Detect if most recent trade on this symbol was stopped out within 5 minutes
// This prevents re-entering immediately after being whipsawed
if (hasContextMetrics && body.timeframe === '5') {
const recentTrades = await getTradesInLastHour()
const symbolRecentTrades = recentTrades.filter(t =>
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