Freeze historical datasets and qualify backtest instrument identities

This commit is contained in:
Boki 2026-10-20 23:59:15 -04:00
parent 63c3856797
commit d6b09eb635
43 changed files with 2249 additions and 1230 deletions

View file

@ -1,4 +1,5 @@
import { EventEmitter } from 'node:events';
import { resolveDataset, type DatasetResolver } from '../data/historical-dataset';
import { StorageService } from '../services/StorageService';
import { BacktestConfigSchema, type BacktestConfig, type OrchestratorServices } from '../types';
import {
@ -47,7 +48,10 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
);
}
async runBacktest(input: BacktestConfig): Promise<NativeBacktestResult> {
async runBacktest(
input: BacktestConfig,
dataset?: DatasetResolver
): Promise<NativeBacktestResult> {
if (this.closing || this.active) {
throw new Error(
this.closing ? 'Native backtest engine is shutting down' : 'Backtest already running'
@ -79,7 +83,7 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
resolveNativeStrategies(config);
// Race the read too: a slow database must not prevent cancellation. A late read
// cannot spawn a worker after the run has been stopped.
const data = await Promise.race([this.loadData(config, signal), aborted]);
const data = await Promise.race([this.loadData(config, signal, dataset), aborted]);
signal.throwIfAborted();
const raw = await this.worker.execute({ type: 'run', config, data }, signal, progress => {
this.progress = progress.progress;
@ -108,36 +112,13 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
private async loadData(
config: BacktestConfig,
signal: AbortSignal
signal: AbortSignal,
resolver?: DatasetResolver
): Promise<NativeWorkerRequest['data']> {
const data: NativeWorkerRequest['data'] = [];
for (const symbol of config.symbols) {
signal.throwIfAborted();
const bars = await this.storageService.getHistoricalBars(
symbol,
new Date(config.startDate),
new Date(config.endDate),
config.dataFrequency
);
signal.throwIfAborted();
if (!bars.length) {
throw new Error(`No historical data for ${symbol}`);
}
for (const bar of bars) {
data.push({
symbol,
timestamp: bar.timestamp.getTime(),
type: 'bar',
open: bar.open,
high: bar.high,
low: bar.low,
close: bar.close,
volume: bar.volume,
vwap: bar.vwap ?? (bar.high + bar.low + bar.close) / 3,
});
}
}
return data;
signal.throwIfAborted();
const dataset = await resolveDataset(this.storageService, config, resolver);
signal.throwIfAborted();
return dataset.bars.map(bar => ({ ...bar, type: 'bar' }));
}
private publish(event: string, data: unknown): void {