diff --git a/app/api/automation/analyze-position/route.js b/app/api/automation/analyze-position/route.js index 68ec64f..853f28e 100644 --- a/app/api/automation/analyze-position/route.js +++ b/app/api/automation/analyze-position/route.js @@ -16,12 +16,74 @@ export async function POST(request) { stopLossPrice: 178.06 }; - // Calculate some metrics - handle missing stop loss - const hasStopLoss = position.stopLossPrice && position.stopLossPrice > 0; - const estimatedStopLoss = hasStopLoss ? position.stopLossPrice : (position.entryPrice * 0.95); // 5% default - const stopLossDistance = Math.abs(position.entryPrice - estimatedStopLoss); + // Fetch actual Drift orders to get real stop loss and take profit + let actualStopLoss = null; + let actualTakeProfit = null; + let orderAnalysis = "Orders not accessible"; + + try { + const ordersResponse = await fetch('http://localhost:3000/api/drift/orders'); + if (ordersResponse.ok) { + const ordersData = await ordersResponse.json(); + + if (ordersData.success && ordersData.orders) { + const relevantOrders = ordersData.orders.filter(order => + order.symbol === position.symbol && + order.reduceOnly && + order.status === 'OPEN' + ); + + // Find stop loss (price below entry for long, above for short) + const stopLossOrders = relevantOrders.filter(order => { + const isStopDirection = position.side.toLowerCase() === 'long' ? + (order.direction === 'SHORT' || order.direction === 'SELL') : + (order.direction === 'LONG' || order.direction === 'BUY'); + + const hasStopPrice = position.side.toLowerCase() === 'long' ? + (order.triggerPrice && parseFloat(order.triggerPrice) < position.entryPrice) : + (order.triggerPrice && parseFloat(order.triggerPrice) > position.entryPrice); + + return isStopDirection && hasStopPrice; + }); + + // Find take profit (price above entry for long, below for short) + const takeProfitOrders = relevantOrders.filter(order => { + const isTpDirection = position.side.toLowerCase() === 'long' ? + (order.direction === 'SHORT' || order.direction === 'SELL') : + (order.direction === 'LONG' || order.direction === 'BUY'); + + const hasTpPrice = position.side.toLowerCase() === 'long' ? + (order.triggerPrice && parseFloat(order.triggerPrice) > position.entryPrice) : + (order.triggerPrice && parseFloat(order.triggerPrice) < position.entryPrice); + + return isTpDirection && hasTpPrice; + }); + + if (stopLossOrders.length > 0) { + actualStopLoss = parseFloat(stopLossOrders[0].triggerPrice); + } + + if (takeProfitOrders.length > 0) { + actualTakeProfit = parseFloat(takeProfitOrders[0].triggerPrice); + } + + orderAnalysis = `Found ${relevantOrders.length} reduce-only orders: ${stopLossOrders.length} stop loss, ${takeProfitOrders.length} take profit`; + } + } + } catch (orderError) { + console.log('Could not fetch orders for analysis:', orderError.message); + orderAnalysis = "Order fetch failed - using estimates"; + } + + // Use actual orders if available, otherwise estimate + const hasRealStopLoss = actualStopLoss !== null; + const hasRealTakeProfit = actualTakeProfit !== null; + const effectiveStopLoss = hasRealStopLoss ? actualStopLoss : (position.entryPrice * 0.95); + const effectiveTakeProfit = hasRealTakeProfit ? actualTakeProfit : (position.entryPrice * 1.10); + + const stopLossDistance = Math.abs(position.entryPrice - effectiveStopLoss); const stopLossPercent = ((stopLossDistance / position.entryPrice) * 100).toFixed(1); - const leverage = (position.size * position.entryPrice) / (position.size * position.entryPrice * 0.08); // Estimate based on position + const leverage = (position.size * position.entryPrice) / (position.size * position.entryPrice * 0.08); const estimatedLeverage = Math.round(leverage * 10) / 10; // Generate realistic AI reasoning based on the position @@ -33,9 +95,10 @@ export async function POST(request) { โข Position size of ${position.size} SOL indicates moderate conviction ๐ Risk Management Assessment: -โข Stop loss at $${estimatedStopLoss.toFixed(2)} (${stopLossPercent}% protection)${hasStopLoss ? '' : ' - ESTIMATED'} -โข Risk/reward setup suggests ${stopLossPercent}% stop with potential 2-3x reward -โข Position sizing appears conservative for risk tolerance +โข Stop loss at $${effectiveStopLoss.toFixed(2)} (${stopLossPercent}% protection)${hasRealStopLoss ? ' โ CONFIRMED' : ' โ ๏ธ ESTIMATED'} +โข Take profit at $${effectiveTakeProfit.toFixed(2)}${hasRealTakeProfit ? ' โ CONFIRMED' : ' โ ๏ธ ESTIMATED'} +โข Risk/reward ratio: ${((Math.abs(effectiveTakeProfit - position.entryPrice) / stopLossDistance)).toFixed(1)}:1 +โข ${orderAnalysis} โก Leverage Analysis: โข Estimated leverage: ~${estimatedLeverage}x (based on position metrics) @@ -44,14 +107,15 @@ export async function POST(request) { ๐ก๏ธ Current Status: โข Position currently ${position.currentPrice > position.entryPrice ? 'profitable' : 'underwater'} -โข Distance to ${hasStopLoss ? 'stop loss' : 'estimated stop'}: ${((Math.abs(position.currentPrice - estimatedStopLoss) / position.currentPrice) * 100).toFixed(1)}% +โข Distance to stop loss: ${((Math.abs(position.currentPrice - effectiveStopLoss) / position.currentPrice) * 100).toFixed(1)}% +โข Distance to take profit: ${((Math.abs(position.currentPrice - effectiveTakeProfit) / position.currentPrice) * 100).toFixed(1)}% โข Monitoring recommended for further developments`; // Create a decision object for the existing position const retroactiveDecision = { timestamp: new Date().toISOString(), recommendation: `${position.side.toUpperCase()} (Executed)`, - confidence: 82, // Estimated confidence based on position size and setup + confidence: hasRealStopLoss && hasRealTakeProfit ? 92 : 82, // Higher confidence with real orders minConfidenceRequired: 75, reasoning: aiReasoning, executed: true, @@ -60,11 +124,16 @@ export async function POST(request) { amount: Math.round(position.size * position.entryPrice), leverage: estimatedLeverage, currentPrice: position.entryPrice, - stopLoss: estimatedStopLoss, - takeProfit: position.entryPrice + (stopLossDistance * 2.5), // Estimate 2.5:1 RR - aiReasoning: `Retrospective analysis: ${estimatedLeverage}x leverage with ${stopLossPercent}% stop loss provides balanced risk/reward. Position sizing suggests moderate risk appetite with professional risk management principles applied.${hasStopLoss ? '' : ' Note: Stop loss estimated as not visible in position data.'}`, + stopLoss: effectiveStopLoss, + takeProfit: effectiveTakeProfit, + aiReasoning: `Retrospective analysis: ${estimatedLeverage}x leverage with ${stopLossPercent}% stop loss provides balanced risk/reward. Position sizing suggests moderate risk appetite with professional risk management principles applied.${hasRealStopLoss ? ' Actual stop loss orders detected and confirmed.' : ' Stop loss estimated - actual orders may differ.'}`, txId: 'existing_position_analysis', - aiStopLossPercent: `${stopLossPercent}% protective stop` + aiStopLossPercent: `${stopLossPercent}% protective stop`, + orderStatus: { + realStopLoss: hasRealStopLoss, + realTakeProfit: hasRealTakeProfit, + orderAnalysis: orderAnalysis + } }, executionError: null, isRetrospective: true // Flag to indicate this is retroactive analysis diff --git a/app/automation-v2/page.js b/app/automation-v2/page.js index 5865659..327c682 100644 --- a/app/automation-v2/page.js +++ b/app/automation-v2/page.js @@ -684,10 +684,10 @@ export default function AutomationPageV2() { {/* Enhanced Sidebar */}
Real-time monitoring
+Status โข Positions โข Risk Monitor
{status.rateLimitMessage}
+ )} ++ Automation stopped. Recharge OpenAI account to continue. +
+{status.rateLimitMessage}
)} -- Automation stopped automatically. Please recharge your OpenAI account to continue. -
Risk assessment
-{positions.length} active trade{positions.length !== 1 ? 's' : ''}
-+ The AI will analyze market conditions and provide detailed reasoning for all trading decisions. +
+{status.rateLimitMessage}
+ )} ++ Automation stopped automatically. Please recharge your OpenAI account to continue. +
++ The AI will analyze market conditions and provide detailed reasoning for all trading decisions. +
+{status.rateLimitMessage}
+ )} ++ Automation stopped automatically. Please recharge your OpenAI account to continue. +
++ The AI will analyze market conditions and provide detailed reasoning for all trading decisions. +
+{monitorData.details}
+Start automation to begin trading
+Start automation to begin trading
+