Skip to content

docs(trading-strategies): document strategy contract, runtimes, and data access#1185

Open
bennycode wants to merge 5 commits into
mainfrom
docs/trading-strategies-contract-and-runtimes
Open

docs(trading-strategies): document strategy contract, runtimes, and data access#1185
bennycode wants to merge 5 commits into
mainfrom
docs/trading-strategies-contract-and-runtimes

Conversation

@bennycode

Copy link
Copy Markdown
Owner

What

The trading-strategies README explained trading domain knowledge well but was thin on the actual programming contract. This adds the missing architectural docs and fixes some stale/incorrect examples.

Changes

  • New "The Strategy Contract" section — the candle-in / advice-out model that wasn't documented anywhere: processCandle(candle, state) as the one method you implement, the TradingSessionState input, the real OrderAdvice output union, and the full lifecycle-hook table (init/onFill/onOrderFilled/onMessage). Also clarifies onCandle is a template method — you override processCandle.
  • New "Running a Strategy" section — documents the two runtimes (BacktestExecutor historical vs TradingSession live) and the key fact that both route advice through the same AdviceExecutor, so a backtest is a faithful dry run of the live path. Includes runnable examples for each.
  • New "Strategies Are Just Async Code" section — makes explicit that processCandle/init are async by design and may await any data source (DB, news, analyst/stock reports, an MCP such as TipRanks), not only trading-signals indicators. Cites MeanReversionStrategy's fetchCandles and init(market) as existing proof, with a perf caveat to keep the per-candle path cheap.
  • Rewrote "Strategy Signals" — the previous BUY_MARKET/SELL_LIMIT/NONE enum did not exist in the code; replaced with the real OrderAdvice shapes.
  • Fixed Usage importsOrderAdvice/AllAvailableAmount come from trading-strategies (were imported from @typedtrader/exchange); OrderSide/OrderType are the real names (were the non-existent ExchangeOrderSide/ExchangeOrderType).

Notes

Docs-only change. All type names and imports were verified against the current source (Strategy.ts, TradingSessionTypes.ts, BacktestExecutor.ts, MeanReversionStrategy.ts).

…ata access

- Add 'The Strategy Contract' section: processCandle candle-in/advice-out
  model, TradingSessionState input, OrderAdvice output, lifecycle hooks
- Add 'Running a Strategy' section: BacktestExecutor vs TradingSession,
  both routing advice through the shared AdviceExecutor
- Add 'Strategies Are Just Async Code' section: strategies may await any
  data source (DB, news, reports, MCP), not only trading-signals indicators
- Rewrite 'Strategy Signals' to the real OrderAdvice shapes (the previous
  BUY_MARKET/NONE enum did not exist)
- Fix Usage imports: OrderAdvice/AllAvailableAmount from trading-strategies,
  OrderSide/OrderType from @typedtrader/exchange
bennycode added 4 commits July 9, 2026 16:09
Add an 'Extending a base strategy' note to the Strategy Contract section:
subclasses can layer shared behaviour onto the base contract (ProtectedStrategy
adds stop-loss/take-profit guards), or extend Strategy directly when none is
needed.
Reader feedback: the added sections were too technical. Replace the type
signatures, interface/union dumps, lifecycle-hooks table, and executor/event
internals with concept-first prose:

- 'How a Strategy Works' — buy / sell / wait, one candle at a time
- 'Same Strategy, Backtest or Live' — one short backtest example
- 'What a Strategy Can Use' — indicators or any other data

Drop the now-orphaned bare-import 'Usage' block.
## Usage
## How a Strategy Works

A strategy watches the market one candle at a time and decides what to do next: **buy, sell, or wait.** You write that decision; the library handles the rest — turning it into a correctly sized order and keeping track of balances, fees, and each exchange's trading rules for you.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@claude don't use this - AI writing style


A strategy watches the market one candle at a time and decides what to do next: **buy, sell, or wait.** You write that decision; the library handles the rest — turning it into a correctly sized order and keeping track of balances, fees, and each exchange's trading rules for you.

On every new candle your strategy sees the latest price data and a snapshot of your account, then returns one of:

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this showcase an example code that returns an advice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant