feat(research): run retained strategies through durable canonical engine
This commit is contained in:
parent
d929db707a
commit
618fb9e33c
58 changed files with 6596 additions and 779 deletions
|
|
@ -1,6 +1,12 @@
|
|||
import { EventEmitter } from 'node:events';
|
||||
import type { Logger } from '@stock-bot/services';
|
||||
import { parseBacktestRequest } from '@stock-bot/stock-config/backtest';
|
||||
import {
|
||||
canonicalRunRequestSchema,
|
||||
type CanonicalRunRequest,
|
||||
type CanonicalRunResult,
|
||||
} from '@stock-bot/stock-config/canonical-research';
|
||||
import type { HistoricalDatasetV2 } from '@stock-bot/types';
|
||||
import { resolveDataset, type DatasetResolver } from '../data/historical-dataset';
|
||||
import { PREPARATION_BATCH_SIZE, preparationCheckpoint } from '../data/preparation';
|
||||
import type { StorageService } from '../services/StorageService';
|
||||
|
|
@ -12,7 +18,7 @@ import {
|
|||
} from './execution-engine';
|
||||
import { nativeResultSchema, type NativeBacktestResult } from './native-result';
|
||||
import { resolveNativeStrategies } from './native-strategies';
|
||||
import type { NativeWorkerRequest } from './native-worker-protocol';
|
||||
import { parseRetainedStrategyResult, type NativeWorkerRequest } from './native-worker-protocol';
|
||||
import { NativeBacktestWorker } from './NativeBacktestWorker';
|
||||
|
||||
export type BacktestResult = NativeBacktestResult;
|
||||
|
|
@ -49,12 +55,56 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
|
|||
input: BacktestConfig,
|
||||
dataset?: DatasetResolver
|
||||
): Promise<NativeBacktestResult> {
|
||||
const config = parseBacktestRequest(input);
|
||||
return this.runOwned(Date.parse(config.startDate), async (signal, aborted) => {
|
||||
resolveNativeStrategies(config);
|
||||
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;
|
||||
this.publish('progress', progress);
|
||||
});
|
||||
const result = nativeResultSchema.parse(JSON.parse(raw));
|
||||
this.currentTime = Date.parse(config.endDate);
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
async runCanonical(
|
||||
input: CanonicalRunRequest,
|
||||
resolveDataset: (signal: AbortSignal) => Promise<HistoricalDatasetV2>
|
||||
): Promise<Pick<CanonicalRunResult, 'financial' | 'analytics'>> {
|
||||
const request = canonicalRunRequestSchema.parse(input);
|
||||
return this.runOwned(
|
||||
request.policy.matching.risk.ledger.riskSession.startsAt,
|
||||
async (signal, aborted) => {
|
||||
const dataset = await Promise.race([resolveDataset(signal), aborted]);
|
||||
signal.throwIfAborted();
|
||||
const raw = await this.worker.execute(
|
||||
{ type: 'runRetainedStrategy', request, dataset },
|
||||
signal,
|
||||
progress => {
|
||||
this.progress = progress.progress;
|
||||
this.publish('progress', progress);
|
||||
}
|
||||
);
|
||||
const result = parseRetainedStrategyResult(raw, request);
|
||||
this.currentTime = dataset.manifest.bounds.tradeEnd;
|
||||
return { financial: result.financial, analytics: result.analytics };
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/** One ownership/deadline/cancellation lifecycle for legacy and canonical work. */
|
||||
private async runOwned<Result>(
|
||||
initialTime: number,
|
||||
work: (signal: AbortSignal, aborted: Promise<never>) => Promise<Result>
|
||||
): Promise<Result> {
|
||||
if (this.closing || this.active) {
|
||||
throw new Error(
|
||||
this.closing ? 'Native backtest engine is shutting down' : 'Backtest already running'
|
||||
);
|
||||
}
|
||||
const config = parseBacktestRequest(input);
|
||||
const controller = new AbortController();
|
||||
let finished!: () => void;
|
||||
this.active = {
|
||||
|
|
@ -64,7 +114,7 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
|
|||
}),
|
||||
};
|
||||
this.progress = 0;
|
||||
this.currentTime = Date.parse(config.startDate);
|
||||
this.currentTime = initialTime;
|
||||
const timeout = setTimeout(
|
||||
() => controller.abort(new BacktestTimeoutError(this.timeoutMs)),
|
||||
this.timeoutMs
|
||||
|
|
@ -77,17 +127,7 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
|
|||
});
|
||||
signal.addEventListener('abort', onAbort, { once: true });
|
||||
try {
|
||||
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, dataset), aborted]);
|
||||
signal.throwIfAborted();
|
||||
const raw = await this.worker.execute({ type: 'run', config, data }, signal, progress => {
|
||||
this.progress = progress.progress;
|
||||
this.publish('progress', progress);
|
||||
});
|
||||
const result = nativeResultSchema.parse(JSON.parse(raw));
|
||||
this.currentTime = Date.parse(config.endDate);
|
||||
const result = await work(signal, aborted);
|
||||
this.progress = 100;
|
||||
this.publish('complete', result);
|
||||
return result;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue