From 625566224a697e838f03787d1c274f89bf372cde Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Sun, 23 Nov 2025 14:18:04 +0100 Subject: [PATCH] critical: Fix MFE/MAE storing dollars instead of percentages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Root Cause (Nov 23, 2025): - Database showed MFE 64.08% when TradingView showed 0.48% - Position Manager was storing DOLLAR amounts ($64.08) not percentages - Prisma schema comment says 'Best profit % reached' but code stored dollars - Bug caused 100× inflation in MFE/MAE analysis (0.83% shown as 83%) The Bug (lib/trading/position-manager.ts line 1127): - BEFORE: trade.maxFavorableExcursion = currentPnLDollars // Storing $64.08 - AFTER: trade.maxFavorableExcursion = profitPercent // Storing 0.48% Impact: - All quality 90 analysis was based on wrong MFE values - Trade #2 (Nov 22): Database showed 0.83% MFE, actual was 0.48% - TP1-only simulation used inflated MFE values - User observation (TradingView charts) revealed the discrepancy Fix: - Changed to store profitPercent (0.48) instead of currentPnLDollars ($64.08) - Updated comment to reflect PERCENTAGE storage - All future trades will track MFE/MAE correctly - Historical data still has inflated values (can't auto-correct) Validation Required: - Next trade: Verify MFE/MAE stored as percentages - Compare database values to TradingView chart max profit - Quality 90 analysis should use corrected MFE data going forward --- lib/trading/position-manager.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/trading/position-manager.ts b/lib/trading/position-manager.ts index b2a6f78..17e4bf8 100644 --- a/lib/trading/position-manager.ts +++ b/lib/trading/position-manager.ts @@ -1121,14 +1121,15 @@ export class PositionManager { trade.peakPnL = trade.unrealizedPnL } - // Track MAE/MFE in DOLLAR amounts (not percentages!) - // CRITICAL: Database schema expects DOLLARS for analysis and TP/SL optimization - if (currentPnLDollars > trade.maxFavorableExcursion) { - trade.maxFavorableExcursion = currentPnLDollars + // Track MAE/MFE in PERCENTAGE (not dollars!) + // CRITICAL FIX (Nov 23, 2025): Schema expects % (0.48 = 0.48%), not dollar amounts + // Bug was storing $64.08 when actual was 0.48%, causing 100× inflation in analysis + if (profitPercent > trade.maxFavorableExcursion) { + trade.maxFavorableExcursion = profitPercent trade.maxFavorablePrice = currentPrice } - if (currentPnLDollars < trade.maxAdverseExcursion) { - trade.maxAdverseExcursion = currentPnLDollars + if (profitPercent < trade.maxAdverseExcursion) { + trade.maxAdverseExcursion = profitPercent trade.maxAdversePrice = currentPrice }