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' };