- 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
71 lines
3 KiB
TypeScript
71 lines
3 KiB
TypeScript
import { parseFinancialDecimal } from '@stock-bot/types';
|
|
import type { NativeBacktestWorker } from '../src/backtest/NativeBacktestWorker';
|
|
import { replayIdentitySchema } from '@stock-bot/stock-config/canonical-replay';
|
|
import {
|
|
parseStrategyReplayResult,
|
|
strategyReplayRequestSchema,
|
|
type StrategyReplayRequest,
|
|
} from '../src/contracts/strategy-replay';
|
|
|
|
/** Four explicit comparisons, not an optimizer. Both ablations retain identical
|
|
* warmup, dataset, thresholds, seeds and engine policies except the named change.
|
|
* Costs are supplied by the caller; this example never fits or chooses thresholds. */
|
|
export function createRegimeComparisons(
|
|
input: StrategyReplayRequest,
|
|
alternativeCosts: { feeRate: string; slippageRate: string }
|
|
) {
|
|
const baseline = strategyReplayRequestSchema.parse(input);
|
|
if (!baseline.strategies.some(strategy => strategy.strategyId === 'mean_reversion')) {
|
|
throw new Error('Regime comparison requires a canonical mean-reversion selection');
|
|
}
|
|
return ([false, true] as const).flatMap(changeCosts =>
|
|
(['volatility', 'none'] as const).map(regimeFilter => {
|
|
const request = structuredClone(baseline);
|
|
request.resultFormat = 'report';
|
|
for (const strategy of request.strategies) {
|
|
if (strategy.strategyId === 'mean_reversion') {
|
|
strategy.parameters.regimeFilter = regimeFilter;
|
|
}
|
|
}
|
|
if (changeCosts) {
|
|
request.config.matching.risk.feeRate = parseFinancialDecimal(alternativeCosts.feeRate);
|
|
request.config.matching.slippageRate = parseFinancialDecimal(alternativeCosts.slippageRate);
|
|
}
|
|
return {
|
|
label: `${regimeFilter}${changeCosts ? '-alternative-costs' : ''}`,
|
|
request: strategyReplayRequestSchema.parse(request),
|
|
};
|
|
})
|
|
);
|
|
}
|
|
|
|
/** Uses the existing isolated worker, never paper services. Caller owns worker
|
|
* lifecycle and stores these exact requests/results with code/engine artifacts.
|
|
* This is diagnostic comparison only: R08 walk-forward/holdout validation is
|
|
* still required before drawing conclusions about strategy performance. */
|
|
export async function runRegimeComparisons(
|
|
worker: Pick<NativeBacktestWorker, 'execute'>,
|
|
input: StrategyReplayRequest,
|
|
alternativeCosts: { feeRate: string; slippageRate: string },
|
|
signal: AbortSignal
|
|
) {
|
|
const trials = createRegimeComparisons(input, alternativeCosts);
|
|
const results = [];
|
|
for (const trial of trials) {
|
|
signal.throwIfAborted();
|
|
const raw = await worker.execute(trial.request, signal, () => {});
|
|
const dataset = trial.request.dataset as { manifest?: { hash?: unknown } } | null;
|
|
const result = parseStrategyReplayResult(raw, {
|
|
strategies: trial.request.strategies,
|
|
identity: replayIdentitySchema.parse({
|
|
version: 1,
|
|
sessionId: trial.request.config.matching.risk.ledger.sessionId,
|
|
datasetHash: dataset?.manifest?.hash,
|
|
}),
|
|
policy: trial.request.config,
|
|
format: 'report',
|
|
});
|
|
results.push({ ...trial, result });
|
|
}
|
|
return results;
|
|
}
|