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
107
test/fixtures/canonical-regime-research.ts
vendored
Normal file
107
test/fixtures/canonical-regime-research.ts
vendored
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
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] };
|
||||
}
|
||||
4
test/fixtures/canonical-sma.ts
vendored
4
test/fixtures/canonical-sma.ts
vendored
|
|
@ -1,4 +1,4 @@
|
|||
import { builtinStrategySelectionSchema } from '../../apps/stock/orchestrator/src/contracts/builtin-strategy';
|
||||
import { smaStrategySelectionSchema } from '../../apps/stock/orchestrator/src/contracts/builtin-strategy';
|
||||
import { createDatasetV2 } from '../../apps/stock/orchestrator/src/data/historical-dataset-v2';
|
||||
import { nativeReplayFixture } from './native-replay';
|
||||
|
||||
|
|
@ -48,7 +48,7 @@ export function canonicalSmaFixture(
|
|||
}),
|
||||
});
|
||||
const selections = owners.map(ownerId =>
|
||||
builtinStrategySelectionSchema.parse({
|
||||
smaStrategySelectionSchema.parse({
|
||||
ownerId,
|
||||
strategyId: 'sma',
|
||||
strategyVersion: 'sma-crossover-long-only/v1',
|
||||
|
|
|
|||
2
test/fixtures/canonical-strategy-worker.ts
vendored
2
test/fixtures/canonical-strategy-worker.ts
vendored
|
|
@ -4,7 +4,7 @@ import { strategyReplayRequestSchema } from '../../apps/stock/orchestrator/src/c
|
|||
import {
|
||||
createBuiltinStrategy,
|
||||
validateBuiltinStrategyBindings,
|
||||
} from '../../apps/stock/orchestrator/src/strategies/canonical-sma';
|
||||
} from '../../apps/stock/orchestrator/src/strategies/canonical-builtins';
|
||||
|
||||
function send(message: unknown): Promise<void> {
|
||||
return new Promise((resolve, reject) =>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue