feat(strategies): add canonical regime-aware mean reversion
This commit is contained in:
parent
75abae2cfc
commit
6a67ae8683
24 changed files with 1910 additions and 88 deletions
110
test/integration/regime-research-example.test.ts
Normal file
110
test/integration/regime-research-example.test.ts
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
import { expect, test } from 'bun:test';
|
||||
import { parseFinancialDecimal } from '@stock-bot/types';
|
||||
import {
|
||||
createRegimeComparisons,
|
||||
runRegimeComparisons,
|
||||
} from '../../apps/stock/orchestrator/examples/canonical-regime-research';
|
||||
import { NativeBacktestWorker } from '../../apps/stock/orchestrator/src/backtest/NativeBacktestWorker';
|
||||
import type { StrategyReplayRequest } from '../../apps/stock/orchestrator/src/contracts/strategy-replay';
|
||||
import {
|
||||
canonicalRegimeResearchFixture,
|
||||
canonicalRegimeSelection,
|
||||
} from '../fixtures/canonical-regime-research';
|
||||
|
||||
function request(): StrategyReplayRequest {
|
||||
const fixture = canonicalRegimeResearchFixture({
|
||||
selections: [canonicalRegimeSelection('A', { parameters: { maxVolatility: 0.05 } })],
|
||||
});
|
||||
return {
|
||||
type: 'runStrategyReplay',
|
||||
version: 1,
|
||||
dataset: fixture.dataset,
|
||||
config: fixture.config,
|
||||
strategies: fixture.selections,
|
||||
eventBudget: 2,
|
||||
resultFormat: 'report',
|
||||
};
|
||||
}
|
||||
const costs = { feeRate: '0.02', slippageRate: '0.01' };
|
||||
|
||||
test('comparison inputs are detached, explicit and differ only by declared filter/cost changes', () => {
|
||||
const input = request();
|
||||
const original = structuredClone(input);
|
||||
const trials = createRegimeComparisons(input, costs);
|
||||
expect(trials.map(trial => trial.label)).toEqual([
|
||||
'volatility',
|
||||
'none',
|
||||
'volatility-alternative-costs',
|
||||
'none-alternative-costs',
|
||||
]);
|
||||
const [on, off, higherOn, higherOff] = trials.map(trial => structuredClone(trial.request));
|
||||
for (const candidate of [on!, off!, higherOn!, higherOff!]) {
|
||||
expect(candidate.dataset).toEqual(original.dataset);
|
||||
expect(candidate.strategies[0]!.strategyVersion).toBe('mean-reversion-regime-long-only/v1');
|
||||
const strategy = candidate.strategies[0]!;
|
||||
if (strategy.strategyId === 'mean_reversion') {
|
||||
strategy.parameters.regimeFilter = 'volatility';
|
||||
}
|
||||
candidate.config.matching.risk.feeRate = parseFinancialDecimal('0.01');
|
||||
candidate.config.matching.slippageRate = parseFinancialDecimal('0');
|
||||
}
|
||||
expect(off!).toEqual(on!);
|
||||
expect(higherOn!).toEqual(on!);
|
||||
expect(higherOff!).toEqual(on!);
|
||||
trials[0]!.request.config.matching.risk.ledger.accountId = 'changed';
|
||||
expect(input).toEqual(original);
|
||||
expect(trials[1]!.request.config.matching.risk.ledger.accountId).toBe(
|
||||
original.config.matching.risk.ledger.accountId
|
||||
);
|
||||
});
|
||||
|
||||
test('malformed costs and cancellation cannot submit a trial', async () => {
|
||||
let calls = 0;
|
||||
const worker = {
|
||||
execute: async () => {
|
||||
calls++;
|
||||
throw new Error('unexpected submit');
|
||||
},
|
||||
};
|
||||
await expect(
|
||||
runRegimeComparisons(
|
||||
worker,
|
||||
request(),
|
||||
{ ...costs, feeRate: 'invalid' },
|
||||
new AbortController().signal
|
||||
)
|
||||
).rejects.toThrow();
|
||||
const controller = new AbortController();
|
||||
controller.abort(new Error('cancelled'));
|
||||
await expect(runRegimeComparisons(worker, request(), costs, controller.signal)).rejects.toThrow(
|
||||
'cancelled'
|
||||
);
|
||||
expect(calls).toBe(0);
|
||||
});
|
||||
|
||||
test('four actual worker reports preserve exact inputs and reproduce the small economic oracle', async () => {
|
||||
const input = request();
|
||||
const result = await runRegimeComparisons(
|
||||
new NativeBacktestWorker(),
|
||||
input,
|
||||
costs,
|
||||
new AbortController().signal
|
||||
);
|
||||
expect(result).toHaveLength(4);
|
||||
expect(result.map(trial => trial.result.result.view.equity)).toEqual(
|
||||
['10019.80000000', '10038.20000000', '10015.40440000', '10034.60800000'].map(
|
||||
parseFinancialDecimal
|
||||
)
|
||||
);
|
||||
expect(result.map(trial => trial.result.strategies)).toEqual(
|
||||
result.map(trial => trial.request.strategies)
|
||||
);
|
||||
expect(
|
||||
await runRegimeComparisons(
|
||||
new NativeBacktestWorker(),
|
||||
input,
|
||||
costs,
|
||||
new AbortController().signal
|
||||
)
|
||||
).toEqual(result);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue