stock-bot/libs/research/examples/fresh-position.ts

39 lines
1.6 KiB
TypeScript

import type { FinancialDecimal, StrategyProgram } from '@stock-bot/research/strategy-api';
import type { RetainedProgramSelection } from '@stock-bot/stock-config/candidate-execution';
/** Authoring example only: request a fixed holding when the selected series is fresh.
* This demonstrates the callback contract, not a validated trading hypothesis. */
export function createStrategy(
selection: RetainedProgramSelection
): StrategyProgram<{ decisions: number }> {
const shares = selection.parameters.shares;
if (typeof shares !== 'number' || !Number.isSafeInteger(shares) || shares < 0) {
throw new Error('shares must be a nonnegative safe integer');
}
// The checked integer gives an exact canonical decimal with no runtime imports.
// Rust still validates intent quantities and enforces all financial constraints.
const quantity = `${shares}.00000000` as FinancialDecimal;
return {
ownerId: selection.ownerId,
strategyId: selection.strategyId,
strategyVersion: selection.strategyVersion,
seed: selection.seed,
mode: 'target',
initialState: { decisions: 0 },
decide(context) {
const fresh = context.barrier.inputs.some(
input =>
input.series_id === selection.seriesId &&
typeof input.availability === 'object' &&
'Fresh' in input.availability
);
return {
nextState: { decisions: context.state.decisions + 1 },
intent: {
mode: 'target',
targets: fresh ? [{ instrumentId: selection.instrumentId, quantity }] : [],
},
};
},
};
}