import { memo, useState } from 'react'; import { ErrorBoundary } from '../../../components/ErrorBoundary'; import type { RunResult } from '../services/backtestApiV2'; import type { Position } from '../types/backtest.types'; import { BacktestChart } from './BacktestChart'; import { CompactPerformanceMetrics } from './CompactPerformanceMetrics'; import { PositionsSummary } from './PositionsSummary'; import { ResultProvenance } from './ResultProvenance'; interface BacktestPlaybackProps { result: RunResult | null; isLoading: boolean; } export const BacktestPlayback = memo(function BacktestPlayback({ result, isLoading, }: BacktestPlaybackProps) { const [showPositions, setShowPositions] = useState(false); if (isLoading) { return (
Loading playback data...
); } if (!result) { return (
No data available
); } // Get positions from the result const openPositions: Position[] = []; const closedPositions: Position[] = []; result.positions.forEach(position => { if (position.quantity !== 0) { openPositions.push(position); } else if (position.realizedPnl !== 0) { closedPositions.push(position); } }); return (
{/* Chart Section */}

Chart failed to load

} >
{/* Performance Metrics and Positions - Fixed height section */}
setShowPositions(true)} />
{/* Detailed Positions View - Optional expanded view */} {showPositions && (openPositions.length > 0 || closedPositions.length > 0) && (

All Positions

{openPositions.length > 0 && (

Open Positions ({openPositions.length})

Symbol
Side
Quantity
Entry Price
Current Price
P&L
{openPositions.map((position, index) => { const quantity = position.quantity || 0; const avgPrice = position.averagePrice || position.avgPrice || 0; const currentPrice = position.currentPrice || position.lastPrice || avgPrice; const side = quantity > 0 ? 'long' : 'short'; const absQuantity = Math.abs(quantity); const pnl = (currentPrice - avgPrice) * quantity; const pnlPercent = avgPrice > 0 ? (pnl / (avgPrice * absQuantity)) * 100 : 0; return (
{position.symbol}
{side.toUpperCase()}
{absQuantity}
${avgPrice.toFixed(2)}
${currentPrice.toFixed(2)}
= 0 ? 'text-success' : 'text-error'}`} > ${pnl.toFixed(2)} ({pnlPercent >= 0 ? '+' : ''} {pnlPercent.toFixed(2)}%)
); })}
)} {closedPositions.length > 0 && (

Closed Positions ({closedPositions.length})

{closedPositions.map((position, index) => (
{position.symbol} Closed
= 0 ? 'text-success' : 'text-error'}`} > Realized P&L: ${position.realizedPnl.toFixed(2)}
))}
)}
Unrealized P&L
{ const quantity = p.quantity || 0; const avgPrice = p.averagePrice || p.avgPrice || 0; const currentPrice = p.currentPrice || p.lastPrice || avgPrice; return sum + (currentPrice - avgPrice) * quantity; }, 0) >= 0 ? 'text-success' : 'text-error' }`} > $ {openPositions .reduce((sum, p) => { const quantity = p.quantity || 0; const avgPrice = p.averagePrice || p.avgPrice || 0; const currentPrice = p.currentPrice || p.lastPrice || avgPrice; return sum + (currentPrice - avgPrice) * quantity; }, 0) .toFixed(2)}
Realized P&L
sum + (p.realizedPnl || 0), 0) >= 0 ? 'text-success' : 'text-error' }`} > ${closedPositions.reduce((sum, p) => sum + (p.realizedPnl || 0), 0).toFixed(2)}
Total P&L
{ const quantity = p.quantity || 0; const avgPrice = p.averagePrice || p.avgPrice || 0; const currentPrice = p.currentPrice || p.lastPrice || avgPrice; return sum + (currentPrice - avgPrice) * quantity; }, 0) + closedPositions.reduce((sum, p) => sum + (p.realizedPnl || 0), 0) >= 0 ? 'text-success' : 'text-error' }`} > $ {( openPositions.reduce((sum, p) => { const quantity = p.quantity || 0; const avgPrice = p.averagePrice || p.avgPrice || 0; const currentPrice = p.currentPrice || p.lastPrice || avgPrice; return sum + (currentPrice - avgPrice) * quantity; }, 0) + closedPositions.reduce((sum, p) => sum + (p.realizedPnl || 0), 0) ).toFixed(2)}
)} ); });