feat(strategies): publish type-only strategy authoring API (CL31)

This commit is contained in:
Boki 2026-10-28 14:42:58 -04:00
parent a411f9a665
commit 3a63b2039e
13 changed files with 300 additions and 121 deletions

View file

@ -8,7 +8,7 @@ A strategy is a TypeScript function that reads observations and account state, t
| File | What to look for |
| --- | --- |
| [canonical-strategy.ts](../../apps/stock/orchestrator/src/contracts/canonical-strategy.ts) | `StrategyProgram`, its `decide` callback, and the read-only `StrategyContext`. |
| [strategy-api.ts](../../libs/research/src/strategy-api.ts) | `StrategyProgram`, its `decide` callback, and the read-only `StrategyContext`. |
| [canonical-sma.ts](../../apps/stock/orchestrator/src/strategies/canonical-sma.ts) | A complete strategy: validated parameters, versioned state, history, crossover, target quantity. |
| [canonical-mean-reversion.ts](../../apps/stock/orchestrator/src/strategies/canonical-mean-reversion.ts) | Trend, momentum, volatility regime, entry filters, and exits composed in TypeScript. |
| [canonical-builtins.ts](../../apps/stock/orchestrator/src/strategies/canonical-builtins.ts) | The small built-in factory and checks binding each strategy owner to its instrument and observation series. |
@ -33,12 +33,14 @@ Keep history in explicit state, use engine time instead of `Date.now()`, and avo
## A minimal callback shape
This is a deliberately simple **in-repository source example**, using imports as if placed beside `canonical-sma.ts`. It requests one share when its series is fresh; it is not an investment strategy or a ready-to-upload candidate package.
Import author types from `@stock-bot/research/strategy-api`. This subpath contains only types; it exposes no native sessions, execution controllers, brokers, or storage services. Existing host imports of `contracts/canonical-strategy.ts` remain compatible.
This simple callback requests one share when its series is fresh. It demonstrates the authoring contract; it is not a validated investment strategy or a ready-to-upload candidate package.
```ts
import { parseFinancialDecimal } from '@stock-bot/types';
import type { RetainedProgramSelection } from '@stock-bot/stock-config/candidate-execution';
import type { StrategyProgram } from '../contracts/canonical-strategy';
import type { StrategyProgram } from '@stock-bot/research/strategy-api';
export function createStrategy(
selection: RetainedProgramSelection
@ -73,6 +75,8 @@ export function createStrategy(
}
```
For a self-contained source with no runtime imports, use [fresh-position.ts](../../libs/research/examples/fresh-position.ts). Its `shares` parameter must be a nonnegative safe integer; the example converts that checked integer to a fixed eight-decimal quantity. All imports erase during TypeScript transformation. [The public API tests](../../scripts/test/public-strategy-api.test.ts) compile the example outside the monorepo without path aliases, check the emitted module, and compare source and packaged decisions/state through the same Rust replay. Production candidate packaging still uses the existing isolated CLI; this test does not replace isolation or approval.
`quantity` is a desired **total holding for this owner**, not “buy this many more.” `parseFinancialDecimal('1')` creates the validated financial string `"1.00000000"`. Numeric arrays remain useful for features; financial postings use the decimal contract.
An empty target list means no new target update. An explicit zero target requests an exit. Neither guarantees a fill: Rust reconciles the target with holdings and pending orders, applies risk checks, and uses eligible future execution events. Never report a position change merely because the callback requested one.