fix: resolve 'Cannot access side before initialization' error in trade execution

- Fixed temporal dead zone error where side variable was accessed before declaration
- Added proper error handling and validation for side variable initialization
- Fixed DCA position scaling logic to properly extract direction from analysis
- Added debugging logs to track side variable state throughout execution
This commit is contained in:
mindesbunister
2025-07-29 17:47:48 +02:00
parent 6c02d39f0a
commit 158cd1741b

View File

@@ -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,