From 1cbed61a46cc843f3bfba6282e6eed3542973d98 Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Thu, 24 Jul 2025 11:35:57 +0200 Subject: [PATCH] CRITICAL FIX: Convert AI absolute prices to percentages for stop loss/take profit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit system was treating them as percentages (530.13%), causing ridiculous targets SOLUTION: - calculateStopLoss(): Convert AI absolute stopLoss.price to percentage - calculateTakeProfit(): Convert AI absolute takeProfits.tp1.price to percentage - Proper calculation: ((targetPrice - currentPrice) / currentPrice) * 100 - Before: SOL @85 → TP: 530.13% = ,152 🤯 - After: SOL @85 → TP: 2.8% = 90 ✅ This ensures scalping trades have realistic targets instead of 500%+ gains. --- lib/automation-service-simple.ts | 60 ++++++++++++------------------- prisma/prisma/dev.db | Bin 978944 -> 983040 bytes 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/lib/automation-service-simple.ts b/lib/automation-service-simple.ts index a6bc305..7c46269 100644 --- a/lib/automation-service-simple.ts +++ b/lib/automation-service-simple.ts @@ -736,64 +736,48 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r. private calculateStopLoss(analysis: any): number { // ✅ AI-FIRST: Use AI analysis stopLoss if available if (analysis.stopLoss?.price) { - return analysis.stopLoss.price + const currentPrice = analysis.entry?.price || 189 + const stopLossPrice = analysis.stopLoss.price + + // Convert absolute price to percentage + if (analysis.recommendation === 'BUY') { + return ((currentPrice - stopLossPrice) / currentPrice) * 100 + } else if (analysis.recommendation === 'SELL') { + return ((stopLossPrice - currentPrice) / currentPrice) * 100 + } } // If AI provides explicit stop loss percentage, use it if (analysis.stopLossPercent) { - const currentPrice = analysis.entry?.price || 189 - const stopLossPercent = analysis.stopLossPercent / 100 - - if (analysis.recommendation === 'BUY') { - return currentPrice * (1 - stopLossPercent) - } else if (analysis.recommendation === 'SELL') { - return currentPrice * (1 + stopLossPercent) - } + return analysis.stopLossPercent } // Fallback: Dynamic stop loss based on market volatility (AI-calculated) - const currentPrice = analysis.entry?.price || 189 // AI determines volatility-based stop loss (0.5% to 2% range) - const aiStopLossPercent = this.calculateAIStopLoss(analysis) / 100 - - if (analysis.recommendation === 'BUY') { - return currentPrice * (1 - aiStopLossPercent) - } else if (analysis.recommendation === 'SELL') { - return currentPrice * (1 + aiStopLossPercent) - } else { - return currentPrice * (1 - aiStopLossPercent) - } + return this.calculateAIStopLoss(analysis) } private calculateTakeProfit(analysis: any): number { // ✅ AI-FIRST: Use AI analysis takeProfit if available if (analysis.takeProfits?.tp1?.price) { - return analysis.takeProfits.tp1.price + const currentPrice = analysis.entry?.price || 150 + const takeProfitPrice = analysis.takeProfits.tp1.price + + // Convert absolute price to percentage + if (analysis.recommendation === 'BUY') { + return ((takeProfitPrice - currentPrice) / currentPrice) * 100 + } else if (analysis.recommendation === 'SELL') { + return ((currentPrice - takeProfitPrice) / currentPrice) * 100 + } } // If AI provides explicit take profit percentage, use it if (analysis.takeProfitPercent) { - const currentPrice = analysis.entry?.price || 150 - const takeProfitPercent = analysis.takeProfitPercent / 100 - - if (analysis.recommendation === 'BUY') { - return currentPrice * (1 + takeProfitPercent) - } else if (analysis.recommendation === 'SELL') { - return currentPrice * (1 - takeProfitPercent) - } + return analysis.takeProfitPercent } // Fallback: Dynamic take profit based on AI risk/reward optimization - const currentPrice = analysis.entry?.price || 150 - const aiTakeProfitPercent = this.calculateAITakeProfit(analysis) / 100 - - if (analysis.recommendation === 'BUY') { - return currentPrice * (1 + aiTakeProfitPercent) - } else if (analysis.recommendation === 'SELL') { - return currentPrice * (1 - aiTakeProfitPercent) - } else { - return currentPrice * (1 + aiTakeProfitPercent) - } + return this.calculateAITakeProfit(analysis) } // AI-calculated dynamic stop loss based on volatility and market conditions diff --git a/prisma/prisma/dev.db b/prisma/prisma/dev.db index b3738bcf1f12f0e78236131c93f48a17bef38176..aa077a5b2337ec112f3f4815edc17981226f1f81 100644 GIT binary patch delta 1279 zcmZ`%ZA=?=7{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