Separate backtest execution settings from strategy parameters
This commit is contained in:
parent
420c47704e
commit
e4925ecdfe
9 changed files with 283 additions and 48 deletions
|
|
@ -1,9 +1,15 @@
|
|||
import { afterEach, describe, expect, test } from 'bun:test';
|
||||
import {
|
||||
movingAverageCrossover,
|
||||
trailingMean,
|
||||
trailingPopulationDeviation,
|
||||
} from '@stock-bot/calculations/indicators';
|
||||
import {
|
||||
IncrementalSMA,
|
||||
BacktestEngine as NativeEngine,
|
||||
TechnicalIndicators,
|
||||
} from '@stock-bot/engine';
|
||||
import { BacktestExecutionSettingsSchema } from '@stock-bot/stock-config/backtest';
|
||||
import { BacktestEngine } from '../../apps/stock/orchestrator/src/backtest/BacktestEngine';
|
||||
import { resolveNativeStrategies } from '../../apps/stock/orchestrator/src/backtest/native-strategies';
|
||||
import { backtestStrategies } from '../../apps/stock/orchestrator/src/backtest/strategy-plan';
|
||||
|
|
@ -20,11 +26,6 @@ import {
|
|||
normalizeStrategyName,
|
||||
} from '../../apps/stock/orchestrator/src/contracts/strategy-names';
|
||||
import type { ModeManager } from '../../apps/stock/orchestrator/src/core/ModeManager';
|
||||
import {
|
||||
movingAverageCrossover,
|
||||
trailingMean,
|
||||
trailingPopulationDeviation,
|
||||
} from '@stock-bot/calculations/indicators';
|
||||
import { StorageService } from '../../apps/stock/orchestrator/src/services/StorageService';
|
||||
import { MeanReversionStrategy } from '../../apps/stock/orchestrator/src/strategies/builtin/MeanReversionStrategy';
|
||||
import { SmaCrossoverStrategy } from '../../apps/stock/orchestrator/src/strategies/builtin/SmaCrossoverStrategy';
|
||||
|
|
@ -158,6 +159,74 @@ describe('explicit strategy contracts and shared planning', () => {
|
|||
expect(nativeSmaParameters.safeParse({ positionFraction: 0.1 }).success).toBe(false);
|
||||
});
|
||||
|
||||
test('mixed execution settings never reach built-in parameters or alter run-wide precedence', () => {
|
||||
const mixed = {
|
||||
...config,
|
||||
config: {
|
||||
...BacktestExecutionSettingsSchema.parse(config),
|
||||
fastPeriod: 3,
|
||||
slowPeriod: 7,
|
||||
useTypeScriptImplementation: false,
|
||||
},
|
||||
strategies: [
|
||||
{ name: 'disabled', enabled: false, config: {} },
|
||||
{
|
||||
name: 'sma',
|
||||
enabled: true,
|
||||
config: { slowPeriod: 9, dataFrequency: '1m', commission: 0.1 },
|
||||
},
|
||||
{ name: 'sma_crossover', enabled: true, config: { fastPeriod: 2 } },
|
||||
],
|
||||
};
|
||||
const before = structuredClone(mixed);
|
||||
for (const backend of ['native', 'typescript'] as const) {
|
||||
const plan = backtestStrategies(mixed, backend);
|
||||
expect(
|
||||
plan.map(entry => [entry.index, entry.parameters.fastPeriod, entry.parameters.slowPeriod])
|
||||
).toEqual([
|
||||
[0, 3, 9],
|
||||
[1, 2, 7],
|
||||
]);
|
||||
for (const entry of plan) {
|
||||
expect(entry.parameters).not.toHaveProperty('dataFrequency');
|
||||
expect(entry.parameters).not.toHaveProperty('commission');
|
||||
expect(entry.parameters).not.toHaveProperty('useTypeScriptImplementation');
|
||||
if (backend === 'typescript') {
|
||||
expect(entry.parameters.initialCapital).toBe(config.initialCapital);
|
||||
}
|
||||
}
|
||||
for (const parameters of [{ fastPreriod: 3 }, { dataFrequncy: '1d' }]) {
|
||||
expect(() => backtestStrategies({ ...config, config: parameters }, backend)).toThrow(
|
||||
'Unrecognized'
|
||||
);
|
||||
}
|
||||
}
|
||||
expect(resolveNativeStrategies(mixed).map(entry => entry.id)).toEqual([
|
||||
'backtest-strategy-0',
|
||||
'backtest-strategy-1',
|
||||
]);
|
||||
expect(mixed).toEqual(before);
|
||||
expect(mixed.dataFrequency).toBe('1d');
|
||||
});
|
||||
|
||||
test('custom factories retain their existing extension bag, including names also used by execution', () => {
|
||||
const parameters = {
|
||||
vendorOption: { window: 9 },
|
||||
dataFrequency: 'feature cadence',
|
||||
commission: 12,
|
||||
};
|
||||
const custom = backtestStrategies(
|
||||
{ ...config, strategy: 'custom', config: parameters },
|
||||
'typescript'
|
||||
);
|
||||
expect(custom[0]?.parameters).toMatchObject({
|
||||
...parameters,
|
||||
initialCapital: config.initialCapital,
|
||||
});
|
||||
(custom[0]!.parameters.vendorOption as { window: number }).window = 100;
|
||||
expect(parameters.vendorOption.window).toBe(9);
|
||||
});
|
||||
|
||||
test('mean reversion and native-only strategies validate their actual, different contracts', () => {
|
||||
const mean = typescriptMeanReversionParameters.parse({ lookbackPeriod: 80 });
|
||||
expect(mean.trendPeriod).toBe(80);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue