fix: eliminate excessive P&L calculations and restore CoinGecko price source

- Fixed Prisma table name errors in price-monitor.ts (trades vs trade, automation_sessions vs automationSession)
- Commented out excessive P&L calculation logging in analysis-details API that was processing all 69 trades
- Restored CoinGecko as primary price source (was falling back to Binance due to DB errors)
- Optimized analysis-details to skip P&L calculations for FAILED/EXECUTED trades
- Added comprehensive cleanup system for orphaned orders
- Performance improvement: eliminated unnecessary processing of old trade data

Result: Clean logs, efficient price fetching from CoinGecko, no excessive calculations
This commit is contained in:
mindesbunister
2025-07-28 13:45:17 +02:00
parent 3ba760df2d
commit 08970acc85
12 changed files with 1291 additions and 310 deletions

View File

@@ -174,7 +174,7 @@ class PriceMonitor extends EventEmitter {
}
private async getActiveTradesForMonitoring(): Promise<any[]> {
return await prisma.trade.findMany({
return await prisma.trades.findMany({
where: {
status: 'OPEN',
isAutomated: true
@@ -230,7 +230,7 @@ class PriceMonitor extends EventEmitter {
const leverage = trade.leverage || 1
// 🔥 FIX: Get actual trading amount from session settings
const session = await prisma.automationSession.findFirst({
const session = await prisma.automation_sessions.findFirst({
where: { userId: trade.userId, symbol: trade.symbol },
orderBy: { createdAt: 'desc' }
})
@@ -343,7 +343,7 @@ class PriceMonitor extends EventEmitter {
private async updateTradeCurrentData(tradeId: string, currentPrice: number, currentPnL: number): Promise<void> {
try {
await prisma.trade.update({
await prisma.trades.update({
where: { id: tradeId },
data: {
// Store current price and PnL for reference
@@ -442,7 +442,7 @@ class PriceMonitor extends EventEmitter {
// Close a trade by updating its status and exit data
private async closeTrade(tradeId: string, exitPrice: number, reason: string): Promise<void> {
try {
const trade = await prisma.trade.findUnique({ where: { id: tradeId } })
const trade = await prisma.trades.findUnique({ where: { id: tradeId } })
if (!trade) return
const entryPrice = trade.entryPrice || trade.price
@@ -450,7 +450,7 @@ class PriceMonitor extends EventEmitter {
const tradingAmount = trade.amount * entryPrice // Estimate trading amount
const pnlPercent = ((pnl / tradingAmount) * 100)
await prisma.trade.update({
await prisma.trades.update({
where: { id: tradeId },
data: {
status: 'COMPLETED',