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:
@@ -5,10 +5,17 @@
|
||||
* Ensures consistent scoring across the trading pipeline.
|
||||
*/
|
||||
|
||||
import { getRecentSignals } from '../database/trades'
|
||||
|
||||
export interface SignalQualityResult {
|
||||
score: number
|
||||
passed: boolean
|
||||
reasons: string[]
|
||||
frequencyPenalties?: {
|
||||
overtrading: number
|
||||
flipFlop: number
|
||||
alternating: number
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,6 +29,7 @@ export interface SignalQualityResult {
|
||||
* - Volume: -10 to +15 points
|
||||
* - Price position: -15 to +5 points
|
||||
* - Volume breakout bonus: +10 points
|
||||
* - Signal frequency penalties: -20 to -30 points
|
||||
*
|
||||
* Total range: ~15-115 points (realistically 30-100)
|
||||
* Threshold: 60 points minimum for execution
|
||||
@@ -29,17 +37,24 @@ export interface SignalQualityResult {
|
||||
* TIMEFRAME-AWARE SCORING:
|
||||
* - 5min charts have lower ADX/ATR thresholds (trends develop slower)
|
||||
* - Higher timeframes require stronger confirmation
|
||||
*
|
||||
* SIGNAL FREQUENCY PENALTIES (NEW):
|
||||
* - 3+ signals in 30 min: -20 points (overtrading zone)
|
||||
* - Opposite direction in last 15 min: -25 points (flip-flop)
|
||||
* - Last 3 trades alternating: -30 points (chop pattern)
|
||||
*/
|
||||
export function scoreSignalQuality(params: {
|
||||
export async function scoreSignalQuality(params: {
|
||||
atr: number
|
||||
adx: number
|
||||
rsi: number
|
||||
volumeRatio: number
|
||||
pricePosition: number
|
||||
direction: 'long' | 'short'
|
||||
symbol: string // Required for frequency check
|
||||
timeframe?: string // "5" = 5min, "15" = 15min, "60" = 1H, "D" = daily
|
||||
minScore?: number // Configurable minimum score threshold
|
||||
}): SignalQualityResult {
|
||||
skipFrequencyCheck?: boolean // For testing or when frequency check not needed
|
||||
}): Promise<SignalQualityResult> {
|
||||
let score = 50 // Base score
|
||||
const reasons: string[] = []
|
||||
|
||||
@@ -171,6 +186,49 @@ export function scoreSignalQuality(params: {
|
||||
reasons.push(`Volume breakout compensates for low ATR`)
|
||||
}
|
||||
|
||||
// Signal frequency penalties (check database for recent signals)
|
||||
const frequencyPenalties = {
|
||||
overtrading: 0,
|
||||
flipFlop: 0,
|
||||
alternating: 0,
|
||||
}
|
||||
|
||||
if (!params.skipFrequencyCheck) {
|
||||
try {
|
||||
const recentSignals = await getRecentSignals({
|
||||
symbol: params.symbol,
|
||||
direction: params.direction,
|
||||
timeWindowMinutes: 30,
|
||||
})
|
||||
|
||||
// Penalty 1: Overtrading (3+ signals in 30 minutes)
|
||||
if (recentSignals.totalSignals >= 3) {
|
||||
frequencyPenalties.overtrading = -20
|
||||
score -= 20
|
||||
reasons.push(`⚠️ Overtrading zone: ${recentSignals.totalSignals} signals in 30min (-20 pts)`)
|
||||
}
|
||||
|
||||
// Penalty 2: Flip-flop (opposite direction in last 15 minutes)
|
||||
if (recentSignals.oppositeDirectionInWindow) {
|
||||
frequencyPenalties.flipFlop = -25
|
||||
score -= 25
|
||||
reasons.push(`⚠️ Flip-flop detected: opposite direction ${recentSignals.oppositeDirectionMinutesAgo}min ago (-25 pts)`)
|
||||
}
|
||||
|
||||
// Penalty 3: Alternating pattern (last 3 trades flip directions)
|
||||
if (recentSignals.isAlternatingPattern) {
|
||||
frequencyPenalties.alternating = -30
|
||||
score -= 30
|
||||
const pattern = recentSignals.last3Trades.map(t => t.direction).join(' → ')
|
||||
reasons.push(`⚠️ Chop pattern: last 3 trades alternating (${pattern}) (-30 pts)`)
|
||||
}
|
||||
} catch (error) {
|
||||
// Don't fail the whole score if frequency check fails
|
||||
console.error('❌ Signal frequency check failed:', error)
|
||||
reasons.push('⚠️ Frequency check unavailable (no penalty applied)')
|
||||
}
|
||||
}
|
||||
|
||||
const minScore = params.minScore || 60
|
||||
const passed = score >= minScore
|
||||
|
||||
@@ -178,5 +236,6 @@ export function scoreSignalQuality(params: {
|
||||
score,
|
||||
passed,
|
||||
reasons,
|
||||
frequencyPenalties,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user