diff --git a/lib/simple-automation.js b/lib/simple-automation.js index 37efc2f..9576758 100644 --- a/lib/simple-automation.js +++ b/lib/simple-automation.js @@ -726,7 +726,22 @@ class SimpleAutomation { console.log('✅ DCA cooldown passed - executing POSITION SCALING DCA...'); // Check if analysis direction matches existing position - const analysisDirection = side.toLowerCase(); + const recommendation = analysis.recommendation?.toLowerCase() || ''; + let analysisDirection = ''; + + if (recommendation.includes('buy')) { + analysisDirection = 'buy'; + } else if (recommendation.includes('sell')) { + analysisDirection = 'sell'; + } else { + return { + success: false, + error: `Invalid recommendation for DCA: ${recommendation}`, + existingPosition: currentPosition, + suggestedAction: 'Cannot determine direction from analysis' + }; + } + const positionDirection = currentPosition.side.toLowerCase(); if (analysisDirection === 'buy' && positionDirection === 'long') { @@ -759,6 +774,14 @@ class SimpleAutomation { console.log('❌ TRADE SKIP: Invalid recommendation - ' + recommendation); return { success: false, error: 'Invalid recommendation: ' + recommendation }; } + + console.log(`🔧 DEBUG: Side variable initialized as: "${side}" from recommendation: "${recommendation}"`); + + // Validate side is properly set + if (!side || (side !== 'BUY' && side !== 'SELL')) { + console.log('❌ TRADE ERROR: Side variable not properly initialized'); + return { success: false, error: 'Side variable initialization error' }; + } // Extract stop loss and take profit from analysis let stopLoss = null; @@ -858,6 +881,7 @@ class SimpleAutomation { } console.log(`🧮 Calculating optimal leverage: Entry=$${currentPrice}, StopLoss=$${stopLoss}`); + console.log(`🔧 DEBUG: Side variable before leverage calc: "${side}"`); const leverageCalcResult = AILeverageCalculator.calculateOptimalLeverage({ accountValue,