Unify analytics calculations and fail closed on unavailable data

This commit is contained in:
Boki 2026-10-21 19:28:21 -04:00
parent 783b619846
commit ccce401f72
31 changed files with 1978 additions and 1553 deletions

View file

@ -59,7 +59,8 @@ const config = BacktestConfigSchema.parse({
async function fixture(
empty = false,
engineOptions: ConstructorParameters<typeof RustBacktestAdapter>[1] = {}
engineOptions: ConstructorParameters<typeof RustBacktestAdapter>[1] = {},
barCount = 70
) {
// PostgreSQL protocol is replaced with an in-process database; SQL and transaction manager are real.
const db = newDb();
@ -108,7 +109,7 @@ async function fixture(
await persistEodPrices(
mongodb,
{ symbol: 'FIXTURE', exchange: 'TEST', eodSearchCode: 'FIXTURE.TEST' },
Array.from({ length: 70 }, (_, index) => {
Array.from({ length: barCount }, (_, index) => {
const close = 100 + Math.sin((index * Math.PI) / 6) * 8 + index * 0.02;
return {
date: new Date(Date.parse(config.startDate) + index * 86400000)
@ -307,6 +308,26 @@ describe('one backtest execution workflow', () => {
expect((await f.postgres.query('SELECT * FROM backtest_results')).rows).toHaveLength(1);
});
test('undefined one-timestamp statistics survive normalization, persistence and the public result reader as null', async () => {
const f = await fixture(false, {}, 1);
const services = await typescriptRuntime(f);
const runner = services.custom!.BacktestRunner!;
cleanup.unshift(async () => {
await runner.shutdown();
await services.custom?.ModeManager?.shutdown();
});
const result = await runner.runBacktest(config);
expect(result.metrics.sharpeRatio).toBeNull();
expect(result.metrics.profitFactor).toBeNull();
expect(result.metrics.cagr).toBeNull();
expect(result.metrics.annualizedReturn).toBeNull();
expect(result.analytics.riskMetrics?.volatility).toBeNull();
const saved = await new BacktestServiceV2(services).getRunResults(result.runId);
expect(saved?.metrics).toEqual(result.metrics);
expect(saved?.analytics).toEqual(result.analytics);
expect(JSON.stringify(saved)).not.toMatch(/Infinity|NaN/);
});
test('TypeScript cancellation is persisted without a partial success or completion event', async () => {
const f = await fixture();
const services = await typescriptRuntime(f);