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
|
|
@ -33,12 +33,12 @@ export const RiskLimitsSchema = z.object({
|
|||
maxGrossExposure: z.number().finite().positive(),
|
||||
maxSymbolExposure: z.number().finite().positive(),
|
||||
});
|
||||
export const BacktestConfigSchema = z.object({
|
||||
/** Run-wide settings are owned here, never by an individual strategy's parameter bag. */
|
||||
export const BacktestExecutionSettingsSchema = z.object({
|
||||
mode: z.literal('backtest'),
|
||||
name: z.string().optional(),
|
||||
runId: z.string().uuid().optional(),
|
||||
backtestId: z.string().uuid().optional(),
|
||||
config: z.record(z.unknown()).optional(),
|
||||
startDate: z.string().datetime(),
|
||||
endDate: z.string().datetime(),
|
||||
symbols: z
|
||||
|
|
@ -49,16 +49,6 @@ export const BacktestConfigSchema = z.object({
|
|||
datasetHash: datasetHashSchema.optional(),
|
||||
initialCapital: z.number().finite().positive(),
|
||||
dataFrequency: z.enum(['1m', '5m', '15m', '1h', '1d']),
|
||||
strategy: z.string().min(1),
|
||||
strategies: z
|
||||
.array(
|
||||
z.object({
|
||||
name: z.string(),
|
||||
enabled: z.boolean().default(true),
|
||||
config: z.record(z.unknown()).optional(),
|
||||
})
|
||||
)
|
||||
.optional(),
|
||||
commission: z.number().finite().min(0).max(0.1).optional(),
|
||||
slippage: z.number().finite().min(0).max(0.1).optional(),
|
||||
fillModel: z
|
||||
|
|
@ -71,7 +61,45 @@ export const BacktestConfigSchema = z.object({
|
|||
speed: z.enum(['max', 'realtime', '2x', '5x', '10x']).default('max'),
|
||||
riskLimits: RiskLimitsSchema.optional(),
|
||||
});
|
||||
export const BacktestConfigSchema = BacktestExecutionSettingsSchema.extend({
|
||||
config: z.record(z.unknown()).optional(),
|
||||
strategy: z.string().min(1),
|
||||
strategies: z
|
||||
.array(
|
||||
z.object({
|
||||
name: z.string(),
|
||||
enabled: z.boolean().default(true),
|
||||
config: z.record(z.unknown()).optional(),
|
||||
})
|
||||
)
|
||||
.optional(),
|
||||
});
|
||||
export type BacktestConfig = z.infer<typeof BacktestConfigSchema>;
|
||||
export type BacktestExecutionSettings = z.infer<typeof BacktestExecutionSettingsSchema>;
|
||||
|
||||
const executionSettingNames = new Set(Object.keys(BacktestExecutionSettingsSchema.shape));
|
||||
const legacyImplementationHint = z.boolean().optional();
|
||||
|
||||
export interface BacktestStrategyInput {
|
||||
parameters: Record<string, unknown>;
|
||||
/** Compatibility hint only: it never selects or changes the requested backend. */
|
||||
useTypeScriptImplementation?: boolean;
|
||||
}
|
||||
|
||||
/** Project a legacy mixed bag once at the request-to-built-in-strategy boundary.
|
||||
* Schema-owned execution settings cannot leak as parameters or override the run.
|
||||
* Unknown strategy keys remain visible so their strategy schema can reject typos. */
|
||||
export function normalizeBacktestStrategyConfig(
|
||||
input: Record<string, unknown>
|
||||
): BacktestStrategyInput {
|
||||
const { useTypeScriptImplementation, ...mixed } = input;
|
||||
return {
|
||||
parameters: Object.fromEntries(
|
||||
Object.entries(mixed).filter(([key]) => !executionSettingNames.has(key))
|
||||
),
|
||||
useTypeScriptImplementation: legacyImplementationHint.parse(useTypeScriptImplementation),
|
||||
};
|
||||
}
|
||||
|
||||
/** One acceptance/defaulting rule for API snapshots, direct execution and queued replay. */
|
||||
export function parseBacktestRequest(input: unknown): BacktestConfig {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue