stock-bot/test/fixtures/walk-forward.ts

50 lines
2 KiB
TypeScript

import type { WalkForwardInput } from '@stock-bot/stock-config/walk-forward';
import { createDatasetV2 } from '../../apps/stock/orchestrator/src/data/historical-dataset-v2';
import { nativeReplayFixture } from './native-replay';
/** Two explicit rolling windows; only the later test suffix changes in isolation regressions. */
export function walkForwardDataset(laterTestPrice?: number) {
const fixture = nativeReplayFixture(72);
const { calendars, series, executionSeries, bounds, universe, corporateActions } =
fixture.dataset.manifest;
const closes = [3, 2, 1, 4, 5, 1, 1, 3, 6, 2, 1, 4];
const dataset = createDatasetV2({
metadata: { calendars, series, executionSeries, bounds, universe, corporateActions },
bars: fixture.dataset.bars.map((bar, index) => {
const close =
index >= 64 && laterTestPrice !== undefined
? laterTestPrice + index
: closes[index % closes.length]!;
const open = index === 0 ? 3 : closes[(index - 1) % closes.length]!;
return {
seriesId: bar.seriesId,
timestamp: bar.intervalStart,
availableAt: bar.availableAt,
sourceSequence: bar.sourceSequence,
open,
close,
high: Math.max(open, close),
low: Math.min(open, close),
volume: 1000,
};
}),
});
const at = (step: number) => fixture.start + step * fixture.minute;
const windows: WalkForwardInput['windows'] = [
{
version: 1,
labelHorizonMs: 0,
train: { warmupStart: at(0), tradeStart: at(6), tradeEnd: at(12) },
validation: { warmupStart: at(10), tradeStart: at(16), tradeEnd: at(24) },
test: { warmupStart: at(22), tradeStart: at(28), tradeEnd: at(36) },
},
{
version: 1,
labelHorizonMs: 0,
train: { warmupStart: at(24), tradeStart: at(30), tradeEnd: at(48) },
validation: { warmupStart: at(46), tradeStart: at(52), tradeEnd: at(60) },
test: { warmupStart: at(58), tradeStart: at(64), tradeEnd: at(72) },
},
];
return { ...fixture, dataset, windows };
}