Files
trading_bot_v4/app/api/analytics/last-trade/route.ts
mindesbunister aecdc108f6 Add last trade details to analytics dashboard
- Add getLastTrade() function to database service
- Create /api/analytics/last-trade endpoint
- Display last trade with full details on analytics page
- Show entry/exit prices, P&L, position size, targets
- Visual indicators for trade direction and exit reason
- Helps quickly diagnose where trades went (TP1, TP2, or SL)
2025-10-31 10:47:19 +01:00

51 lines
1.4 KiB
TypeScript

/**
* Last Trade API Endpoint
*
* Returns details of the most recent trade
*/
import { NextResponse } from 'next/server'
import { getLastTrade } from '@/lib/database/trades'
export async function GET() {
try {
const trade = await getLastTrade()
if (!trade) {
return NextResponse.json({
trade: null,
})
}
// Format the trade data for the frontend
const formattedTrade = {
id: trade.id,
symbol: trade.symbol,
direction: trade.direction,
entryPrice: trade.entryPrice,
entryTime: trade.entryTime.toISOString(),
exitPrice: trade.exitPrice || undefined,
exitTime: trade.exitTime?.toISOString() || undefined,
exitReason: trade.exitReason || undefined,
realizedPnL: trade.realizedPnL || undefined,
realizedPnLPercent: trade.realizedPnLPercent || undefined,
positionSizeUSD: trade.positionSizeUSD,
leverage: trade.leverage,
stopLossPrice: trade.stopLossPrice,
takeProfit1Price: trade.takeProfit1Price,
takeProfit2Price: trade.takeProfit2Price,
isTestTrade: trade.isTestTrade || false,
}
return NextResponse.json({
trade: formattedTrade,
})
} catch (error) {
console.error('Failed to fetch last trade:', error)
return NextResponse.json(
{ error: 'Failed to fetch last trade' },
{ status: 500 }
)
}
}