107 lines
4.2 KiB
TypeScript
107 lines
4.2 KiB
TypeScript
/**
|
|
* Strategy-facing types for synchronous decisions on the canonical engine.
|
|
* The execution host supplies these values and owns time,
|
|
* order admission and accounting. Implementations return intent, not broker calls.
|
|
*/
|
|
import type {
|
|
DecisionContext,
|
|
NativeOrderStatus,
|
|
PublishedObservation,
|
|
ReplayIdentity,
|
|
} from '@stock-bot/stock-config/canonical-replay';
|
|
import type { DecisionIdentity, FinancialStrategyIntent } from '@stock-bot/types';
|
|
|
|
export type { DecisionIdentity, FinancialDecimal, FinancialStrategyIntent } from '@stock-bot/types';
|
|
|
|
export type DeepReadonly<T> = T extends string | number | boolean | null | undefined
|
|
? T
|
|
: T extends object
|
|
? { readonly [Key in keyof T]: DeepReadonly<T[Key]> }
|
|
: T;
|
|
|
|
/**
|
|
* Order outcomes copied when the previous decision batch was admitted.
|
|
* These are not later fill notifications: inspect current positions and the
|
|
* account view to learn what has executed since that decision.
|
|
*/
|
|
export interface StrategyAdmissionFeedback {
|
|
decision: DecisionIdentity;
|
|
revision: number;
|
|
orders: {
|
|
orderId: string;
|
|
instrumentId: string;
|
|
decision: DecisionIdentity;
|
|
status: NativeOrderStatus;
|
|
}[];
|
|
}
|
|
|
|
/** Read-only input to one owner at an engine-defined decision barrier. */
|
|
export interface StrategyContext<State = unknown> {
|
|
readonly ownerId: string;
|
|
readonly strategyId: string;
|
|
readonly strategyVersion: string;
|
|
/** Monotonic per-owner identity; advances after confirmed batch admission. */
|
|
readonly decisionSequence: number;
|
|
/** Engine availability time in milliseconds, never the host's wall clock. */
|
|
readonly now: number;
|
|
/** Due owners, account revision and freshness of the required input series. */
|
|
readonly barrier: DeepReadonly<DecisionContext['barrier']>;
|
|
/** The exact same read-only account view is shared by all due callbacks. */
|
|
readonly portfolio: DeepReadonly<DecisionContext['view']>;
|
|
/** Already filtered to this owner; portfolio contains the account-wide view. */
|
|
readonly positions: DeepReadonly<DecisionContext['view']['portfolio']['positions']>;
|
|
/** Published since this owner's last callback, buffered across slower cadences. */
|
|
readonly observations: readonly DeepReadonly<PublishedObservation>[];
|
|
readonly previousAdmission: DeepReadonly<StrategyAdmissionFeedback> | null;
|
|
/** Last confirmed JSON state, or initialState before the first decision. */
|
|
readonly state: DeepReadonly<State>;
|
|
/** Valid only during this synchronous callback; never use Math.random for decisions. */
|
|
readonly random: () => number;
|
|
}
|
|
|
|
/**
|
|
* One strategy instance bound to one configured financial owner and intent mode.
|
|
* Keep decision history in explicit state so a fresh attempt can recompute it.
|
|
* Native risk rules still apply to every returned intent.
|
|
*/
|
|
export interface StrategyProgram<State = unknown> {
|
|
readonly ownerId: string;
|
|
readonly strategyId: string;
|
|
readonly strategyVersion: string;
|
|
readonly mode: 'target' | 'orders';
|
|
readonly seed: number;
|
|
/** Finite plain JSON, checked and detached before the native attempt starts. */
|
|
readonly initialState: State;
|
|
/**
|
|
* Propose the next JSON state and target holdings or explicit order actions.
|
|
* A target quantity is this owner's desired total, not an incremental buy.
|
|
* Return synchronously: state and seeded RNG advances become confirmed only
|
|
* after Rust acknowledges the whole decision batch, including any rejections.
|
|
*/
|
|
decide(context: StrategyContext<State>): {
|
|
nextState: State;
|
|
intent: FinancialStrategyIntent;
|
|
};
|
|
}
|
|
|
|
/** Confirmed callback state for inspection; it does not contain the native ledger. */
|
|
export interface StrategyDiagnostic {
|
|
ownerId: string;
|
|
strategyId: string;
|
|
strategyVersion: string;
|
|
nextDecisionSequence: number;
|
|
state: unknown;
|
|
rng: { algorithm: 'lcg32-v1'; seed: number; state: number; draws: number };
|
|
observationCount: number;
|
|
previousAdmission: StrategyAdmissionFeedback | null;
|
|
}
|
|
|
|
/** Last confirmed strategy transitions only. This cannot restore financial state.
|
|
* Retry always starts a fresh native session and recomputes from original inputs. */
|
|
export interface StrategyDiagnosticSnapshot {
|
|
version: 1;
|
|
identity: ReplayIdentity;
|
|
committedBarriers: number;
|
|
lastBarrierId: string | null;
|
|
strategies: StrategyDiagnostic[];
|
|
}
|