import type { RunResult } from '../services/backtestApiV2'; interface ExtendedMetrics { // Core metrics totalReturn?: number | null; sharpeRatio?: number | null; maxDrawdown?: number | null; winRate?: number | null; totalProfit?: number | null; totalPnl?: number | null; profitFactor?: number | null; totalTrades?: number | null; avgWin?: number | null; avgLoss?: number | null; expectancy?: number | null; sortinoRatio?: number | null; calmarRatio?: number | null; profitableTrades?: number | null; // Extended metrics from orchestrator annualizedReturn?: number | null; volatility?: number | null; avgHoldingPeriod?: number | null; maxConsecutiveLosses?: number | null; maxConsecutiveWins?: number | null; payoffRatio?: number | null; largestWin?: number | null; largestLoss?: number | null; avgWinLoss?: number | null; skewness?: number | null; kurtosis?: number | null; tailRatio?: number | null; kellyFraction?: number | null; informationRatio?: number | null; avgTradesPerDay?: number | null; } interface CompactPerformanceMetricsProps { result: RunResult | null; isLoading: boolean; } export function CompactPerformanceMetrics({ result, isLoading }: CompactPerformanceMetricsProps) { if (isLoading || !result) { return null; } const metrics = result.metrics as ExtendedMetrics; const analytics = result.analytics || {}; // Merge metrics from both sources const allMetrics = { ...metrics, ...analytics, // Override with metrics values if they exist ...metrics, }; // Never infer a historical cash amount from a mutable legacy definition's capital. const totalProfit = allMetrics.totalProfit ?? allMetrics.totalPnl ?? (result.provenance?.configurationSource === 'stored-snapshot' && typeof allMetrics.totalReturn === 'number' && typeof result.config?.initialCapital === 'number' ? allMetrics.totalReturn * result.config.initialCapital : undefined); const formatValue = ( value: number | null | undefined, format: 'percent' | 'number' | 'currency', decimals = 2 ) => { if (value === undefined || value === null) { return '-'; } switch (format) { case 'percent': return `${(value * 100).toFixed(decimals)}%`; case 'currency': { const prefix = value < 0 ? '-$' : '$'; return `${prefix}${Math.abs(value).toFixed(decimals)}`; } case 'number': return value.toFixed(decimals); } }; const getColorClass = ( value: number | null | undefined, thresholds: { good: number; warning?: number } ) => { if (value === undefined || value === null) { return 'text-text-secondary'; } if (thresholds.warning !== undefined) { if (value >= thresholds.good) { return 'text-success'; } if (value >= thresholds.warning) { return 'text-warning'; } return 'text-error'; } return value >= thresholds.good ? 'text-success' : 'text-error'; }; return (

Performance Analytics

{/* Column 1 - Return Metrics */}
Total Return {formatValue(allMetrics.totalReturn, 'percent')}
Sharpe Ratio {formatValue(allMetrics.sharpeRatio, 'number')}
Max Drawdown {formatValue(allMetrics.maxDrawdown, 'percent')}
Win Rate {formatValue(allMetrics.winRate, 'percent', 1)}
Total Profit {formatValue(totalProfit, 'currency')}
{/* Column 2 - Risk Metrics */}
Profit Factor {formatValue(allMetrics.profitFactor, 'number')}
Total Trades {allMetrics.totalTrades ?? '-'}
Avg Win/Loss {formatValue(allMetrics.avgWin, 'currency')} / {formatValue(allMetrics.avgLoss, 'currency')}
Expectancy {formatValue(allMetrics.expectancy, 'currency')}
Sortino Ratio {formatValue(allMetrics.sortinoRatio, 'number')}
{/* Column 3 - Additional Metrics */}
Annual Return {formatValue(allMetrics.annualizedReturn, 'percent')}
Volatility {formatValue( allMetrics.volatility ? allMetrics.volatility / 100 : undefined, 'percent', 1 )}
Avg Holding {allMetrics.avgHoldingPeriod ? `${(allMetrics.avgHoldingPeriod / 60).toFixed(1)}h` : '-'}
Max Consec Loss 5 ? 'text-error' : 'text-text-primary'}`} > {allMetrics.maxConsecutiveLosses ?? '-'}
Payoff Ratio {formatValue(allMetrics.payoffRatio, 'number')}
{/* Advanced Metrics - Show only if available */} {(allMetrics.skewness !== undefined || allMetrics.kurtosis !== undefined || allMetrics.informationRatio !== undefined || allMetrics.kellyFraction !== undefined) && (
{allMetrics.informationRatio !== undefined && (
Info Ratio
{formatValue(allMetrics.informationRatio, 'number')}
)} {allMetrics.skewness !== undefined && (
Skewness
{formatValue(allMetrics.skewness, 'number')}
)} {allMetrics.kurtosis !== undefined && (
Kurtosis
{formatValue(allMetrics.kurtosis, 'number')}
)} {allMetrics.kellyFraction !== undefined && (
Kelly %
{formatValue(allMetrics.kellyFraction, 'percent')}
)}
)} {/* Additional Metrics Row */}
Calmar
{formatValue(allMetrics.calmarRatio, 'number')}
Profitable
{allMetrics.profitableTrades ?? '-'}/{allMetrics.totalTrades ?? '-'}
Exposure
{typeof result.analytics?.exposureTime === 'number' ? formatValue(result.analytics.exposureTime, 'percent', 1) : '-'}
Largest Win
{formatValue(allMetrics.largestWin, 'currency')}
Largest Loss
{formatValue(allMetrics.largestLoss, 'currency')}
); }