From 9928f1cabd67421d6d265cb332f25597f2b53eb4 Mon Sep 17 00:00:00 2001 From: Brandon Li Date: Thu, 23 Apr 2026 13:53:18 -0500 Subject: [PATCH] Add consistency shortfall check to computeDailyTarget When a big day has been made, total trading profit must be >= maxDay/consistency for the consistency ratio to be satisfied. Previously the function only checked equity vs profitTarget and would coast on min-day even when consistency was still violated. Now the remaining = max(equityShortfall, consistencyShortfall). Per-stage: dailyPnL is already filtered to post-withdrawal entries by filterActivePnL, so tradingProfit, qualifyingDays, and maxDay naturally scope to the current stage. Example PAAPEX5466170000038 (consistency 50%, maxDay $3307.60, total $3306.05): - equityShortfall = 0 (equity way above $6600 target due to $150k deposit) - consistencyShortfall = $6615.20 - $3306.05 = $3309.15 - Target now aims for $3309.15 spread across remaining days, not $350 min-day Co-Authored-By: Claude Opus 4.6 (1M context) --- lib/trading-logic.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/trading-logic.ts b/lib/trading-logic.ts index 892334e..14fe8b2 100644 --- a/lib/trading-logic.ts +++ b/lib/trading-logic.ts @@ -93,10 +93,23 @@ export function computeDailyTarget( const qualifyingDays = minDayPnL === 0 ? dailyPnL : dailyPnL.filter((d) => d.pnl >= minDayPnL); const daysTraded = qualifyingDays.length; const effectiveMinDay = Math.max(0, minDayPnL); - const currentProfit = equityProfit; - const remaining = profitTarget - currentProfit; + const tradingProfit = dailyPnL.reduce((s, d) => s + d.pnl, 0); // cycle-local trading profit - // 1. Profit target already met — coast on min-day if mandatory days remain, else nothing to do. + // Shortfall to hit profitTarget (measured in equity terms — accounts for deposits/withdrawals) + const equityShortfall = Math.max(0, profitTarget - equityProfit); + + // Shortfall to satisfy consistency rule: total trading profit must be ≥ maxDay / consistency, + // otherwise the biggest day would exceed the consistency ratio. + let consistencyShortfall = 0; + if (consistency > 0 && consistency < 1 && qualifyingDays.length > 0) { + const maxDay = Math.max(...qualifyingDays.map((d) => d.pnl)); + const realTarget = maxDay / consistency; + consistencyShortfall = Math.max(0, realTarget - tradingProfit); + } + + const remaining = Math.max(equityShortfall, consistencyShortfall); + + // 1. Both target and consistency already satisfied — coast on min-day if mandatory days remain. if (remaining <= 0) { const needsMoreDays = minTradingDays > daysTraded; return { amount: needsMoreDays ? effectiveMinDay : 0, path: 'reduced_day' };