feat: implement signal frequency penalties for flip-flop detection
PHASE 1 IMPLEMENTATION: Signal quality scoring now checks database for recent trading patterns and applies penalties to prevent overtrading and flip-flop losses. NEW PENALTIES: 1. Overtrading: 3+ signals in 30min → -20 points - Detects consolidation zones where system generates excessive signals - Counts both executed trades AND blocked signals 2. Flip-flop: Opposite direction in last 15min → -25 points - Prevents rapid long→short→long whipsaws - Example: SHORT at 10:00, LONG at 10:12 = blocked 3. Alternating pattern: Last 3 trades flip directions → -30 points - Detects choppy market conditions - Pattern like long→short→long = system getting chopped DATABASE INTEGRATION: - New function: getRecentSignals() in lib/database/trades.ts - Queries last 30min of trades + blocked signals - Checks last 3 executed trades for alternating pattern - Zero performance impact (fast indexed queries) ARCHITECTURE: - scoreSignalQuality() now async (requires database access) - All callers updated: check-risk, execute, reentry-check - skipFrequencyCheck flag available for special cases - Frequency penalties included in qualityResult breakdown EXPECTED IMPACT: - Eliminate overnight flip-flop losses (like SOL $141-145 chop) - Reduce overtrading during sideways consolidation - Better capital preservation in non-trending markets - Should improve win rate by 5-10% by avoiding worst setups TESTING: - Deploy and monitor next 5 signals in choppy markets - Check logs for frequency penalty messages - Analyze if blocked signals would have been losers Files changed: - lib/database/trades.ts: Added getRecentSignals() - lib/trading/signal-quality.ts: Made async, added frequency checks - app/api/trading/check-risk/route.ts: await + symbol parameter - app/api/trading/execute/route.ts: await + symbol parameter - app/api/analytics/reentry-check/route.ts: await + skipFrequencyCheck
This commit is contained in:
@@ -139,13 +139,15 @@ export async function POST(request: NextRequest) {
|
||||
console.log(`📊 Recent performance: ${last3Count} trades, ${winRate.toFixed(0)}% WR, ${avgPnL.toFixed(2)}% avg P&L`)
|
||||
|
||||
// 3. Score the re-entry with real/fallback metrics
|
||||
const qualityResult = scoreSignalQuality({
|
||||
const qualityResult = await scoreSignalQuality({
|
||||
atr: metrics.atr,
|
||||
adx: metrics.adx,
|
||||
rsi: metrics.rsi,
|
||||
volumeRatio: metrics.volumeRatio,
|
||||
pricePosition: metrics.pricePosition,
|
||||
direction: direction as 'long' | 'short'
|
||||
direction: direction as 'long' | 'short',
|
||||
symbol: symbol,
|
||||
skipFrequencyCheck: true, // Re-entry check already considers recent trades
|
||||
})
|
||||
|
||||
let finalScore = qualityResult.score
|
||||
|
||||
Reference in New Issue
Block a user