feat(research): add durable walk-forward validation workflows
This commit is contained in:
parent
abd1c5b83d
commit
3ed7b98f7c
45 changed files with 5188 additions and 199 deletions
|
|
@ -0,0 +1,103 @@
|
|||
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<ValidationPhase, Record<(typeof fields)[number], string>>;
|
||||
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 (
|
||||
<div className="grid gap-3 lg:grid-cols-3">
|
||||
{phases.map(phase => (
|
||||
<fieldset
|
||||
key={phase}
|
||||
disabled={disabled}
|
||||
className="space-y-2 rounded border border-border p-3"
|
||||
>
|
||||
<legend className="px-1 font-medium">
|
||||
{prefix}
|
||||
{phaseLabels[phase]} window
|
||||
</legend>
|
||||
{fields.map(field => (
|
||||
<label key={field} className="block text-xs">
|
||||
{prefix}
|
||||
{phaseLabels[phase]} {labels[field]} (UTC)
|
||||
<input
|
||||
className="mt-1 w-full rounded-md border border-border bg-background px-3 py-2 text-text-primary disabled:opacity-60"
|
||||
type="text"
|
||||
value={windows[phase][field]}
|
||||
placeholder="2024-01-01T09:00:00Z"
|
||||
onChange={event => onChange(phase, field, event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
))}
|
||||
</fieldset>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue