From 046629520c7c25c1ae8d290c2e4f7a09924b0822 Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Mon, 24 Nov 2025 08:31:05 +0100 Subject: [PATCH] critical: Fix adaptive leverage not working + P&L compounding Issue 1: Adaptive Leverage Not Working - Quality 90 trade used 15x instead of 10x leverage - Root cause: USE_ADAPTIVE_LEVERAGE ENV variable missing from .env - Fix: Added 4 ENV variables to .env file: * USE_ADAPTIVE_LEVERAGE=true * HIGH_QUALITY_LEVERAGE=15 * LOW_QUALITY_LEVERAGE=10 * QUALITY_LEVERAGE_THRESHOLD=95 - Code was correct, just missing configuration - Container restarted to load new ENV variables - Trade cmici8j640001ry074d7leugt showed $974.05 in DB vs $72.41 actual - 14 duplicate Telegram notifications sent - Root cause: Still investigating - closingInProgress flag already exists - Interim fix: closingInProgress flag added Nov 24 (line 818-821) - Manual correction: Updated DB P&L from $974.05 to $72.41 - This is Common Pitfall #49/#59/#60 recurring Files Changed: - .env: Added adaptive leverage configuration (4 lines) - Database: Corrected P&L for trade cmici8j640001ry074d7leugt Next Steps: - Monitor next quality 90-94 trade for 10x leverage confirmation - Investigate why duplicate processing still occurs despite guards - May need additional serialization mechanism for external closures --- .env | 5 +++++ lib/trading/position-manager.ts | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/.env b/.env index 3cfa0e1..b2cd43a 100644 --- a/.env +++ b/.env @@ -393,6 +393,11 @@ TRAILING_STOP_ACTIVATION=0.4 MIN_SIGNAL_QUALITY_SCORE=91 MIN_SIGNAL_QUALITY_SCORE_LONG=90 # Nov 23, 2025: Longs have 71.4% WR at quality 90-94 (+$44.77 on 7 trades) MIN_SIGNAL_QUALITY_SCORE_SHORT=95 # Nov 23, 2025: Shorts toxic at quality 90-94 (28.6% WR, -$553.76 on 7 trades) +\n# Adaptive Leverage System (Nov 24, 2025) +USE_ADAPTIVE_LEVERAGE=true +HIGH_QUALITY_LEVERAGE=15 +LOW_QUALITY_LEVERAGE=10 +QUALITY_LEVERAGE_THRESHOLD=95 SOLANA_ENABLED=true SOLANA_POSITION_SIZE=100 SOLANA_LEVERAGE=15 diff --git a/lib/trading/position-manager.ts b/lib/trading/position-manager.ts index 17e4bf8..86dd2e4 100644 --- a/lib/trading/position-manager.ts +++ b/lib/trading/position-manager.ts @@ -815,6 +815,12 @@ export class PositionManager { if ((position === null || position.size === 0) && !trade.closingInProgress) { + // CRITICAL FIX (Nov 24, 2025): IMMEDIATELY mark as closingInProgress + // This prevents ANY duplicate processing before DB update completes + trade.closingInProgress = true + trade.closeConfirmedAt = Date.now() + console.log(`🔒 Marked ${trade.symbol} as closingInProgress to prevent duplicate external closure processing`) + // CRITICAL FIX (Nov 20, 2025): If TP1 already hit, this is RUNNER closure // We should have been monitoring with trailing stop active // Check if we should have had trailing stop protection