import type { ChronologicalValidationInput, ValidationPhase, } from '@stock-bot/stock-config/chronological-validation'; const phases = ['train', 'validation', 'test'] as const; const fields = ['warmupStart', 'tradeStart', 'tradeEnd'] as const; const labels = { warmupStart: 'warmup start', tradeStart: 'trading start', tradeEnd: 'trading end', }; const phaseLabels = { train: 'Train', validation: 'Validation', test: 'Test' }; export type ValidationWindows = Record>; export const emptyValidationWindows = (): ValidationWindows => ({ train: { warmupStart: '', tradeStart: '', tradeEnd: '' }, validation: { warmupStart: '', tradeStart: '', tradeEnd: '' }, test: { warmupStart: '', tradeStart: '', tradeEnd: '' }, }); export const validationWindowsDirty = (windows: ValidationWindows) => phases.some(phase => fields.some(field => windows[phase][field] !== '')); function parseInstant(raw: string, label: string) { const text = raw.trim(), match = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,3}))?Z$/.exec(text), value = Date.parse(text); if ( !match || !Number.isFinite(value) || new Date(value).toISOString() !== `${match[1]}.${(match[2] ?? '').padEnd(3, '0')}Z` ) { throw new Error( `${label} must be a real UTC instant ending in Z, for example 2024-01-01T09:00:00Z.` ); } return value; } export function parseValidationWindows( windows: ValidationWindows, prefix = '' ): ChronologicalValidationInput['recipe'] { const bounds = (phase: ValidationPhase) => ({ warmupStart: parseInstant( windows[phase].warmupStart, `${prefix}${phaseLabels[phase]} warmup start (UTC)` ), tradeStart: parseInstant( windows[phase].tradeStart, `${prefix}${phaseLabels[phase]} trading start (UTC)` ), tradeEnd: parseInstant( windows[phase].tradeEnd, `${prefix}${phaseLabels[phase]} trading end (UTC)` ), }); return { version: 1, train: bounds('train'), validation: bounds('validation'), test: bounds('test'), labelHorizonMs: 0, }; } export function ValidationWindowFields({ windows, disabled, prefix = '', onChange, }: { windows: ValidationWindows; disabled: boolean; prefix?: string; onChange: (phase: ValidationPhase, field: (typeof fields)[number], value: string) => void; }) { return (
{phases.map(phase => (
{prefix} {phaseLabels[phase]} window {fields.map(field => ( ))}
))}
); }