- 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)
51 lines
1.4 KiB
TypeScript
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 }
|
|
)
|
|
}
|
|
}
|