107 lines
3.9 KiB
TypeScript
107 lines
3.9 KiB
TypeScript
/** Trusted built-in selection only. No callbacks, code paths or checkpoint transport. */
|
|
import { z } from 'zod';
|
|
import { parseFinancialDecimal, type FinancialDecimal } from '@stock-bot/types';
|
|
import { builtinStrategy, maxStrategyPeriod } from './strategy-names';
|
|
|
|
const identity = z
|
|
.string()
|
|
.min(1)
|
|
.refine(value =>
|
|
[...value].every(character => {
|
|
const code = character.charCodeAt(0);
|
|
return (
|
|
code >= 32 && code !== 127 && !(character.length === 1 && code >= 0xd800 && code <= 0xdfff)
|
|
);
|
|
})
|
|
);
|
|
const period = z.number().int().min(1).max(maxStrategyPeriod);
|
|
const positiveBudget: z.ZodType<FinancialDecimal, z.ZodTypeDef, string> = z
|
|
.string()
|
|
.transform(value => parseFinancialDecimal(value))
|
|
.refine(
|
|
value => !value.startsWith('-') && value !== '0.00000000',
|
|
'Owner budget must be positive'
|
|
);
|
|
const nonnegativeNotional: z.ZodType<FinancialDecimal, z.ZodTypeDef, string> = z
|
|
.string()
|
|
.transform(value => parseFinancialDecimal(value))
|
|
.refine(value => !value.startsWith('-'), 'Notional volume must be nonnegative');
|
|
|
|
const smaShape = {
|
|
ownerId: identity,
|
|
strategyId: z
|
|
.string()
|
|
.refine(value => builtinStrategy(value) === 'sma_crossover', 'Unsupported canonical strategy')
|
|
.transform(() => 'sma_crossover' as const),
|
|
strategyVersion: z.literal('sma-crossover-long-only/v1'),
|
|
seed: z.number().int().min(0).max(0xffffffff),
|
|
instrumentId: identity,
|
|
seriesId: identity,
|
|
ownerBudget: positiveBudget,
|
|
parameters: z
|
|
.object({
|
|
fastPeriod: period.default(5),
|
|
slowPeriod: period.min(2).default(15),
|
|
})
|
|
.strict()
|
|
.refine(
|
|
value => value.fastPeriod < value.slowPeriod,
|
|
'SMA fastPeriod must be below slowPeriod'
|
|
),
|
|
};
|
|
export const smaStrategySelectionSchema: z.ZodObject<typeof smaShape, 'strict'> = z
|
|
.object(smaShape)
|
|
.strict();
|
|
|
|
const meanParameterShape = {
|
|
lookbackPeriod: period.min(2),
|
|
trendPeriod: period.min(3),
|
|
momentumPeriod: period,
|
|
volatilityPeriod: period.min(2),
|
|
entryZScore: z.number().finite().positive(),
|
|
exitZScore: z.number().finite().nonnegative(),
|
|
minTrendReturn: z.number().finite().gt(-1),
|
|
minMomentumReturn: z.number().finite().gt(-1),
|
|
maxVolatility: z.number().finite().nonnegative(),
|
|
minNotionalVolume: nonnegativeNotional,
|
|
regimeFilter: z.enum(['volatility', 'none']),
|
|
};
|
|
type MeanParametersSchema = z.ZodEffects<
|
|
z.ZodEffects<z.ZodObject<typeof meanParameterShape, 'strict'>>
|
|
>;
|
|
const meanParameters: MeanParametersSchema = z
|
|
.object(meanParameterShape)
|
|
.strict()
|
|
.refine(
|
|
value => value.trendPeriod > value.lookbackPeriod,
|
|
'Trend period must exceed the mean-reversion lookback'
|
|
)
|
|
.refine(
|
|
value => value.exitZScore < value.entryZScore,
|
|
'Exit threshold must be below the entry threshold'
|
|
);
|
|
|
|
const meanShape = {
|
|
ownerId: identity,
|
|
strategyId: z
|
|
.string()
|
|
.refine(value => builtinStrategy(value) === 'mean_reversion', 'Unsupported canonical strategy')
|
|
.transform(() => 'mean_reversion' as const),
|
|
strategyVersion: z.literal('mean-reversion-regime-long-only/v1'),
|
|
seed: z.number().int().min(0).max(0xffffffff),
|
|
instrumentId: identity,
|
|
seriesId: identity,
|
|
ownerBudget: positiveBudget,
|
|
parameters: meanParameters,
|
|
};
|
|
export const meanReversionStrategySelectionSchema: z.ZodObject<typeof meanShape, 'strict'> = z
|
|
.object(meanShape)
|
|
.strict();
|
|
|
|
export const builtinStrategySelectionSchema: z.ZodUnion<
|
|
[typeof smaStrategySelectionSchema, typeof meanReversionStrategySelectionSchema]
|
|
> = z.union([smaStrategySelectionSchema, meanReversionStrategySelectionSchema]);
|
|
export type SmaStrategySelection = z.infer<typeof smaStrategySelectionSchema>;
|
|
export type MeanReversionStrategySelection = z.infer<typeof meanReversionStrategySelectionSchema>;
|
|
export type MeanReversionSignalParameters = MeanReversionStrategySelection['parameters'];
|
|
export type BuiltinStrategySelection = z.infer<typeof builtinStrategySelectionSchema>;
|