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,9 +1,9 @@
import { useEffect, useState } from 'react';
import { parseInstrumentId } from '@stock-bot/types';
import type { BacktestConfig } from '../types';
import { HistoricalDataSelection } from './HistoricalDataSelection';
interface BacktestConfigurationProps {
onSubmit: (config: BacktestConfig) => void;
onSubmit: (config: BacktestConfig) => void | Promise<void>;
disabled?: boolean;
initialConfig?: BacktestConfig;
}
@ -16,8 +16,12 @@ export function BacktestConfiguration({
const [formData, setFormData] = useState<BacktestConfig>(
initialConfig || {
name: '',
startDate: new Date(new Date().setMonth(new Date().getMonth() - 6)), // 6 months ago
endDate: new Date(),
startDate: new Date(new Date().setMonth(new Date().getMonth() - 6))
.toISOString()
.slice(0, 10), // 6 months ago
endDate: new Date().toISOString().slice(0, 10),
dataFrequency: '1d',
dataSource: 'auto',
initialCapital: 100000,
symbols: [],
strategy: 'sma-crossover',
@ -27,7 +31,10 @@ export function BacktestConfiguration({
}
);
const [symbolInput, setSymbolInput] = useState('');
const [historicalValid, setHistoricalValid] = useState(false);
const [saving, setSaving] = useState(false);
const [saveError, setSaveError] = useState<string | null>(null);
const locked = disabled || saving;
useEffect(() => {
if (initialConfig) {
@ -35,53 +42,32 @@ export function BacktestConfiguration({
}
}, [initialConfig]);
const handleSubmit = (e: React.FormEvent) => {
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (formData.symbols.length === 0) {
alert('Please add at least one symbol');
if (!historicalValid || locked) {
return;
}
onSubmit(formData);
setSaving(true);
setSaveError(null);
try {
await onSubmit(formData);
} catch (error) {
setSaveError(error instanceof Error ? error.message : 'Could not save configuration');
} finally {
setSaving(false);
}
};
const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
const { name, value, type } = e.target;
setFormData(prev => ({
...prev,
[name]: type === 'number' ? parseFloat(value) : type === 'date' ? new Date(value) : value,
[name]: type === 'number' ? parseFloat(value) : value,
}));
};
const handleAddSymbol = () => {
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(prev => ({
...prev,
symbols: [...prev.symbols, symbol],
}));
setSymbolInput('');
}
};
const handleRemoveSymbol = (symbol: string) => {
setFormData(prev => ({
...prev,
symbols: prev.symbols.filter(s => s !== symbol),
}));
};
const formatDate = (date: Date) => {
return date.toISOString().split('T')[0];
};
return (
<form onSubmit={handleSubmit} className="space-y-4">
<form onSubmit={event => void handleSubmit(event)} className="space-y-4">
<div>
<label className="block text-sm font-medium text-text-secondary mb-1">Backtest Name</label>
<input
@ -91,60 +77,22 @@ export function BacktestConfiguration({
onChange={handleChange}
placeholder="My Strategy Backtest"
className="w-full px-3 py-2 bg-background border border-border rounded-md text-sm text-text-primary focus:outline-none focus:ring-1 focus:ring-primary-500"
disabled={disabled}
disabled={locked}
required
/>
</div>
<div>
<label className="block text-sm font-medium text-text-secondary mb-1">
Historical prices
</label>
<select
value={formData.priceAdjustment ?? 'raw'}
disabled={disabled}
onChange={e =>
setFormData(prev => ({
...prev,
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-1">Start Date</label>
<input
type="date"
name="startDate"
value={formatDate(formData.startDate)}
onChange={handleChange}
className="w-full px-3 py-2 bg-background border border-border rounded-md text-sm text-text-primary focus:outline-none focus:ring-1 focus:ring-primary-500"
disabled={disabled}
required
/>
</div>
<div>
<label className="block text-sm font-medium text-text-secondary mb-1">End Date</label>
<input
type="date"
name="endDate"
value={formatDate(formData.endDate)}
onChange={handleChange}
className="w-full px-3 py-2 bg-background border border-border rounded-md text-sm text-text-primary focus:outline-none focus:ring-1 focus:ring-primary-500"
disabled={disabled}
required
/>
</div>
</div>
<HistoricalDataSelection
value={formData}
onChange={selection => setFormData(previous => ({ ...previous, ...selection }))}
onValidityChange={setHistoricalValid}
disabled={locked}
/>
{saveError && (
<p role="alert" className="text-sm text-error">
{saveError}
</p>
)}
<div>
<label className="block text-sm font-medium text-text-secondary mb-1">
@ -158,52 +106,11 @@ export function BacktestConfiguration({
min="1000"
step="1000"
className="w-full px-3 py-2 bg-background border border-border rounded-md text-sm text-text-primary focus:outline-none focus:ring-1 focus:ring-primary-500"
disabled={disabled}
disabled={locked}
required
/>
</div>
<div>
<label className="block text-sm font-medium text-text-secondary mb-1">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(), handleAddSymbol())}
placeholder="AAPL.US"
className="flex-1 px-3 py-2 bg-background border border-border rounded-md text-sm text-text-primary focus:outline-none focus:ring-1 focus:ring-primary-500"
disabled={disabled}
/>
<button
type="button"
onClick={handleAddSymbol}
className="px-4 py-2 bg-primary-500 text-white rounded-md text-sm font-medium hover:bg-primary-600 transition-colors disabled:opacity-50"
disabled={disabled}
>
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-2 py-1 bg-primary-500/10 text-primary-400 rounded-md text-sm"
>
{symbol}
<button
type="button"
onClick={() => handleRemoveSymbol(symbol)}
className="text-primary-400 hover:text-primary-300"
disabled={disabled}
>
×
</button>
</span>
))}
</div>
</div>
<div>
<label className="block text-sm font-medium text-text-secondary mb-1">Strategy</label>
<select
@ -211,7 +118,7 @@ export function BacktestConfiguration({
value={formData.strategy}
onChange={handleChange}
className="w-full px-3 py-2 bg-background border border-border rounded-md text-sm text-text-primary focus:outline-none focus:ring-1 focus:ring-primary-500"
disabled={disabled}
disabled={locked}
>
<option value="sma-crossover">SMA Crossover</option>
<option value="mean-reversion">Mean Reversion</option>
@ -244,7 +151,7 @@ export function BacktestConfiguration({
min="0"
step="0.01"
className="w-full px-3 py-2 bg-background border border-border rounded-md text-sm text-text-primary focus:outline-none focus:ring-1 focus:ring-primary-500"
disabled={disabled}
disabled={locked}
/>
</div>
@ -260,7 +167,7 @@ export function BacktestConfiguration({
min="0"
step="0.01"
className="w-full px-3 py-2 bg-background border border-border rounded-md text-sm text-text-primary focus:outline-none focus:ring-1 focus:ring-primary-500"
disabled={disabled}
disabled={locked}
/>
</div>
</div>
@ -278,7 +185,7 @@ export function BacktestConfiguration({
max="1000"
step="0.1"
className="w-full px-3 py-2 bg-background border border-border rounded-md text-sm text-text-primary focus:outline-none focus:ring-1 focus:ring-primary-500"
disabled={disabled}
disabled={locked}
/>
<p className="text-xs text-text-muted mt-1">1x = real-time, 10x = 10x faster</p>
</div>
@ -286,7 +193,7 @@ export function BacktestConfiguration({
<button
type="submit"
className="w-full px-4 py-2 bg-primary-500 text-white rounded-md text-sm font-medium hover:bg-primary-600 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
disabled={disabled}
disabled={locked || !historicalValid}
>
Save
</button>