feat: fix AI Trading Analysis panel to show real successful trade data
Fixed analysis-details API to use exact same logic as position-history Now shows successful .13 WIN trade instead of old mock data Successful trade sync script working correctly Position monitor shows active SHORT position with proper stop-loss Next: Complete risk management system integration
This commit is contained in:
@@ -1,313 +0,0 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
console.log('✅ API CORRECTED: Loading with fixed trade calculations...')
|
||||
|
||||
const sessions = await prisma.automation_sessions.findMany({
|
||||
where: {
|
||||
userId: 'default-user',
|
||||
symbol: 'SOLUSD'
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
take: 10
|
||||
})
|
||||
|
||||
if (sessions.length === 0) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: 'No automation sessions found'
|
||||
})
|
||||
}
|
||||
|
||||
const latestSession = sessions[0]
|
||||
|
||||
const sessionsByTimeframe = {}
|
||||
sessions.forEach(session => {
|
||||
if (!sessionsByTimeframe[session.timeframe]) {
|
||||
sessionsByTimeframe[session.timeframe] = session
|
||||
}
|
||||
})
|
||||
|
||||
const recentTrades = await prisma.trades.findMany({
|
||||
where: {
|
||||
userId: latestSession.userId,
|
||||
symbol: latestSession.symbol
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
take: 10
|
||||
})
|
||||
|
||||
const completedTrades = recentTrades.filter(t => t.status === 'COMPLETED')
|
||||
const successfulTrades = completedTrades.filter(t => (t.profit || 0) > 0)
|
||||
const totalPnL = completedTrades.reduce((sum, trade) => sum + (trade.profit || 0), 0)
|
||||
const winRate = completedTrades.length > 0 ? (successfulTrades.length / completedTrades.length * 100) : 0
|
||||
|
||||
// 🔥 GET REAL CURRENT PRICE - SYNCHRONIZED WITH PRICE MONITOR
|
||||
let currentPrice = 193.54 // Fallback price
|
||||
try {
|
||||
// First try to get price from price-monitor endpoint (most recent and consistent)
|
||||
const priceMonitorResponse = await fetch('http://localhost:3000/api/price-monitor')
|
||||
if (priceMonitorResponse.ok) {
|
||||
const priceMonitorData = await priceMonitorResponse.json()
|
||||
if (priceMonitorData.success && priceMonitorData.data.prices.SOLUSD) {
|
||||
currentPrice = priceMonitorData.data.prices.SOLUSD
|
||||
console.log('📊 Using synchronized price from price monitor:', currentPrice)
|
||||
} else {
|
||||
throw new Error('Price monitor data not available')
|
||||
}
|
||||
} else {
|
||||
throw new Error('Price monitor API not responding')
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('⚠️ Price monitor unavailable, fetching directly from Binance:', error.message)
|
||||
try {
|
||||
// Fallback to direct Binance API call
|
||||
const priceResponse = await fetch('https://api.binance.com/api/v3/ticker/price?symbol=SOLUSDT')
|
||||
if (priceResponse.ok) {
|
||||
const priceData = await priceResponse.json()
|
||||
currentPrice = parseFloat(priceData.price)
|
||||
console.log('📊 Using backup price from Binance:', currentPrice)
|
||||
}
|
||||
} catch (backupError) {
|
||||
console.error('⚠️ Both price sources failed, using fallback:', backupError)
|
||||
}
|
||||
}
|
||||
|
||||
const formattedTrades = recentTrades.map(trade => {
|
||||
const priceChange = trade.side === 'BUY' ?
|
||||
(currentPrice - trade.price) :
|
||||
(trade.price - currentPrice)
|
||||
|
||||
// 🔥 FIX: Calculate P&L based on ACTUAL investment amount, not position size
|
||||
// Get the actual trading amount from the trade or session settings
|
||||
const actualTradingAmount = trade.tradingAmount || latestSession.settings?.tradingAmount || 100
|
||||
const storedPositionValue = trade.amount * trade.price // What was actually bought
|
||||
|
||||
// Calculate proportional P&L based on actual investment
|
||||
const realizedPnL = trade.status === 'COMPLETED' ? (trade.profit || 0) : null
|
||||
const unrealizedPnL = trade.status === 'OPEN' ?
|
||||
(priceChange * trade.amount * (actualTradingAmount / storedPositionValue)) : null
|
||||
|
||||
// console.log(`💰 P&L Calculation for trade ${trade.id}:`, {
|
||||
// actualTradingAmount,
|
||||
// storedPositionValue: storedPositionValue.toFixed(2),
|
||||
// priceChange: priceChange.toFixed(2),
|
||||
// rawPnL: (priceChange * trade.amount).toFixed(2),
|
||||
// adjustedPnL: unrealizedPnL?.toFixed(2),
|
||||
// adjustment_ratio: (actualTradingAmount / storedPositionValue).toFixed(4)
|
||||
// })
|
||||
|
||||
const entryTime = new Date(trade.createdAt)
|
||||
const exitTime = trade.closedAt ? new Date(trade.closedAt) : null
|
||||
const currentTime = new Date()
|
||||
|
||||
const durationMs = trade.status === 'COMPLETED' ?
|
||||
(exitTime ? exitTime.getTime() - entryTime.getTime() : 0) :
|
||||
(currentTime.getTime() - entryTime.getTime())
|
||||
|
||||
const durationMinutes = Math.floor(durationMs / (1000 * 60))
|
||||
const formatDuration = (minutes) => {
|
||||
if (minutes < 60) return `${minutes}m`
|
||||
const hours = Math.floor(minutes / 60)
|
||||
const mins = minutes % 60
|
||||
return mins > 0 ? `${hours}h ${mins}m` : `${hours}h`
|
||||
}
|
||||
|
||||
// ✅ CORRECTED CALCULATION: Show actual investment amounts
|
||||
const leverage = trade.leverage || 1
|
||||
const displayPositionSize = actualTradingAmount.toFixed(2)
|
||||
|
||||
// Mark old trades with wrong data
|
||||
const isOldWrongTrade = trade.price < 150 && trade.amount > 1.5 // Detect old wrong trades
|
||||
|
||||
// Enhanced entry/exit price handling
|
||||
const entryPrice = trade.entryPrice || trade.price
|
||||
let exitPrice = trade.exitPrice
|
||||
let calculatedProfit = trade.profit
|
||||
|
||||
// If exit price is null but trade is completed, try to calculate from profit
|
||||
if (trade.status === 'COMPLETED' && !exitPrice && calculatedProfit !== null && calculatedProfit !== undefined) {
|
||||
// Calculate exit price from profit: profit = (exitPrice - entryPrice) * amount
|
||||
if (trade.side === 'BUY') {
|
||||
exitPrice = entryPrice + (calculatedProfit / trade.amount)
|
||||
} else {
|
||||
exitPrice = entryPrice - (calculatedProfit / trade.amount)
|
||||
}
|
||||
}
|
||||
|
||||
// If profit is null but we have both prices, calculate profit
|
||||
if (trade.status === 'COMPLETED' && (calculatedProfit === null || calculatedProfit === undefined) && exitPrice && entryPrice) {
|
||||
if (trade.side === 'BUY') {
|
||||
calculatedProfit = (exitPrice - entryPrice) * trade.amount
|
||||
} else {
|
||||
calculatedProfit = (entryPrice - exitPrice) * trade.amount
|
||||
}
|
||||
}
|
||||
|
||||
// Determine result based on actual profit - use profit field as fallback
|
||||
let result = 'ACTIVE'
|
||||
if (trade.status === 'COMPLETED') {
|
||||
// First try to use the stored profit field
|
||||
const storedProfit = trade.profit || 0
|
||||
|
||||
if (calculatedProfit !== null && calculatedProfit !== undefined) {
|
||||
// Use calculated profit if available
|
||||
if (Math.abs(calculatedProfit) < 0.01) {
|
||||
result = 'BREAKEVEN'
|
||||
} else if (calculatedProfit > 0) {
|
||||
result = 'WIN'
|
||||
} else {
|
||||
result = 'LOSS'
|
||||
}
|
||||
} else if (storedProfit !== null) {
|
||||
// Fallback to stored profit field
|
||||
if (Math.abs(storedProfit) < 0.01) {
|
||||
result = 'BREAKEVEN'
|
||||
} else if (storedProfit > 0) {
|
||||
result = 'WIN'
|
||||
} else {
|
||||
result = 'LOSS'
|
||||
}
|
||||
} else {
|
||||
result = 'UNKNOWN' // When we truly don't have any profit data
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id: trade.id,
|
||||
type: 'MARKET',
|
||||
side: trade.side,
|
||||
amount: trade.amount, // Keep original SOL amount for reference
|
||||
tradingAmount: actualTradingAmount, // Show actual investment amount
|
||||
realTradingAmount: actualTradingAmount, // Show real trading amount
|
||||
leverage: leverage,
|
||||
positionSize: displayPositionSize,
|
||||
price: trade.price,
|
||||
status: trade.status,
|
||||
pnl: realizedPnL ? realizedPnL.toFixed(2) : (unrealizedPnL ? unrealizedPnL.toFixed(2) : '0.00'),
|
||||
pnlPercent: realizedPnL ? `${((realizedPnL / actualTradingAmount) * 100).toFixed(2)}%` :
|
||||
(unrealizedPnL ? `${((unrealizedPnL / actualTradingAmount) * 100).toFixed(2)}%` : '0.00%'),
|
||||
createdAt: trade.createdAt,
|
||||
entryTime: trade.createdAt,
|
||||
exitTime: trade.closedAt,
|
||||
actualDuration: durationMs,
|
||||
durationText: formatDuration(durationMinutes) + (trade.status === 'OPEN' ? ' (Active)' : ''),
|
||||
reason: `REAL: ${trade.side} signal with ${trade.confidence || 75}% confidence`,
|
||||
entryPrice: entryPrice,
|
||||
exitPrice: exitPrice,
|
||||
currentPrice: trade.status === 'OPEN' ? currentPrice : null,
|
||||
unrealizedPnl: unrealizedPnL ? unrealizedPnL.toFixed(2) : null,
|
||||
realizedPnl: realizedPnL ? realizedPnL.toFixed(2) : null,
|
||||
calculatedProfit: calculatedProfit,
|
||||
stopLoss: trade.stopLoss || (trade.side === 'BUY' ? (trade.price * 0.98).toFixed(2) : (trade.price * 1.02).toFixed(2)),
|
||||
takeProfit: trade.takeProfit || (trade.side === 'BUY' ? (trade.price * 1.04).toFixed(2) : (trade.price * 0.96).toFixed(2)),
|
||||
isActive: trade.status === 'OPEN' || trade.status === 'PENDING',
|
||||
confidence: trade.confidence || 75,
|
||||
result: result,
|
||||
resultDescription: trade.status === 'COMPLETED' ?
|
||||
`REAL: ${result === 'WIN' ? 'Profitable' : result === 'LOSS' ? 'Loss' : result} ${trade.side} trade - Completed` :
|
||||
`REAL: ${trade.side} position active - ${formatDuration(durationMinutes)}`,
|
||||
isOldWrongTrade: isOldWrongTrade,
|
||||
correctedAmount: isOldWrongTrade ? (actualTradingAmount / currentPrice).toFixed(4) : null,
|
||||
originalStoredPrice: trade.price,
|
||||
tradingMode: trade.tradingMode || latestSession.mode, // 🔥 USE ACTUAL TRADING MODE FROM DATABASE
|
||||
driftTxId: trade.driftTxId, // Jupiter DEX transaction ID
|
||||
fees: trade.fees || 0, // Trading fees
|
||||
actualInvestment: actualTradingAmount, // Show the real investment amount
|
||||
positionAdjustment: `${actualTradingAmount}/${storedPositionValue.toFixed(2)}`
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
session: {
|
||||
id: latestSession.id,
|
||||
symbol: latestSession.symbol,
|
||||
timeframe: latestSession.timeframe,
|
||||
status: latestSession.status,
|
||||
mode: latestSession.mode,
|
||||
createdAt: latestSession.createdAt,
|
||||
lastAnalysisAt: latestSession.lastAnalysis || new Date().toISOString(),
|
||||
totalTrades: completedTrades.length,
|
||||
successfulTrades: successfulTrades.length,
|
||||
errorCount: latestSession.errorCount,
|
||||
totalPnL: totalPnL
|
||||
},
|
||||
multiTimeframeSessions: sessionsByTimeframe,
|
||||
analysis: {
|
||||
decision: "HOLD",
|
||||
confidence: 84,
|
||||
summary: `🔥 REAL DATABASE: ${completedTrades.length} trades, ${successfulTrades.length} wins (${winRate.toFixed(1)}% win rate), P&L: $${totalPnL.toFixed(2)}`,
|
||||
sentiment: "NEUTRAL",
|
||||
testField: "CORRECTED_CALCULATIONS",
|
||||
analysisContext: {
|
||||
currentSignal: "HOLD",
|
||||
explanation: `🎯 REAL DATA: ${recentTrades.length} database trades shown with corrected calculations`
|
||||
},
|
||||
timeframeAnalysis: {
|
||||
"15m": { decision: "HOLD", confidence: 75 },
|
||||
"1h": { decision: "HOLD", confidence: 70 },
|
||||
"2h": { decision: "HOLD", confidence: 70 },
|
||||
"4h": { decision: "HOLD", confidence: 70 }
|
||||
},
|
||||
multiTimeframeResults: [
|
||||
{
|
||||
timeframe: "1h",
|
||||
status: "ACTIVE",
|
||||
decision: "BUY",
|
||||
confidence: 85,
|
||||
sentiment: "BULLISH",
|
||||
analysisComplete: true
|
||||
},
|
||||
{
|
||||
timeframe: "2h",
|
||||
status: "ACTIVE",
|
||||
decision: "BUY",
|
||||
confidence: 78,
|
||||
sentiment: "BULLISH",
|
||||
analysisComplete: true
|
||||
},
|
||||
{
|
||||
timeframe: "4h",
|
||||
status: "ACTIVE",
|
||||
decision: "BUY",
|
||||
confidence: 82,
|
||||
sentiment: "BULLISH",
|
||||
analysisComplete: true
|
||||
}
|
||||
],
|
||||
layoutsAnalyzed: ["AI Layout", "DIY Layout"],
|
||||
entry: {
|
||||
price: currentPrice,
|
||||
buffer: "±0.25",
|
||||
rationale: "Current market level"
|
||||
},
|
||||
stopLoss: {
|
||||
price: 174.5,
|
||||
rationale: "Technical support level"
|
||||
},
|
||||
takeProfits: {
|
||||
tp1: { price: 176.5, description: "First target" },
|
||||
tp2: { price: 177.5, description: "Extended target" }
|
||||
},
|
||||
reasoning: `✅ CORRECTED DATA: ${completedTrades.length} completed trades, ${winRate.toFixed(1)}% win rate, $${totalPnL.toFixed(2)} P&L`,
|
||||
timestamp: new Date().toISOString(),
|
||||
processingTime: "~2.5 minutes"
|
||||
},
|
||||
recentTrades: formattedTrades
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error fetching analysis details:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'Failed to fetch analysis details',
|
||||
details: error.message
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
153
app/api/automation/place-risk-management/route.js
Normal file
153
app/api/automation/place-risk-management/route.js
Normal file
@@ -0,0 +1,153 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export async function POST(request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { symbol, action } = body;
|
||||
|
||||
console.log('🛡️ Risk Management Request:', { symbol, action });
|
||||
|
||||
// Get current position first
|
||||
const positionResponse = await fetch(`${process.env.INTERNAL_API_URL || 'http://localhost:9001'}/api/automation/position-monitor`);
|
||||
const positionData = await positionResponse.json();
|
||||
|
||||
if (!positionData.success || !positionData.monitor.hasPosition) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'No active position found'
|
||||
});
|
||||
}
|
||||
|
||||
const position = positionData.monitor.position;
|
||||
console.log('📊 Current position:', position);
|
||||
|
||||
// Calculate proper stop-loss and take-profit levels
|
||||
const entryPrice = position.entryPrice;
|
||||
const currentPrice = position.currentPrice;
|
||||
const size = position.size;
|
||||
const side = position.side; // 'short' or 'long'
|
||||
|
||||
let stopLossPrice, takeProfitPrice;
|
||||
|
||||
if (side.toLowerCase() === 'short') {
|
||||
// For SHORT positions:
|
||||
// Stop-loss: BUY above entry price (limit losses)
|
||||
// Take-profit: BUY below current price (secure profits)
|
||||
stopLossPrice = entryPrice * 1.025; // 2.5% above entry
|
||||
takeProfitPrice = currentPrice * 0.985; // 1.5% below current (secure profits)
|
||||
} else {
|
||||
// For LONG positions:
|
||||
// Stop-loss: SELL below entry price (limit losses)
|
||||
// Take-profit: SELL above current price (secure profits)
|
||||
stopLossPrice = entryPrice * 0.975; // 2.5% below entry
|
||||
takeProfitPrice = currentPrice * 1.015; // 1.5% above current
|
||||
}
|
||||
|
||||
const orders = [];
|
||||
|
||||
// Place stop-loss order
|
||||
try {
|
||||
const stopLossOrder = {
|
||||
symbol: symbol,
|
||||
side: side.toLowerCase() === 'short' ? 'BUY' : 'SELL',
|
||||
amount: size,
|
||||
orderType: 'TRIGGER_LIMIT',
|
||||
triggerPrice: stopLossPrice,
|
||||
limitPrice: side.toLowerCase() === 'short' ? stopLossPrice * 1.002 : stopLossPrice * 0.998,
|
||||
reduceOnly: true
|
||||
};
|
||||
|
||||
console.log('🛑 Placing stop-loss order:', stopLossOrder);
|
||||
|
||||
const slResponse = await fetch(`${process.env.INTERNAL_API_URL || 'http://localhost:9001'}/api/drift/place-order`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(stopLossOrder)
|
||||
});
|
||||
|
||||
const slResult = await slResponse.json();
|
||||
orders.push({
|
||||
type: 'stop-loss',
|
||||
success: slResult.success,
|
||||
price: stopLossPrice,
|
||||
orderId: slResult.orderId,
|
||||
error: slResult.error
|
||||
});
|
||||
|
||||
} catch (slError) {
|
||||
orders.push({
|
||||
type: 'stop-loss',
|
||||
success: false,
|
||||
error: slError.message
|
||||
});
|
||||
}
|
||||
|
||||
// Place take-profit order
|
||||
try {
|
||||
const takeProfitOrder = {
|
||||
symbol: symbol,
|
||||
side: side.toLowerCase() === 'short' ? 'BUY' : 'SELL',
|
||||
amount: size,
|
||||
orderType: 'TRIGGER_LIMIT',
|
||||
triggerPrice: takeProfitPrice,
|
||||
limitPrice: side.toLowerCase() === 'short' ? takeProfitPrice * 0.998 : takeProfitPrice * 1.002,
|
||||
reduceOnly: true
|
||||
};
|
||||
|
||||
console.log('🎯 Placing take-profit order:', takeProfitOrder);
|
||||
|
||||
const tpResponse = await fetch(`${process.env.INTERNAL_API_URL || 'http://localhost:9001'}/api/drift/place-order`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(takeProfitOrder)
|
||||
});
|
||||
|
||||
const tpResult = await tpResponse.json();
|
||||
orders.push({
|
||||
type: 'take-profit',
|
||||
success: tpResult.success,
|
||||
price: takeProfitPrice,
|
||||
orderId: tpResult.orderId,
|
||||
error: tpResult.error
|
||||
});
|
||||
|
||||
} catch (tpError) {
|
||||
orders.push({
|
||||
type: 'take-profit',
|
||||
success: false,
|
||||
error: tpError.message
|
||||
});
|
||||
}
|
||||
|
||||
const successfulOrders = orders.filter(o => o.success);
|
||||
const failedOrders = orders.filter(o => !o.success);
|
||||
|
||||
return NextResponse.json({
|
||||
success: successfulOrders.length > 0,
|
||||
message: `Risk management: ${successfulOrders.length} orders placed, ${failedOrders.length} failed`,
|
||||
position: {
|
||||
symbol: position.symbol,
|
||||
side: position.side,
|
||||
size: position.size,
|
||||
entryPrice: position.entryPrice,
|
||||
currentPrice: position.currentPrice,
|
||||
unrealizedPnl: position.unrealizedPnl
|
||||
},
|
||||
orders: orders,
|
||||
riskManagement: {
|
||||
stopLossPrice: stopLossPrice.toFixed(4),
|
||||
takeProfitPrice: takeProfitPrice.toFixed(4),
|
||||
riskPercent: side.toLowerCase() === 'short' ? '+2.5%' : '-2.5%',
|
||||
rewardPercent: side.toLowerCase() === 'short' ? '-1.5%' : '+1.5%'
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Risk management error:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'Failed to place risk management orders',
|
||||
details: error.message
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user