Add AI leverage calculator integration to automation with debug logging
- Import AI leverage calculator in simple-automation.js - Calculate optimal leverage based on stop loss distance and account balance - Use real account data from Drift API for calculations - Add comprehensive debug logging to troubleshoot leverage calculation - Replace hardcoded 1x leverage with AI-calculated optimal leverage The AI should now use 6-8x leverage instead of 1x for better risk/reward
This commit is contained in:
@@ -1,4 +1,16 @@
|
|||||||
// Simple automation service for basic start/stop functionality
|
// Simple automation service for basic start/stop functionality
|
||||||
|
|
||||||
|
// Import AI Leverage Calculator for dynamic leverage
|
||||||
|
async function importAILeverageCalculator() {
|
||||||
|
try {
|
||||||
|
const { AILeverageCalculator } = await import('./ai-leverage-calculator.js');
|
||||||
|
return AILeverageCalculator;
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('⚠️ AI Leverage Calculator not available, using default leverage');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class SimpleAutomation {
|
class SimpleAutomation {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.isRunning = false;
|
this.isRunning = false;
|
||||||
@@ -363,12 +375,67 @@ class SimpleAutomation {
|
|||||||
|
|
||||||
console.log(`🎯 Trade levels - SL: ${stopLoss}, TP: ${takeProfit}`);
|
console.log(`🎯 Trade levels - SL: ${stopLoss}, TP: ${takeProfit}`);
|
||||||
|
|
||||||
|
// Calculate optimal leverage using AI Leverage Calculator
|
||||||
|
let optimalLeverage = 1; // Default fallback
|
||||||
|
console.log('🔧 DEBUG: Starting leverage calculation...');
|
||||||
|
try {
|
||||||
|
const AILeverageCalculator = await importAILeverageCalculator();
|
||||||
|
console.log('🔧 DEBUG: AI Leverage Calculator imported:', !!AILeverageCalculator);
|
||||||
|
if (AILeverageCalculator && stopLoss) {
|
||||||
|
console.log('🔧 DEBUG: Both calculator and stopLoss available, proceeding...');
|
||||||
|
// Get current price from analysis
|
||||||
|
const currentPrice = analysis.entry?.price || analysis.currentPrice || 178; // fallback price
|
||||||
|
|
||||||
|
// Get real account data from Drift API
|
||||||
|
let accountValue = 49; // fallback
|
||||||
|
let availableBalance = 49; // fallback
|
||||||
|
|
||||||
|
try {
|
||||||
|
const baseUrl = process.env.INTERNAL_API_URL || 'http://localhost:3000';
|
||||||
|
console.log('🔧 DEBUG: Fetching balance from:', baseUrl);
|
||||||
|
const balanceResponse = await fetch(`${baseUrl}/api/drift/balance`);
|
||||||
|
const balanceData = await balanceResponse.json();
|
||||||
|
if (balanceData.success) {
|
||||||
|
accountValue = balanceData.accountValue;
|
||||||
|
availableBalance = balanceData.availableBalance;
|
||||||
|
console.log(`💰 Real account data: $${accountValue.toFixed(2)} total, $${availableBalance.toFixed(2)} available`);
|
||||||
|
} else {
|
||||||
|
console.log('🔧 DEBUG: Balance API returned error:', balanceData);
|
||||||
|
}
|
||||||
|
} catch (balanceError) {
|
||||||
|
console.warn('⚠️ Failed to get real balance, using fallback:', balanceError.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`🧮 Calculating optimal leverage: Entry=$${currentPrice}, StopLoss=$${stopLoss}`);
|
||||||
|
|
||||||
|
const leverageResult = AILeverageCalculator.calculateOptimalLeverage({
|
||||||
|
accountValue,
|
||||||
|
availableBalance,
|
||||||
|
entryPrice: currentPrice,
|
||||||
|
stopLossPrice: stopLoss,
|
||||||
|
side: side === 'BUY' ? 'long' : 'short',
|
||||||
|
maxLeverageAllowed: 10, // Drift platform max
|
||||||
|
safetyBuffer: 0.10 // 10% safety buffer
|
||||||
|
});
|
||||||
|
|
||||||
|
optimalLeverage = leverageResult.recommendedLeverage;
|
||||||
|
console.log(`🎯 AI Calculated Leverage: ${optimalLeverage.toFixed(1)}x (Risk: ${leverageResult.riskAssessment})`);
|
||||||
|
console.log(`📊 Leverage Details: ${leverageResult.reasoning}`);
|
||||||
|
} else {
|
||||||
|
console.log('🔧 DEBUG: Skipping leverage calc - Calculator:', !!AILeverageCalculator, 'StopLoss:', !!stopLoss);
|
||||||
|
}
|
||||||
|
} catch (leverageError) {
|
||||||
|
console.warn('⚠️ Leverage calculation failed, using default 1x:', leverageError.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`🔧 DEBUG: Final leverage to use: ${optimalLeverage}x`);
|
||||||
|
|
||||||
// Use the trading API with proper fields for Drift
|
// Use the trading API with proper fields for Drift
|
||||||
const tradePayload = {
|
const tradePayload = {
|
||||||
symbol: this.config.symbol,
|
symbol: this.config.symbol,
|
||||||
side: side,
|
side: side,
|
||||||
amount: this.config.tradingAmount || 49, // Use available balance
|
amount: this.config.tradingAmount || 49, // Use available balance
|
||||||
leverage: 1, // Default leverage
|
leverage: optimalLeverage, // Use AI-calculated optimal leverage
|
||||||
stopLoss: stopLoss,
|
stopLoss: stopLoss,
|
||||||
takeProfit: takeProfit,
|
takeProfit: takeProfit,
|
||||||
useRealDEX: this.config.enableTrading === true, // Use real DEX when live trading enabled
|
useRealDEX: this.config.enableTrading === true, // Use real DEX when live trading enabled
|
||||||
|
|||||||
Reference in New Issue
Block a user