Freeze historical datasets and qualify backtest instrument identities
This commit is contained in:
parent
63c3856797
commit
d6b09eb635
43 changed files with 2249 additions and 1230 deletions
|
|
@ -1,7 +1,9 @@
|
|||
import { EventEmitter } from 'node:events';
|
||||
import { z } from 'zod';
|
||||
import type { Logger } from '@stock-bot/services';
|
||||
import type { BacktestRunResult } from '@stock-bot/types';
|
||||
import type { BacktestRunResult, HistoricalDatasetManifest } from '@stock-bot/types';
|
||||
import { validateHistoricalDataRequest } from '@stock-bot/types';
|
||||
import { dataPolicy, validateDataset } from '../data/historical-dataset';
|
||||
import { BacktestConfigSchema, type BacktestConfig } from '../types';
|
||||
import { BacktestCancelledError, type BacktestExecutionEngine } from './execution-engine';
|
||||
import { normalizeBacktestResult } from './result';
|
||||
|
|
@ -14,6 +16,8 @@ import {
|
|||
|
||||
export function parseBacktestRequest(input: unknown): BacktestConfig {
|
||||
const config = BacktestConfigSchema.parse(input);
|
||||
validateHistoricalDataRequest(config);
|
||||
dataPolicy(config);
|
||||
if (new Date(config.endDate) <= new Date(config.startDate)) {
|
||||
throw new z.ZodError([
|
||||
{ code: 'custom', path: ['endDate'], message: 'Backtest endDate must be after startDate' },
|
||||
|
|
@ -193,7 +197,24 @@ export class BacktestRunner extends EventEmitter {
|
|||
throw new BacktestCancelledError();
|
||||
}
|
||||
this.phase = 'executing';
|
||||
const raw = await this.dependencies.engine.runBacktest(executionConfig);
|
||||
let manifest: HistoricalDatasetManifest | undefined;
|
||||
const claim = this.active;
|
||||
const raw = await this.dependencies.engine.runBacktest(executionConfig, async load => {
|
||||
const saved = await this.dependencies.repository.loadDataset(
|
||||
claim.runId,
|
||||
claim.attemptId,
|
||||
executionConfig.datasetHash
|
||||
);
|
||||
const dataset = validateDataset(saved ?? (await load()), executionConfig);
|
||||
const frozen =
|
||||
saved ??
|
||||
(await this.dependencies.repository.freezeDataset(claim.runId, claim.attemptId, dataset));
|
||||
manifest = frozen.manifest;
|
||||
return frozen;
|
||||
});
|
||||
if (!manifest) {
|
||||
throw new Error('Backtest engine did not resolve frozen execution inputs');
|
||||
}
|
||||
if (this.leaseFailure) {
|
||||
throw this.leaseFailure;
|
||||
}
|
||||
|
|
@ -206,7 +227,8 @@ export class BacktestRunner extends EventEmitter {
|
|||
executionConfig,
|
||||
this.active,
|
||||
performance.now() - started,
|
||||
this.cancelled
|
||||
this.cancelled,
|
||||
manifest
|
||||
);
|
||||
} catch (error) {
|
||||
throw new Error('Backtest engine returned an invalid result', { cause: error });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue