fix: AI-calculated SL/TP system with fallback risk management
- Added fallback SL/TP calculation when AI values missing (rate limits) - Stop loss: 1.5% from entry (scalping-optimized) - Take profit: 3% from entry (2:1 risk/reward) - Relaxed API validation to require only stop loss (most critical) - Disabled problematic import in position-history route - System now guarantees risk management on every trade No more unprotected positions - works with or without AI analysis
This commit is contained in:
@@ -798,7 +798,33 @@ class SimpleAutomation {
|
||||
takeProfit = parseFloat(takeProfit.replace(/[^0-9.]/g, ''));
|
||||
}
|
||||
|
||||
console.log(`🎯 Trade levels - SL: ${stopLoss}, TP: ${takeProfit}`);
|
||||
// 🛡️ FALLBACK RISK MANAGEMENT: Ensure SL/TP always exist
|
||||
if (!stopLoss || !takeProfit) {
|
||||
console.log('⚠️ Missing AI-calculated SL/TP, generating fallback values...');
|
||||
const currentPrice = analysis.entry?.price || analysis.currentPrice || 178;
|
||||
|
||||
if (!stopLoss) {
|
||||
// Fallback: 1.5% stop loss for scalping
|
||||
if (side === 'BUY' || side === 'LONG') {
|
||||
stopLoss = currentPrice * 0.985; // 1.5% below entry
|
||||
} else {
|
||||
stopLoss = currentPrice * 1.015; // 1.5% above entry
|
||||
}
|
||||
console.log(`🔧 Generated fallback stop loss: $${stopLoss.toFixed(4)} (1.5%)`);
|
||||
}
|
||||
|
||||
if (!takeProfit) {
|
||||
// Fallback: 3% take profit for scalping (2:1 risk/reward)
|
||||
if (side === 'BUY' || side === 'LONG') {
|
||||
takeProfit = currentPrice * 1.03; // 3% above entry
|
||||
} else {
|
||||
takeProfit = currentPrice * 0.97; // 3% below entry
|
||||
}
|
||||
console.log(`🔧 Generated fallback take profit: $${takeProfit.toFixed(4)} (3%)`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`🎯 Final trade levels - SL: ${stopLoss}, TP: ${takeProfit}`);
|
||||
|
||||
// Calculate optimal leverage using AI Leverage Calculator
|
||||
let optimalLeverage = 1; // Default fallback
|
||||
|
||||
Reference in New Issue
Block a user