Files
trading_bot_v3/app/api/automation/status/route.js
mindesbunister 71e1a64b5d fix: correct entry prices and position sizing in trading system
- Fixed automation service to use real SOL price (~89) instead of hardcoded 00
- Updated position size calculation to properly convert USD investment to token amount
- Enhanced trade display to show separate entry/exit prices with price difference
- Added data quality warnings for trades with missing exit data
- Updated API to use current SOL price (189.50) and improved trade result determination
- Added detection and warnings for old trades with incorrect price data

Resolves issue where trades showed 9-100 entry prices instead of real SOL price of 89
and position sizes of 2.04 SOL instead of correct ~0.53 SOL for 00 investment
2025-07-21 09:26:48 +02:00

92 lines
2.5 KiB
JavaScript

import { NextResponse } from 'next/server'
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
export async function GET() {
try {
// Get the latest automation session with correct data
const session = await prisma.automationSession.findFirst({
where: {
userId: 'default-user',
symbol: 'SOLUSD',
timeframe: '1h'
},
orderBy: { createdAt: 'desc' }
})
if (!session) {
return NextResponse.json({
success: true,
status: {
isActive: false,
mode: 'SIMULATION',
symbol: 'SOLUSD',
timeframe: '1h',
totalTrades: 0,
successfulTrades: 0,
winRate: 0,
totalPnL: 0,
errorCount: 0
}
})
}
// Get actual trade data to calculate real statistics
// Get ALL trades count for consistency
const totalTradesCount = await prisma.trade.count({
where: {
userId: session.userId,
symbol: session.symbol
}
})
const trades = await prisma.trade.findMany({
where: {
userId: session.userId,
symbol: session.symbol
},
orderBy: { createdAt: 'desc' }
})
const completedTrades = trades.filter(t => t.status === 'COMPLETED')
const successfulTrades = completedTrades.filter(t => {
const profit = t.profit || 0
return profit > 0
})
const totalPnL = completedTrades.reduce((sum, trade) => {
const profit = trade.profit || 0
return sum + profit
}, 0)
const winRate = completedTrades.length > 0 ?
(successfulTrades.length / completedTrades.length * 100) : 0
return NextResponse.json({
success: true,
status: {
isActive: session.status === 'ACTIVE',
mode: session.mode,
symbol: session.symbol,
timeframe: session.timeframe,
totalTrades: totalTradesCount, // Use actual total count
successfulTrades: successfulTrades.length,
winRate: Math.round(winRate * 10) / 10, // Round to 1 decimal
totalPnL: Math.round(totalPnL * 100) / 100, // Round to 2 decimals
lastAnalysis: session.lastAnalysis,
lastTrade: session.lastTrade,
nextScheduled: session.nextScheduled,
errorCount: session.errorCount,
lastError: session.lastError
}
})
} catch (error) {
console.error('Automation status error:', error)
return NextResponse.json({
success: false,
error: 'Failed to get automation status'
}, { status: 500 })
}
}