feat(backtests): separate saving from retry-safe run submission
This commit is contained in:
parent
5eb0985057
commit
9b9403e36b
18 changed files with 1200 additions and 226 deletions
|
|
@ -1,14 +1,14 @@
|
|||
import { XMarkIcon } from '@heroicons/react/24/solid';
|
||||
import { useState } from 'react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { parseInstrumentId } from '@stock-bot/types';
|
||||
import type { Backtest } from '../services/backtestApiV2';
|
||||
import { backtestApiV2, type Backtest, type Run } from '../services/backtestApiV2';
|
||||
|
||||
interface CreateBacktestDialogProps {
|
||||
onClose: () => void;
|
||||
onCreate: (config: Omit<Backtest, 'id' | 'createdAt' | 'updatedAt'>) => void;
|
||||
onCreated: (backtest: Backtest, run?: Run) => void;
|
||||
}
|
||||
|
||||
export function CreateBacktestDialog({ onClose, onCreate }: CreateBacktestDialogProps) {
|
||||
export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialogProps) {
|
||||
const [formData, setFormData] = useState({
|
||||
name: '',
|
||||
strategy: 'moving-average',
|
||||
|
|
@ -22,23 +22,91 @@ export function CreateBacktestDialog({ onClose, onCreate }: CreateBacktestDialog
|
|||
});
|
||||
|
||||
const [symbolInput, setSymbolInput] = useState('');
|
||||
const [phase, setPhase] = useState<'idle' | 'saving' | 'submitting'>('idle');
|
||||
const isSubmitting = phase !== 'idle';
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const saved = useRef<Backtest | null>(null);
|
||||
const submission = useRef<{ key: string; body: { speedMultiplier: null } } | null>(null);
|
||||
const pending = useRef(false);
|
||||
const active = useRef(true);
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
useEffect(() => {
|
||||
active.current = true;
|
||||
return () => {
|
||||
active.current = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
if (!formData.name.trim()) {
|
||||
alert('Please enter a backtest name');
|
||||
const close = () => {
|
||||
// A list refresh before this write settles could miss the newly saved row.
|
||||
if (pending.current && !saved.current) {
|
||||
return;
|
||||
}
|
||||
active.current = false;
|
||||
onClose();
|
||||
};
|
||||
|
||||
const handleSubmit = async (runAfterSave: boolean) => {
|
||||
if (pending.current || !active.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
onCreate({
|
||||
...formData,
|
||||
config: {
|
||||
commission: formData.commission,
|
||||
slippage: formData.slippage,
|
||||
dataPolicy: { adjustment: formData.priceAdjustment, timestamp: 'utc-session-date' },
|
||||
},
|
||||
});
|
||||
if (!formData.name.trim()) {
|
||||
setError('Please enter a backtest name');
|
||||
return;
|
||||
}
|
||||
|
||||
pending.current = true;
|
||||
setPhase(saved.current ? 'submitting' : 'saving');
|
||||
setError(null);
|
||||
try {
|
||||
if (!saved.current) {
|
||||
const created = await backtestApiV2.createBacktest({
|
||||
name: formData.name,
|
||||
strategy: formData.strategy,
|
||||
symbols: formData.symbols,
|
||||
startDate: formData.startDate,
|
||||
endDate: formData.endDate,
|
||||
initialCapital: formData.initialCapital,
|
||||
config: {
|
||||
commission: formData.commission,
|
||||
slippage: formData.slippage,
|
||||
dataPolicy: { adjustment: formData.priceAdjustment, timestamp: 'utc-session-date' },
|
||||
},
|
||||
});
|
||||
if (!active.current) {
|
||||
return;
|
||||
}
|
||||
saved.current = created;
|
||||
}
|
||||
let run: Run | undefined;
|
||||
if (runAfterSave) {
|
||||
setPhase('submitting');
|
||||
submission.current ??= { key: crypto.randomUUID(), body: { speedMultiplier: null } };
|
||||
run = await backtestApiV2.createRun(
|
||||
saved.current.id,
|
||||
submission.current.body,
|
||||
submission.current.key
|
||||
);
|
||||
}
|
||||
if (active.current) {
|
||||
onCreated(saved.current, run);
|
||||
}
|
||||
} catch (err) {
|
||||
if (active.current) {
|
||||
const detail = err instanceof Error ? err.message : 'Please try again.';
|
||||
setError(
|
||||
saved.current
|
||||
? `Backtest saved, but run submission failed. Retry submission to recover the same run. ${detail}`
|
||||
: `Failed to save backtest. ${detail}`
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
pending.current = false;
|
||||
if (active.current) {
|
||||
setPhase('idle');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const addSymbol = () => {
|
||||
|
|
@ -68,192 +136,228 @@ export function CreateBacktestDialog({ onClose, onCreate }: CreateBacktestDialog
|
|||
<div className="flex justify-between items-center mb-6">
|
||||
<h2 className="text-lg font-bold text-text-primary">Create New Backtest</h2>
|
||||
<button
|
||||
onClick={onClose}
|
||||
onClick={close}
|
||||
disabled={phase === 'saving'}
|
||||
aria-label="Close backtest dialog"
|
||||
className="p-1 hover:bg-surface-tertiary rounded transition-colors"
|
||||
>
|
||||
<XMarkIcon className="w-5 h-5 text-text-secondary" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-6">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-2">
|
||||
Backtest Name
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={formData.name}
|
||||
onChange={e => setFormData({ ...formData, name: 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"
|
||||
placeholder="My Strategy Backtest"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-2">Strategy</label>
|
||||
<select
|
||||
value={formData.strategy}
|
||||
onChange={e => setFormData({ ...formData, strategy: 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"
|
||||
>
|
||||
<option value="moving-average">Moving Average</option>
|
||||
<option value="momentum">Momentum</option>
|
||||
<option value="mean-reversion">Mean Reversion</option>
|
||||
<option value="pairs-trading">Pairs Trading</option>
|
||||
<option value="custom">Custom</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-2">Symbols</label>
|
||||
<div className="flex gap-2 mb-2">
|
||||
{error && (
|
||||
<p role="alert" className="mb-4 text-sm text-error">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
<form
|
||||
onSubmit={e => {
|
||||
e.preventDefault();
|
||||
void handleSubmit(saved.current !== null);
|
||||
}}
|
||||
className="space-y-6"
|
||||
>
|
||||
<fieldset disabled={isSubmitting || saved.current !== null} className="space-y-6">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-2">
|
||||
Backtest Name
|
||||
</label>
|
||||
<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)"
|
||||
value={formData.name}
|
||||
onChange={e => setFormData({ ...formData, name: 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"
|
||||
placeholder="My Strategy Backtest"
|
||||
required
|
||||
/>
|
||||
<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"
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-2">Strategy</label>
|
||||
<select
|
||||
value={formData.strategy}
|
||||
onChange={e => setFormData({ ...formData, strategy: 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"
|
||||
>
|
||||
Add
|
||||
</button>
|
||||
<option value="moving-average">Moving Average</option>
|
||||
<option value="momentum">Momentum</option>
|
||||
<option value="mean-reversion">Mean Reversion</option>
|
||||
<option value="pairs-trading">Pairs Trading</option>
|
||||
<option value="custom">Custom</option>
|
||||
</select>
|
||||
</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"
|
||||
|
||||
<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"
|
||||
>
|
||||
{symbol}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => removeSymbol(symbol)}
|
||||
className="hover:text-error 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"
|
||||
>
|
||||
<XMarkIcon className="w-4 h-4" />
|
||||
</button>
|
||||
</span>
|
||||
))}
|
||||
{symbol}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => removeSymbol(symbol)}
|
||||
className="hover:text-error transition-colors"
|
||||
>
|
||||
<XMarkIcon className="w-4 h-4" />
|
||||
</button>
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
</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
|
||||
Historical prices
|
||||
</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
|
||||
/>
|
||||
<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>
|
||||
<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 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>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-2">
|
||||
Initial Capital
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
value={formData.initialCapital}
|
||||
onChange={e =>
|
||||
setFormData({ ...formData, initialCapital: parseFloat(e.target.value) || 0 })
|
||||
}
|
||||
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"
|
||||
min="0"
|
||||
step="1000"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-2">
|
||||
Commission (per trade)
|
||||
Initial Capital
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
value={formData.commission}
|
||||
value={formData.initialCapital}
|
||||
onChange={e =>
|
||||
setFormData({ ...formData, commission: parseFloat(e.target.value) || 0 })
|
||||
setFormData({ ...formData, initialCapital: parseFloat(e.target.value) || 0 })
|
||||
}
|
||||
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"
|
||||
min="0"
|
||||
step="0.01"
|
||||
step="1000"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-2">
|
||||
Slippage (%)
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
value={formData.slippage}
|
||||
onChange={e =>
|
||||
setFormData({ ...formData, slippage: parseFloat(e.target.value) || 0 })
|
||||
}
|
||||
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"
|
||||
min="0"
|
||||
step="0.01"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-2">
|
||||
Commission (per trade)
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
value={formData.commission}
|
||||
onChange={e =>
|
||||
setFormData({ ...formData, commission: parseFloat(e.target.value) || 0 })
|
||||
}
|
||||
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"
|
||||
min="0"
|
||||
step="0.01"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-2">
|
||||
Slippage (%)
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
value={formData.slippage}
|
||||
onChange={e =>
|
||||
setFormData({ ...formData, slippage: parseFloat(e.target.value) || 0 })
|
||||
}
|
||||
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"
|
||||
min="0"
|
||||
step="0.01"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
<div className="flex justify-end gap-3 pt-4">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
onClick={close}
|
||||
disabled={phase === 'saving'}
|
||||
className="px-4 py-2 border border-border text-text-secondary rounded-md text-sm font-medium hover:bg-surface-tertiary transition-colors"
|
||||
>
|
||||
Cancel
|
||||
Close
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isSubmitting}
|
||||
className="px-4 py-2 bg-primary-500 text-white rounded-md text-sm font-medium hover:bg-primary-600 transition-colors"
|
||||
>
|
||||
Create Backtest
|
||||
{isSubmitting
|
||||
? 'Saving / submitting...'
|
||||
: saved.current
|
||||
? 'Retry submission'
|
||||
: 'Save'}
|
||||
</button>
|
||||
{!saved.current && (
|
||||
<button
|
||||
type="button"
|
||||
disabled={isSubmitting}
|
||||
onClick={e => {
|
||||
if (e.currentTarget.form?.reportValidity()) {
|
||||
void handleSubmit(true);
|
||||
}
|
||||
}}
|
||||
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"
|
||||
>
|
||||
Save and run
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ interface RunControlsCompactProps {
|
|||
cancelRequested: boolean;
|
||||
onCancel: () => void;
|
||||
onRerun: (speed: number | null) => void;
|
||||
isSubmitting?: boolean;
|
||||
hasPendingSubmission?: boolean;
|
||||
}
|
||||
|
||||
export function RunControlsCompact({
|
||||
|
|
@ -13,6 +15,8 @@ export function RunControlsCompact({
|
|||
cancelRequested,
|
||||
onCancel,
|
||||
onRerun,
|
||||
isSubmitting = false,
|
||||
hasPendingSubmission = false,
|
||||
}: RunControlsCompactProps) {
|
||||
const active = ['pending', 'running', 'paused'].includes(run.status);
|
||||
const requested = active && (cancelRequested || run.cancelRequested);
|
||||
|
|
@ -38,9 +42,10 @@ export function RunControlsCompact({
|
|||
)}
|
||||
<button
|
||||
onClick={() => onRerun(null)}
|
||||
className="p-1 rounded hover:bg-surface-tertiary transition-colors"
|
||||
title="Rerun"
|
||||
aria-label="Rerun"
|
||||
disabled={isSubmitting}
|
||||
className="p-1 rounded hover:bg-surface-tertiary transition-colors disabled:opacity-50"
|
||||
title={hasPendingSubmission ? 'Retry submission' : 'Run current settings'}
|
||||
aria-label={hasPendingSubmission ? 'Retry submission' : 'Run current settings'}
|
||||
>
|
||||
<ArrowPathIcon className="w-4 h-4 text-text-primary" />
|
||||
</button>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue