107 lines
3.5 KiB
TypeScript
107 lines
3.5 KiB
TypeScript
import {
|
|
meanReversionStrategySelectionSchema,
|
|
type BuiltinStrategySelection,
|
|
type MeanReversionSignalParameters,
|
|
type MeanReversionStrategySelection,
|
|
} from '../../apps/stock/orchestrator/src/contracts/builtin-strategy';
|
|
import { createDatasetV2 } from '../../apps/stock/orchestrator/src/data/historical-dataset-v2';
|
|
import { nativeReplayFixture } from './native-replay';
|
|
|
|
export function canonicalRegimeSelection(
|
|
ownerId = 'A',
|
|
options: {
|
|
ownerBudget?: string;
|
|
parameters?: Partial<MeanReversionSignalParameters>;
|
|
} = {}
|
|
): MeanReversionStrategySelection {
|
|
return meanReversionStrategySelectionSchema.parse({
|
|
ownerId,
|
|
strategyId: 'mean_reversion',
|
|
strategyVersion: 'mean-reversion-regime-long-only/v1',
|
|
seed: 42,
|
|
instrumentId: 'X0.US',
|
|
seriesId: 'X0.US:1m',
|
|
ownerBudget: options.ownerBudget ?? '80',
|
|
parameters: {
|
|
lookbackPeriod: 2,
|
|
trendPeriod: 3,
|
|
momentumPeriod: 1,
|
|
volatilityPeriod: 2,
|
|
entryZScore: 1,
|
|
exitZScore: 0,
|
|
minTrendReturn: -0.9,
|
|
minMomentumReturn: -0.9,
|
|
maxVolatility: 1,
|
|
minNotionalVolume: '0',
|
|
regimeFilter: 'volatility',
|
|
...options.parameters,
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Small economic oracle, not a profitable-strategy claim: with the default
|
|
* selection, the third completed close8 requests ten shares, filled at next
|
|
* open7. The fifth close10 requests flat, filled at next open11. Gross40,
|
|
* fees1.80 at1%, final equity10038.20. No unobserved close is executable. */
|
|
export function canonicalRegimeResearchFixture(
|
|
options: {
|
|
selections?: readonly BuiltinStrategySelection[];
|
|
closes?: readonly number[];
|
|
opens?: readonly number[];
|
|
volumes?: readonly number[];
|
|
availabilitySteps?: readonly number[];
|
|
tradeStartStep?: number;
|
|
feeRate?: string;
|
|
slippageRate?: string;
|
|
} = {}
|
|
) {
|
|
const closes = options.closes ?? [10, 10, 8, 7, 10, 10];
|
|
const opens = options.opens ?? [10, 10, 10, 7, 9, 11];
|
|
if (
|
|
opens.length !== closes.length ||
|
|
(options.volumes && options.volumes.length !== closes.length) ||
|
|
(options.availabilitySteps && options.availabilitySteps.length !== closes.length)
|
|
) {
|
|
throw new Error('Research fixture columns must have equal lengths');
|
|
}
|
|
const selections = options.selections ?? [canonicalRegimeSelection()];
|
|
const fixture = nativeReplayFixture(
|
|
closes.length,
|
|
1,
|
|
selections.map(item => item.ownerId)
|
|
);
|
|
const { calendars, series, executionSeries, bounds, universe, corporateActions } =
|
|
fixture.dataset.manifest;
|
|
const dataset = createDatasetV2({
|
|
metadata: {
|
|
calendars,
|
|
series,
|
|
executionSeries,
|
|
bounds: {
|
|
...bounds,
|
|
tradeStart: fixture.start + (options.tradeStartStep ?? 1) * fixture.minute,
|
|
},
|
|
universe,
|
|
corporateActions,
|
|
},
|
|
bars: fixture.dataset.bars.map((bar, index) => {
|
|
const open = opens[index]!,
|
|
close = closes[index]!;
|
|
return {
|
|
seriesId: bar.seriesId,
|
|
timestamp: bar.intervalStart,
|
|
availableAt:
|
|
fixture.start + (options.availabilitySteps?.[index] ?? index + 1) * fixture.minute,
|
|
sourceSequence: bar.sourceSequence,
|
|
open,
|
|
high: Math.max(open, close),
|
|
low: Math.min(open, close),
|
|
close,
|
|
volume: options.volumes?.[index] ?? 1000,
|
|
};
|
|
}),
|
|
});
|
|
fixture.config.matching.risk.feeRate = options.feeRate ?? '0.01';
|
|
fixture.config.matching.slippageRate = options.slippageRate ?? '0';
|
|
return { ...fixture, dataset, selections: [...selections] };
|
|
}
|