stock-bot/test/fixtures/canonical-regime-research.ts
Boki 4cd6038063 refactor: pre-M0 cleanup, core library hardening, and isolated dev/QA Docker stack
- Remove ~44k LOC of dead code, legacy v1 TS simulator/strategy stack, event-bus, analytics FastAPI prototype, legacy calculation archives, and unused deps (data-ingestion provider API handlers untouched)
- Extract @stock-bot/di/http-security and isolate @stock-bot/browser to data-ingestion via ServiceContainerBuilder.withRegistrations()
- Harden @stock-bot/mongodb read-only proxies, flatten @stock-bot/postgres with unified master+research migrations, and fix @stock-bot/config EnvLoader, ServiceApplication worker lifecycle, NamespacedCache.clear(), and BatchProcessor error propagation
- Flatten workspace root (remove apps/stock/package.json) and collapse orchestrator re-export shims
- Pin all Docker image versions, add shm_size/nofile/RAM caps and docker-compose.quant.yml, fix 05-add-visible-column.sql view recreation, and decouple dev/QA Compose project names for side-by-side execution
- Update PLATFORM-PLAN, DATA-CONTRACTS (bars_1m PARTITION BY MONTH, Rust bulk ETL, DuckDB limits), MILESTONES, and servers docs
2026-11-27 00:11:50 -05:00

107 lines
3.5 KiB
TypeScript

import {
meanReversionStrategySelectionSchema,
type BuiltinStrategySelection,
type MeanReversionSignalParameters,
type MeanReversionStrategySelection,
} from '@stock-bot/stock-config/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] };
}