feat(research): add strategy catalog and versioned backtest setup
This commit is contained in:
parent
8cca0ee588
commit
d929db707a
44 changed files with 4198 additions and 209 deletions
|
|
@ -1,4 +1,8 @@
|
|||
import { useEffect, useState } from 'react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import {
|
||||
LegacyStrategySelect,
|
||||
StrategySelection,
|
||||
} from '@/features/strategies/components/StrategySelection';
|
||||
import type { BacktestConfig } from '../types';
|
||||
import { HistoricalDataSelection } from './HistoricalDataSelection';
|
||||
|
||||
|
|
@ -31,20 +35,28 @@ export function BacktestConfiguration({
|
|||
}
|
||||
);
|
||||
|
||||
const catalog = formData.strategy === 'catalog' || Boolean(formData.strategyVersionId);
|
||||
const [strategyValid, setStrategyValid] = useState(false);
|
||||
const [selectionEpoch, setSelectionEpoch] = useState(0);
|
||||
const authoritativeConfig = useRef(initialConfig);
|
||||
const [historicalValid, setHistoricalValid] = useState(false);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [saveError, setSaveError] = useState<string | null>(null);
|
||||
const locked = disabled || saving;
|
||||
|
||||
useEffect(() => {
|
||||
if (initialConfig) {
|
||||
if (initialConfig && initialConfig !== authoritativeConfig.current) {
|
||||
authoritativeConfig.current = initialConfig;
|
||||
setFormData(initialConfig);
|
||||
setStrategyValid(false);
|
||||
setSaveError(null);
|
||||
setSelectionEpoch(value => value + 1);
|
||||
}
|
||||
}, [initialConfig]);
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!historicalValid || locked) {
|
||||
if (!historicalValid || (catalog && !strategyValid) || locked) {
|
||||
return;
|
||||
}
|
||||
setSaving(true);
|
||||
|
|
@ -111,30 +123,26 @@ export function BacktestConfiguration({
|
|||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-1">Strategy</label>
|
||||
<select
|
||||
name="strategy"
|
||||
{catalog ? (
|
||||
<StrategySelection
|
||||
key={selectionEpoch}
|
||||
allowArchivedSelection
|
||||
value={formData}
|
||||
onChange={selection => setFormData(previous => ({ ...previous, ...selection }))}
|
||||
onValidityChange={setStrategyValid}
|
||||
disabled={Boolean(locked)}
|
||||
/>
|
||||
) : (
|
||||
<LegacyStrategySelect
|
||||
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={locked}
|
||||
>
|
||||
<option value="sma-crossover">SMA Crossover</option>
|
||||
<option value="mean-reversion">Mean Reversion</option>
|
||||
<option value="ml-enhanced">ML Enhanced (Advanced)</option>
|
||||
<option value="momentum">Momentum (Coming Soon)</option>
|
||||
<option value="pairs-trading">Pairs Trading (Coming Soon)</option>
|
||||
</select>
|
||||
<p className="text-xs text-text-muted mt-1">
|
||||
{formData.strategy === 'sma-crossover' && 'Trades on 10/20 day moving average crossovers'}
|
||||
{formData.strategy === 'mean-reversion' &&
|
||||
'Trades when price deviates from mean by 2+ std devs'}
|
||||
{formData.strategy === 'ml-enhanced' && 'Uses machine learning for signal generation'}
|
||||
{formData.strategy === 'momentum' && 'Follows strong price trends'}
|
||||
{formData.strategy === 'pairs-trading' && 'Trades correlated asset pairs'}
|
||||
</p>
|
||||
</div>
|
||||
onChange={strategy => setFormData(previous => ({ ...previous, strategy }))}
|
||||
disabled={Boolean(locked)}
|
||||
/>
|
||||
)}
|
||||
<p className="text-xs text-text-muted">
|
||||
This saved template keeps its strategy mode. Create another backtest to switch between
|
||||
catalog and legacy modes.
|
||||
</p>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
|
|
@ -172,28 +180,30 @@ export function BacktestConfiguration({
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-1">
|
||||
Speed Multiplier
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
name="speedMultiplier"
|
||||
value={formData.speedMultiplier}
|
||||
onChange={handleChange}
|
||||
min="0.1"
|
||||
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={locked}
|
||||
/>
|
||||
<p className="text-xs text-text-muted mt-1">1x = real-time, 10x = 10x faster</p>
|
||||
</div>
|
||||
{!catalog && (
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-1">
|
||||
Speed Multiplier
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
name="speedMultiplier"
|
||||
value={formData.speedMultiplier}
|
||||
onChange={handleChange}
|
||||
min="0.1"
|
||||
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={locked}
|
||||
/>
|
||||
<p className="text-xs text-text-muted mt-1">1x = real-time, 10x = 10x faster</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<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={locked || !historicalValid}
|
||||
disabled={locked || !historicalValid || (catalog && !strategyValid)}
|
||||
>
|
||||
Save
|
||||
</button>
|
||||
|
|
|
|||
|
|
@ -1,15 +1,29 @@
|
|||
import { XMarkIcon } from '@heroicons/react/24/solid';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import {
|
||||
LegacyStrategySelect,
|
||||
StrategySelection,
|
||||
type CatalogSelection,
|
||||
} from '@/features/strategies/components/StrategySelection';
|
||||
import {
|
||||
confirmDiscard,
|
||||
useUnsavedWarning,
|
||||
} from '@/features/strategies/components/useUnsavedWarning';
|
||||
import { backtestApiV2, type Backtest, type Run } from '../services/backtestApiV2';
|
||||
import type { HistoricalSelection } from '../services/historical-selection';
|
||||
import { HistoricalDataSelection } from './HistoricalDataSelection';
|
||||
|
||||
interface CreateBacktestDialogProps {
|
||||
initialSelection?: CatalogSelection;
|
||||
onClose: () => void;
|
||||
onCreated: (backtest: Backtest, run?: Run) => void;
|
||||
}
|
||||
|
||||
export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialogProps) {
|
||||
export function CreateBacktestDialog({
|
||||
onClose,
|
||||
onCreated,
|
||||
initialSelection,
|
||||
}: CreateBacktestDialogProps) {
|
||||
const [formData, setFormData] = useState<
|
||||
HistoricalSelection & {
|
||||
name: string;
|
||||
|
|
@ -32,6 +46,11 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
|
|||
dataSource: 'auto',
|
||||
});
|
||||
|
||||
const [catalogMode, setCatalogMode] = useState(Boolean(initialSelection?.strategyVersionId));
|
||||
const [selection, setSelection] = useState<CatalogSelection>(initialSelection ?? {});
|
||||
const [strategyValid, setStrategyValid] = useState(false);
|
||||
const [edited, setEdited] = useState(false);
|
||||
useUnsavedWarning(edited);
|
||||
const [historicalValid, setHistoricalValid] = useState(true);
|
||||
const [phase, setPhase] = useState<'idle' | 'saving' | 'submitting'>('idle');
|
||||
const isSubmitting = phase !== 'idle';
|
||||
|
|
@ -53,12 +72,20 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
|
|||
if (pending.current && !saved.current) {
|
||||
return;
|
||||
}
|
||||
if (!saved.current && !confirmDiscard(edited)) {
|
||||
return;
|
||||
}
|
||||
active.current = false;
|
||||
onClose();
|
||||
};
|
||||
|
||||
const handleSubmit = async (runAfterSave: boolean) => {
|
||||
if (pending.current || !active.current || (!saved.current && !historicalValid)) {
|
||||
if (
|
||||
pending.current ||
|
||||
!active.current ||
|
||||
(!saved.current && (!historicalValid || (catalogMode && !strategyValid))) ||
|
||||
(runAfterSave && catalogMode)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -74,7 +101,8 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
|
|||
if (!saved.current) {
|
||||
const created = await backtestApiV2.createBacktest({
|
||||
name: formData.name,
|
||||
strategy: formData.strategy,
|
||||
strategy: catalogMode ? 'catalog' : formData.strategy,
|
||||
...(catalogMode ? selection : {}),
|
||||
symbols: formData.symbols,
|
||||
startDate: formData.startDate,
|
||||
endDate: formData.endDate,
|
||||
|
|
@ -152,6 +180,7 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
|
|||
void handleSubmit(saved.current !== null);
|
||||
}}
|
||||
className="space-y-6"
|
||||
onChange={() => setEdited(true)}
|
||||
>
|
||||
<fieldset disabled={isSubmitting || saved.current !== null} className="space-y-6">
|
||||
<div>
|
||||
|
|
@ -168,20 +197,35 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
|
|||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-secondary mb-2">Strategy</label>
|
||||
<label className="block text-sm text-text-secondary">
|
||||
Strategy mode
|
||||
<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"
|
||||
aria-label="Strategy mode"
|
||||
value={catalogMode ? 'catalog' : 'legacy'}
|
||||
onChange={event => {
|
||||
if (confirmDiscard(Boolean(selection.strategyVersionId))) {
|
||||
setCatalogMode(event.target.value === 'catalog');
|
||||
}
|
||||
}}
|
||||
className="w-full px-3 py-2 bg-background border border-border rounded-md"
|
||||
>
|
||||
<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>
|
||||
<option value="legacy">Legacy strategy</option>
|
||||
<option value="catalog">Catalog version (save only)</option>
|
||||
</select>
|
||||
</div>
|
||||
</label>
|
||||
{catalogMode ? (
|
||||
<StrategySelection
|
||||
value={selection}
|
||||
onChange={setSelection}
|
||||
onValidityChange={setStrategyValid}
|
||||
disabled={isSubmitting || saved.current !== null}
|
||||
/>
|
||||
) : (
|
||||
<LegacyStrategySelect
|
||||
value={formData.strategy}
|
||||
onChange={strategy => setFormData(previous => ({ ...previous, strategy }))}
|
||||
/>
|
||||
)}
|
||||
|
||||
<HistoricalDataSelection
|
||||
value={formData}
|
||||
|
|
@ -251,7 +295,10 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
|
|||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isSubmitting || (!saved.current && !historicalValid)}
|
||||
disabled={
|
||||
isSubmitting ||
|
||||
(!saved.current && (!historicalValid || (catalogMode && !strategyValid)))
|
||||
}
|
||||
className="px-4 py-2 bg-primary-500 text-white rounded-md text-sm font-medium hover:bg-primary-600 transition-colors"
|
||||
>
|
||||
{isSubmitting
|
||||
|
|
@ -263,7 +310,7 @@ export function CreateBacktestDialog({ onClose, onCreated }: CreateBacktestDialo
|
|||
{!saved.current && (
|
||||
<button
|
||||
type="button"
|
||||
disabled={isSubmitting || (!saved.current && !historicalValid)}
|
||||
disabled={isSubmitting || catalogMode || (!saved.current && !historicalValid)}
|
||||
onClick={e => {
|
||||
if (e.currentTarget.form?.reportValidity()) {
|
||||
void handleSubmit(true);
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ interface RunControlsCompactProps {
|
|||
isSubmitting?: boolean;
|
||||
hasPendingSubmission?: boolean;
|
||||
archived?: boolean;
|
||||
executionUnavailable?: boolean;
|
||||
}
|
||||
|
||||
export function RunControlsCompact({
|
||||
|
|
@ -19,6 +20,7 @@ export function RunControlsCompact({
|
|||
isSubmitting = false,
|
||||
hasPendingSubmission = false,
|
||||
archived = false,
|
||||
executionUnavailable = false,
|
||||
}: RunControlsCompactProps) {
|
||||
const active = ['pending', 'running', 'paused'].includes(run.status);
|
||||
const requested = active && (cancelRequested || run.cancelRequested);
|
||||
|
|
@ -44,7 +46,7 @@ export function RunControlsCompact({
|
|||
)}
|
||||
<button
|
||||
onClick={() => onRerun(null)}
|
||||
disabled={isSubmitting || (archived && !hasPendingSubmission)}
|
||||
disabled={isSubmitting || ((archived || executionUnavailable) && !hasPendingSubmission)}
|
||||
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'}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue