212 lines
7.1 KiB
TypeScript
212 lines
7.1 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
|
import {
|
|
LegacyStrategySelect,
|
|
StrategySelection,
|
|
} from '@/features/strategies/components/StrategySelection';
|
|
import type { BacktestConfig } from '../types';
|
|
import { HistoricalDataSelection } from './HistoricalDataSelection';
|
|
|
|
interface BacktestConfigurationProps {
|
|
onSubmit: (config: BacktestConfig) => void | Promise<void>;
|
|
disabled?: boolean;
|
|
initialConfig?: BacktestConfig;
|
|
}
|
|
|
|
export function BacktestConfiguration({
|
|
onSubmit,
|
|
disabled,
|
|
initialConfig,
|
|
}: BacktestConfigurationProps) {
|
|
const [formData, setFormData] = useState<BacktestConfig>(
|
|
initialConfig || {
|
|
name: '',
|
|
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',
|
|
speedMultiplier: 1,
|
|
commission: 0.001, // 0.1%
|
|
slippage: 0.0005, // 0.05%
|
|
}
|
|
);
|
|
|
|
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 && 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 || (catalog && !strategyValid) || locked) {
|
|
return;
|
|
}
|
|
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) : value,
|
|
}));
|
|
};
|
|
|
|
return (
|
|
<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
|
|
type="text"
|
|
name="name"
|
|
value={formData.name}
|
|
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={locked}
|
|
required
|
|
/>
|
|
</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">
|
|
Initial Capital ($)
|
|
</label>
|
|
<input
|
|
type="number"
|
|
name="initialCapital"
|
|
value={formData.initialCapital}
|
|
onChange={handleChange}
|
|
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={locked}
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
{catalog ? (
|
|
<StrategySelection
|
|
key={selectionEpoch}
|
|
allowArchivedSelection
|
|
value={formData}
|
|
onChange={selection => setFormData(previous => ({ ...previous, ...selection }))}
|
|
onValidityChange={setStrategyValid}
|
|
disabled={Boolean(locked)}
|
|
/>
|
|
) : (
|
|
<LegacyStrategySelect
|
|
value={formData.strategy}
|
|
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>
|
|
<label className="block text-sm font-medium text-text-secondary mb-1">
|
|
Commission (%)
|
|
</label>
|
|
<input
|
|
type="number"
|
|
name="commission"
|
|
value={formData.commission * 100}
|
|
onChange={e =>
|
|
setFormData(prev => ({ ...prev, commission: parseFloat(e.target.value) / 100 }))
|
|
}
|
|
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={locked}
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium text-text-secondary mb-1">Slippage (%)</label>
|
|
<input
|
|
type="number"
|
|
name="slippage"
|
|
value={formData.slippage * 100}
|
|
onChange={e =>
|
|
setFormData(prev => ({ ...prev, slippage: parseFloat(e.target.value) / 100 }))
|
|
}
|
|
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={locked}
|
|
/>
|
|
</div>
|
|
</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 || (catalog && !strategyValid)}
|
|
>
|
|
Save
|
|
</button>
|
|
</form>
|
|
);
|
|
}
|