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:
@@ -726,7 +726,22 @@ class SimpleAutomation {
|
|||||||
console.log('✅ DCA cooldown passed - executing POSITION SCALING DCA...');
|
console.log('✅ DCA cooldown passed - executing POSITION SCALING DCA...');
|
||||||
|
|
||||||
// Check if analysis direction matches existing position
|
// 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();
|
const positionDirection = currentPosition.side.toLowerCase();
|
||||||
|
|
||||||
if (analysisDirection === 'buy' && positionDirection === 'long') {
|
if (analysisDirection === 'buy' && positionDirection === 'long') {
|
||||||
@@ -760,6 +775,14 @@ class SimpleAutomation {
|
|||||||
return { success: false, error: '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
|
// Extract stop loss and take profit from analysis
|
||||||
let stopLoss = null;
|
let stopLoss = null;
|
||||||
let takeProfit = null;
|
let takeProfit = null;
|
||||||
@@ -858,6 +881,7 @@ class SimpleAutomation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log(`🧮 Calculating optimal leverage: Entry=$${currentPrice}, StopLoss=$${stopLoss}`);
|
console.log(`🧮 Calculating optimal leverage: Entry=$${currentPrice}, StopLoss=$${stopLoss}`);
|
||||||
|
console.log(`🔧 DEBUG: Side variable before leverage calc: "${side}"`);
|
||||||
|
|
||||||
const leverageCalcResult = AILeverageCalculator.calculateOptimalLeverage({
|
const leverageCalcResult = AILeverageCalculator.calculateOptimalLeverage({
|
||||||
accountValue,
|
accountValue,
|
||||||
|
|||||||
Reference in New Issue
Block a user