stock-bot/apps/stock/orchestrator/src/backtest/execution-engine.ts

53 lines
1.9 KiB
TypeScript

import type { EventEmitter } from 'node:events';
import type {
CanonicalRunRequest,
CanonicalRunResult,
} from '@stock-bot/stock-config/canonical-research';
import type { HistoricalDatasetV2 } from '@stock-bot/types';
import type { DatasetResolver } from '../data/historical-dataset';
import type { BacktestConfig } from '../types';
import type { NativeBacktestResult } from './native-result';
import type { BacktestResult } from './typescript-result';
/** The runner depends on execution capabilities, not backend implementations. */
export interface BacktestExecutionEngine extends Pick<EventEmitter, 'on' | 'off'> {
runBacktest(
config: BacktestConfig,
dataset?: DatasetResolver
): Promise<BacktestResult | NativeBacktestResult>;
runCanonical?(
request: CanonicalRunRequest,
resolveDataset: (signal: AbortSignal) => Promise<HistoricalDatasetV2>,
controls?: { executionDeadlineAt?: number }
): Promise<Pick<CanonicalRunResult, 'financial' | 'analytics'>>;
stopBacktest(): Promise<void>;
pauseBacktest(): Promise<void>;
resumeBacktest(): Promise<void>;
setSpeedMultiplier(speed: number | null): void;
getStatus(): {
isRunning: boolean;
isPaused: boolean;
progress: number;
currentTime: number;
};
shutdown(): Promise<void>;
}
/** Cancellation has no result when a native worker is forcibly stopped. */
export class BacktestCancelledError extends Error {
constructor() {
super('Backtest cancelled');
this.name = 'BacktestCancelledError';
}
}
export class BacktestTimeoutError extends Error {
constructor(timeoutMs: number) {
super(`Native backtest exceeded its ${timeoutMs}ms execution deadline`);
this.name = 'BacktestTimeoutError';
}
}
/** Old requests remain readable, but must be explicitly reconfigured for canonical admission. */
export const LEGACY_EXECUTION_RETIRED =
'Legacy backtest execution is retired. Select an approved strategy version and retained canonical dataset.';