import type { ColumnDef } from '@tanstack/react-table'; import type { BacktestRunTrade as Trade } from '@stock-bot/types'; import { DataTable } from '@/components/ui/DataTable'; import { browserLog } from '@/utils/browserLog'; import type { RunResult } from '../services/backtestApiV2'; interface BacktestTradesProps { result: RunResult | null; isLoading: boolean; } export function BacktestTrades({ result, isLoading }: BacktestTradesProps) { if (isLoading) { return (
{[...Array(5)].map((_, i) => (
))}
); } if (!result || !result.trades || result.trades.length === 0) { return (

No trades executed in this backtest.

); } // Add debug logging browserLog.log('Trades data:', result.trades); const columns: ColumnDef[] = [ { accessorKey: 'symbol', header: 'Symbol', size: 100, cell: ({ getValue }) => ( {getValue() as string} ), }, { id: 'side', header: 'Side', size: 80, cell: ({ row }) => { const trade = row.original; const side = trade.side || (trade.quantity > 0 ? 'buy' : 'sell'); return ( {side.toUpperCase()} ); }, }, { id: 'date', header: 'Date', size: 180, cell: ({ row }) => { const trade = row.original; const date = trade.timestamp; return ( {date ? new Date(date).toLocaleString() : '-'} ); }, }, { id: 'price', header: 'Price', size: 100, cell: ({ row }) => { const trade = row.original; const price = trade.price || trade.entryPrice; return ( ${price !== null && price !== undefined ? price.toFixed(2) : '0.00'} ); }, }, { accessorKey: 'quantity', header: 'Quantity', size: 80, cell: ({ getValue }) => { const qty = Math.abs((getValue() as number) || 0); return {qty}; }, }, { accessorKey: 'commission', header: 'Commission', size: 100, cell: ({ getValue }) => { const commission = (getValue() as number) || 0; return ${commission.toFixed(2)}; }, }, { id: 'pnl', header: 'P&L', size: 100, cell: ({ row }) => { const trade = row.original; const pnl = trade.pnl ?? 0; return ( = 0 ? 'text-success' : 'text-error'}`}> ${pnl.toFixed(2)} ); }, }, ]; // Calculate trade statistics const tradeStats = result.trades.reduce( (stats, trade) => { const pnl = trade.pnl ?? 0; if (pnl > 0) { stats.wins++; stats.totalWin += pnl; } else if (pnl < 0) { stats.losses++; stats.totalLoss += Math.abs(pnl); } stats.totalPnL += pnl; return stats; }, { wins: 0, losses: 0, totalWin: 0, totalLoss: 0, totalPnL: 0 } ); const winRate = result.metrics.winRate * 100; return (

Closed Trades

{result.metrics.totalTrades}

Win Rate

{winRate.toFixed(1)}%

Wins

{tradeStats.wins}

Losses

{tradeStats.losses}

Total P&L

= 0 ? 'text-success' : 'text-error'}`} > ${tradeStats.totalPnL.toFixed(2)}

{result.positions && Object.keys(result.positions).length > 0 && (() => { const positionsArray = result.positions; const positionColumns: ColumnDef<(typeof positionsArray)[0]>[] = [ { accessorKey: 'symbol', header: 'Symbol', size: 100, cell: ({ getValue }) => ( {getValue() as string} ), }, { accessorKey: 'quantity', header: 'Quantity', size: 100, cell: ({ getValue }) => ( {(getValue() as number) || 0} ), }, { accessorKey: 'averagePrice', header: 'Avg Price', size: 120, cell: ({ getValue }) => { const price = getValue() as number; return ( ${price !== null && price !== undefined ? price.toFixed(2) : '0.00'} ); }, }, { accessorKey: 'unrealizedPnl', header: 'Unrealized P&L', size: 150, cell: ({ getValue }) => { const pnl = (getValue() as number) || 0; return ( = 0 ? 'text-success' : 'text-error'}`} > ${pnl !== null && pnl !== undefined ? pnl.toFixed(2) : '0.00'} ); }, }, ]; return (

Open Positions

); })()}
); }