adding backtest table / pages
This commit is contained in:
parent
38a6e73ad5
commit
a876f3c35b
19 changed files with 1058 additions and 69 deletions
|
|
@ -0,0 +1,93 @@
|
|||
import type { BacktestResult } from '../types/backtest.types';
|
||||
import { useState, useMemo } from 'react';
|
||||
import { Chart } from '../../../components/charts';
|
||||
|
||||
interface BacktestChartProps {
|
||||
result: BacktestResult | null;
|
||||
isLoading: boolean;
|
||||
}
|
||||
|
||||
export function BacktestChart({ result, isLoading }: BacktestChartProps) {
|
||||
const [selectedSymbol, setSelectedSymbol] = useState<string>('');
|
||||
|
||||
const chartData = useMemo(() => {
|
||||
if (!result?.equity || !result?.ohlcData) return null;
|
||||
|
||||
const symbols = Object.keys(result.ohlcData);
|
||||
const symbol = selectedSymbol || symbols[0] || '';
|
||||
|
||||
const ohlcData = result.ohlcData[symbol] || [];
|
||||
const equityData = result.equity.map(e => ({
|
||||
time: new Date(e.date).getTime() / 1000,
|
||||
value: e.value
|
||||
}));
|
||||
|
||||
// Find trades for this symbol
|
||||
const symbolTrades = result.trades?.filter(t => t.symbol === symbol) || [];
|
||||
const tradeMarkers = symbolTrades.map(trade => ({
|
||||
time: new Date(trade.entryDate).getTime() / 1000,
|
||||
position: 'belowBar' as const,
|
||||
color: trade.side === 'buy' ? '#10b981' : '#ef4444',
|
||||
shape: trade.side === 'buy' ? 'arrowUp' : 'arrowDown' as const,
|
||||
text: `${trade.side === 'buy' ? 'Buy' : 'Sell'} @ $${trade.entryPrice.toFixed(2)}`
|
||||
}));
|
||||
|
||||
return {
|
||||
ohlcData: ohlcData.map(d => ({
|
||||
time: d.time / 1000,
|
||||
open: d.open,
|
||||
high: d.high,
|
||||
low: d.low,
|
||||
close: d.close,
|
||||
volume: d.volume
|
||||
})),
|
||||
equityData,
|
||||
tradeMarkers,
|
||||
symbols
|
||||
};
|
||||
}, [result, selectedSymbol]);
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="bg-surface-secondary rounded-lg border border-border p-4 h-full animate-pulse">
|
||||
<div className="h-full bg-surface rounded"></div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!result || !chartData) {
|
||||
return (
|
||||
<div className="bg-surface-secondary rounded-lg border border-border p-8 text-center h-full flex items-center justify-center">
|
||||
<p className="text-text-secondary">Run a backtest to see the equity curve and price chart.</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="h-full flex flex-col">
|
||||
{chartData.symbols.length > 1 && (
|
||||
<div className="flex space-x-2 mb-4">
|
||||
<label className="text-sm text-text-secondary">Symbol:</label>
|
||||
<select
|
||||
value={selectedSymbol || chartData.symbols[0]}
|
||||
onChange={(e) => setSelectedSymbol(e.target.value)}
|
||||
className="px-2 py-1 bg-background border border-border rounded text-sm text-text-primary focus:outline-none focus:ring-1 focus:ring-primary-500"
|
||||
>
|
||||
{chartData.symbols.map(symbol => (
|
||||
<option key={symbol} value={symbol}>{symbol}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex-1">
|
||||
<Chart
|
||||
data={chartData.ohlcData}
|
||||
equityData={chartData.equityData}
|
||||
markers={chartData.tradeMarkers}
|
||||
height={500}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue