116 lines
3.7 KiB
TypeScript
116 lines
3.7 KiB
TypeScript
/** A bounded collection of existing chronological validations, not a new execution engine. */
|
|
import { z } from 'zod';
|
|
import type { ResearchActor } from '@stock-bot/types';
|
|
import {
|
|
chronologicalValidationInputSchema,
|
|
type ChronologicalValidationDetail,
|
|
type ChronologicalValidationPreview,
|
|
type ValidationActionCapability,
|
|
type ValidationResultEvidence,
|
|
} from './chronological-validation';
|
|
import {
|
|
experimentInputSchema,
|
|
MAX_EXPERIMENT_TRIALS,
|
|
type ExperimentPreview,
|
|
} from './experiments';
|
|
|
|
export const MAX_WALK_FORWARD_WINDOWS = 10;
|
|
/** Includes training and both future holdouts for every window. */
|
|
export const MAX_WALK_FORWARD_RUNS = MAX_EXPERIMENT_TRIALS;
|
|
export const walkForwardInputSchema = experimentInputSchema.extend({
|
|
windows: z
|
|
.array(chronologicalValidationInputSchema.shape.recipe)
|
|
.min(2)
|
|
.max(MAX_WALK_FORWARD_WINDOWS),
|
|
});
|
|
const hash = z.string().regex(/^[a-f0-9]{64}$/);
|
|
export const walkForwardSubmissionSchema = walkForwardInputSchema.extend({ previewHash: hash });
|
|
export const walkForwardFinalizationSchema = z.object({ previewHash: hash }).strict();
|
|
export type WalkForwardInput = z.infer<typeof walkForwardInputSchema>;
|
|
export type WalkForwardSubmission = z.infer<typeof walkForwardSubmissionSchema>;
|
|
export type WalkForwardFinalizationInput = z.infer<typeof walkForwardFinalizationSchema>;
|
|
|
|
export const walkForwardPolicy = {
|
|
selection: 'independent-training-winner-per-window',
|
|
training: 'nondecreasing-start-strictly-increasing-cutoff',
|
|
holdouts: 'previous-test-end-at-or-before-next-validation-start',
|
|
reset: 'initial-capital-empty-positions',
|
|
reporting: 'ordered-independent-reset-segments',
|
|
} as const;
|
|
export interface WalkForwardPreview {
|
|
version: 'walk-forward-preview/v1';
|
|
execution: 'preview-only';
|
|
previewHash: string;
|
|
input: WalkForwardInput;
|
|
source: ExperimentPreview;
|
|
policy: typeof walkForwardPolicy;
|
|
windows: { index: number; preview: ChronologicalValidationPreview }[];
|
|
counts: {
|
|
windows: number;
|
|
trainingRuns: number;
|
|
holdoutRuns: number;
|
|
totalRuns: number;
|
|
maxRuns: number;
|
|
};
|
|
}
|
|
export interface WalkForwardSegment {
|
|
index: number;
|
|
validationId: string;
|
|
previewHash: string;
|
|
selectionHash: string;
|
|
reportId: string;
|
|
reportHash: string;
|
|
/** Copied diagnostic and test results, never a compounded portfolio. */
|
|
validation: ValidationResultEvidence;
|
|
test: ValidationResultEvidence;
|
|
}
|
|
export interface WalkForwardReport {
|
|
version: 'walk-forward-report/v1';
|
|
id: string;
|
|
walkForwardId: string;
|
|
previewHash: string;
|
|
reportHash: string;
|
|
createdAt: string;
|
|
principal: ResearchActor;
|
|
reset: 'initial-capital-empty-positions';
|
|
/** Every frozen window in ascending index; absent evidence cannot be omitted. */
|
|
segments: WalkForwardSegment[];
|
|
limitations: string[];
|
|
}
|
|
export interface WalkForwardSummary {
|
|
id: string;
|
|
backtestId: string;
|
|
name: string;
|
|
previewHash: string;
|
|
createdAt: string;
|
|
cancelRequested: boolean;
|
|
windowCount: number;
|
|
reportedWindowCount: number;
|
|
trainingRunCount: number;
|
|
plannedRunCount: number;
|
|
stage: 'windows' | 'reported';
|
|
}
|
|
export interface WalkForwardDetail extends WalkForwardSummary {
|
|
preview: WalkForwardPreview;
|
|
windows: {
|
|
index: number;
|
|
validationId: string;
|
|
status: 'admitted' | 'missing';
|
|
validation: ChronologicalValidationDetail | null;
|
|
}[];
|
|
report: WalkForwardReport | null;
|
|
actions: { finalize: ValidationActionCapability };
|
|
}
|
|
export interface WalkForwardReceipt {
|
|
id: string;
|
|
walkForwardId: string;
|
|
operation: 'finalize-walk-forward-report';
|
|
previewHash: string;
|
|
reportHash: string;
|
|
createdAt: string;
|
|
principal: ResearchActor;
|
|
}
|
|
export interface WalkForwardActionResult {
|
|
receipt: WalkForwardReceipt;
|
|
walkForward: WalkForwardDetail;
|
|
}
|