feat(research): select and reuse frozen datasets in workbench

This commit is contained in:
Boki 2026-10-23 18:13:37 -04:00
parent 6a67ae8683
commit b106c1ca85
29 changed files with 2295 additions and 426 deletions

View file

@ -1,7 +1,8 @@
import { XMarkIcon } from '@heroicons/react/24/solid';
import { useEffect, useRef, useState } from 'react';
import { parseInstrumentId } from '@stock-bot/types';
import { backtestApiV2, type Backtest, type Run } from '../services/backtestApiV2';
import type { HistoricalSelection } from '../services/historical-selection';
import { HistoricalDataSelection } from './HistoricalDataSelection';
interface CreateBacktestDialogProps {
onClose: () => void;
@ -9,7 +10,15 @@ interface CreateBacktestDialogProps {
}
export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialogProps) {
const [formData, setFormData] = useState({
const [formData, setFormData] = useState<
HistoricalSelection & {
name: string;
strategy: string;
initialCapital: number;
commission: number;
slippage: number;
}
>({
name: '',
strategy: 'moving-average',
symbols: ['AAPL.US'],
@ -18,10 +27,12 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
initialCapital: 100000,
commission: 0,
slippage: 0,
priceAdjustment: 'raw' as 'raw' | 'provider-adjusted',
priceAdjustment: 'raw',
dataFrequency: '1d',
dataSource: 'auto',
});
const [symbolInput, setSymbolInput] = useState('');
const [historicalValid, setHistoricalValid] = useState(true);
const [phase, setPhase] = useState<'idle' | 'saving' | 'submitting'>('idle');
const isSubmitting = phase !== 'idle';
const [error, setError] = useState<string | null>(null);
@ -47,7 +58,7 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
};
const handleSubmit = async (runAfterSave: boolean) => {
if (pending.current || !active.current) {
if (pending.current || !active.current || (!saved.current && !historicalValid)) {
return;
}
@ -71,7 +82,13 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
config: {
commission: formData.commission,
slippage: formData.slippage,
dataPolicy: { adjustment: formData.priceAdjustment, timestamp: 'utc-session-date' },
dataFrequency: formData.dataFrequency,
dataSource: formData.dataSource,
datasetHash: formData.datasetHash,
dataPolicy: {
adjustment: formData.priceAdjustment ?? 'raw',
timestamp: formData.dataFrequency === '1d' ? 'utc-session-date' : 'utc-instant',
},
},
});
if (!active.current) {
@ -109,27 +126,6 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
}
};
const addSymbol = () => {
const symbol = symbolInput.trim().toUpperCase();
try {
parseInstrumentId(symbol);
} catch {
alert('Enter an exchange-qualified ID, for example AAPL.US');
return;
}
if (symbol && !formData.symbols.includes(symbol)) {
setFormData({ ...formData, symbols: [...formData.symbols, symbol] });
setSymbolInput('');
}
};
const removeSymbol = (symbol: string) => {
setFormData({
...formData,
symbols: formData.symbols.filter(s => s !== symbol),
});
};
return (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
<div className="bg-surface-secondary rounded-lg p-6 w-full max-w-2xl max-h-[90vh] overflow-y-auto">
@ -187,92 +183,12 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
</select>
</div>
<div>
<label className="block text-sm font-medium text-text-secondary mb-2">Symbols</label>
<div className="flex gap-2 mb-2">
<input
type="text"
value={symbolInput}
onChange={e => setSymbolInput(e.target.value)}
onKeyPress={e => e.key === 'Enter' && (e.preventDefault(), addSymbol())}
className="flex-1 px-3 py-2 bg-background border border-border rounded-md text-text-primary focus:outline-none focus:ring-1 focus:ring-primary-500"
placeholder="Instrument ID (e.g., AAPL.US)"
/>
<button
type="button"
onClick={addSymbol}
className="px-4 py-2 bg-primary-500 text-white rounded-md text-sm font-medium hover:bg-primary-600 transition-colors"
>
Add
</button>
</div>
<div className="flex flex-wrap gap-2">
{formData.symbols.map(symbol => (
<span
key={symbol}
className="inline-flex items-center gap-1 px-3 py-1 bg-background border border-border rounded-full text-sm text-text-primary"
>
{symbol}
<button
type="button"
onClick={() => removeSymbol(symbol)}
className="hover:text-error transition-colors"
>
<XMarkIcon className="w-4 h-4" />
</button>
</span>
))}
</div>
</div>
<div>
<label className="block text-sm font-medium text-text-secondary mb-2">
Historical prices
</label>
<select
value={formData.priceAdjustment}
onChange={e =>
setFormData({
...formData,
priceAdjustment:
e.target.value === 'provider-adjusted' ? 'provider-adjusted' : 'raw',
})
}
className="w-full px-3 py-2 bg-background border border-border rounded-md text-text-primary"
>
<option value="raw">Raw prices (no corporate-action adjustment)</option>
<option value="provider-adjusted">
Provider adjusted (splits/dividends; reported volume)
</option>
</select>
</div>
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-text-secondary mb-2">
Start Date
</label>
<input
type="date"
value={formData.startDate}
onChange={e => setFormData({ ...formData, startDate: e.target.value })}
className="w-full px-3 py-2 bg-background border border-border rounded-md text-text-primary focus:outline-none focus:ring-1 focus:ring-primary-500"
required
/>
</div>
<div>
<label className="block text-sm font-medium text-text-secondary mb-2">
End Date
</label>
<input
type="date"
value={formData.endDate}
onChange={e => setFormData({ ...formData, endDate: e.target.value })}
className="w-full px-3 py-2 bg-background border border-border rounded-md text-text-primary focus:outline-none focus:ring-1 focus:ring-primary-500"
required
/>
</div>
</div>
<HistoricalDataSelection
value={formData}
onChange={selection => setFormData(previous => ({ ...previous, ...selection }))}
onValidityChange={setHistoricalValid}
disabled={isSubmitting || saved.current !== null}
/>
<div>
<label className="block text-sm font-medium text-text-secondary mb-2">
@ -335,7 +251,7 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
</button>
<button
type="submit"
disabled={isSubmitting}
disabled={isSubmitting || (!saved.current && !historicalValid)}
className="px-4 py-2 bg-primary-500 text-white rounded-md text-sm font-medium hover:bg-primary-600 transition-colors"
>
{isSubmitting
@ -347,7 +263,7 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
{!saved.current && (
<button
type="button"
disabled={isSubmitting}
disabled={isSubmitting || (!saved.current && !historicalValid)}
onClick={e => {
if (e.currentTarget.form?.reportValidity()) {
void handleSubmit(true);