feat: pre-implementation backup - current ai-analysis system state
- Current basic analysis system with technical indicators - About to implement professional trading desk features - Backup before major analysis prompt enhancements
This commit is contained in:
85
app/api/safe-paper-trading/database-trades/route.js
Normal file
85
app/api/safe-paper-trading/database-trades/route.js
Normal file
@@ -0,0 +1,85 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
// Get recent trades from database
|
||||
const trades = await prisma.trades.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
take: 50, // Last 50 trades
|
||||
select: {
|
||||
id: true,
|
||||
symbol: true,
|
||||
side: true,
|
||||
amount: true,
|
||||
price: true,
|
||||
entryPrice: true,
|
||||
status: true,
|
||||
confidence: true,
|
||||
createdAt: true,
|
||||
profit: true,
|
||||
fees: true,
|
||||
tradingMode: true,
|
||||
isAutomated: true
|
||||
}
|
||||
})
|
||||
|
||||
// Convert database trades to Safe Paper Trading format
|
||||
const formattedTrades = trades.map(trade => ({
|
||||
id: trade.id,
|
||||
symbol: trade.symbol,
|
||||
side: trade.side.toUpperCase(),
|
||||
positionSize: trade.amount,
|
||||
entryPrice: trade.entryPrice || trade.price,
|
||||
confidence: trade.confidence,
|
||||
reasoning: `${trade.isAutomated ? 'Automated' : 'Manual'} ${trade.tradingMode || 'trade'}`,
|
||||
source: trade.isAutomated ? 'automation' : 'manual',
|
||||
status: trade.status === 'EXECUTED' ? 'OPEN' : trade.status,
|
||||
timestamp: trade.createdAt,
|
||||
pnl: trade.profit || 0,
|
||||
fees: trade.fees || 0
|
||||
}))
|
||||
|
||||
// Filter for valid paper trades (exclude failed ones)
|
||||
const validTrades = formattedTrades.filter(trade =>
|
||||
trade.status !== 'FAILED' && trade.positionSize > 0
|
||||
)
|
||||
|
||||
// Calculate stats
|
||||
const totalTrades = validTrades.length
|
||||
const totalValue = validTrades.reduce((sum, trade) => {
|
||||
return sum + (trade.side === 'BUY' ? -trade.positionSize : trade.positionSize) + trade.pnl
|
||||
}, 0)
|
||||
|
||||
const buyTrades = validTrades.filter(t => t.side === 'BUY').length
|
||||
const sellTrades = validTrades.filter(t => t.side === 'SELL').length
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
trades: validTrades,
|
||||
totalTrades,
|
||||
totalValue,
|
||||
buyTrades,
|
||||
sellTrades,
|
||||
source: 'database',
|
||||
timestamp: new Date().toISOString()
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Database trades error:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: 'Failed to fetch database trades',
|
||||
error: error.message,
|
||||
trades: [],
|
||||
totalTrades: 0,
|
||||
totalValue: 0,
|
||||
buyTrades: 0,
|
||||
sellTrades: 0
|
||||
}, { status: 500 })
|
||||
} finally {
|
||||
await prisma.$disconnect()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user