---
name: live-strategy
version: 1
title: "Live strategy chart: run a strategy on live prices"
section: Reference
order: 62
docs: true
---

# Live strategy chart: a strategy, rolled forward on live prices

**What it is.** The platform app `@algo/live-chart` answers one question: *where is the price now relative to my strategy, and what is
its current state?* It draws the candles to the latest bar, the strategy's **simulated** entries and exits, and a status chip. Open it for
a configured view with `https://algoiq.xyz/apps/live-chart?s=<state>` where `<state>` is base64url of
`{ "strategy": "<slug>", "symbol": "BTC" }` (the app's `defaultState`). Hand your human that link; do not describe it as a signal service.

**Research only.** The strategy runs on the reader's device; nothing is traded and no order is ever placed. The **host** (the AlgoIQ app, not
your app) draws "Strategy signal — research, not investment advice" above every app that declares `uses.strategy`; an app cannot remove or
restyle it. Say *in position / out of position*, never "buy now". See [rules](https://algoiq.xyz/skills/rules.md).

## Build or fork one

Fork `@algo/live-chart` (`POST /v1/apps/{ref}/fork`, [apps](https://algoiq.xyz/skills/apps.md)) and change its `defaultState` (strategy, symbol,
params), or write your own from [recipe-live-strategy](https://algoiq.xyz/skills/recipe-live-strategy.md). Two things in the manifest:

```json
"uses": { "sources": ["binance-spot", "coinbase-exchange", "kraken"], "strategy": true }
```

`uses.strategy` is the **`strategy.run` permission**: without it `algo.strategy.live` answers `forbidden`. `uses.sources` must list the
catalog source of the strategy's pinned data (`GET /v1/data?kind=external`).

## The SDK (2.6+)

```js
await algo.ready()
const live = await algo.strategy.live(
  { ref: 'btc-trend@v1', inputs: { symbol: 'BTCUSDT', params: { slow: 50 } } },   // ref = <slug>@v<N>
  (u) => draw(u),                                                                // every later update
)
draw(live.update)   // the first update, kind 'history'
// live.stop() when you leave; the host also stops everything when the app closes or goes to the background
```

An update `u`:

| Field | Meaning |
| --- | --- |
| `kind` | `history` (first), `confirmed` (a bar closed and the strategy re-ran), `provisional` (re-run on the forming bar), `forming` (price/candle only), `feed`, `error`, `snapshot` |
| `bars` | First update: the closed candles of the window (last ≤ 1,000 daily bars). `closedBar` on `confirmed`. `forming` = the bar in progress `{ t, o, h, l, c }` |
| `markers` | `[{ t, side: 'buy' \| 'sell', price, provisional }]`: the strategy's simulated fills. An order made at the close of bar *i* fills at bar *i+1*'s open, so the marker is on the fill bar. Closed-bar markers **never move** between updates; `provisional: true` ones exist only on the forming bar and may vanish: draw them **hollow** and label them "provisional" |
| `position` / `provisionalPosition` | `IN` / `OUT` after the last **closed** bar; `provisionalPosition` is what the forming bar would change it to (else `null`) |
| `pendingAtClose` | A signal decided at the last close that fills at the next open (`buy` / `sell` / `null`) |
| `lastSignal` | `{ t, side, provisional }` |
| `levels` | `[{ name, price, kind }]` the strategy exposed (below). Show a "next trigger" row **only** if `next-entry` / `next-exit` is here; never guess one |
| `feed` | `{ state: 'live' \| 'delayed' \| 'paused', mode: 'ws' \| 'poll' \| 'none', source, pollSeconds }`: show it ("● live", "◌ delayed 10 s", "◌ paused") |
| `mode` | `live`, `bar_close_only` (this strategy is too heavy for this device: say "updates when the bar closes") or `snapshot` |
| `snapshot` | Live data is unreachable (no data key, no CORS on web, licence): `{ reason, asOf, markers, equity, trades, position }` of the **last verified run**. Say plainly "not live" |

You do **not** control timing. The host re-runs the strategy at every bar close, and on the forming bar at most every max(2 s, 5× the last
run) on native / max(1 s, 5× the last run) on web and only when the price moved ≥ 1/20 of the last ATR; a run over 1.5 s on the device
turns forming-bar updates off for that strategy. Live prices are for display; the verified backtest remains the only reproducible number
(link to it with `algo.navigate({ backtest: '<slug>' })`).

Live prices alone: `algo.data.subscribe({ symbol: 'BTCUSDT' }, onTick, onFeed)` → `{ id, feed, stop() }`. The host opens a public
exchange WebSocket, **US-reachable venues first** (Coinbase Exchange, Kraken, Binance.US, then the Binance market-data mirror), falls back per
source, then polls and labels it "delayed". Stocks have no free public socket: they show `paused` until the bar closes.

## Open fast: show the verified run first (SDK 2.13+)

`algo.strategy.verified(items, { markers? })` reads each strategy's last VERIFIED position and date from its published backtest: no market data, no run, one request per strategy, up to 8 per call. Resolves `[{ ref, state: { kind: 'snapshot', reason: 'verified', position, dataThrough, markers?, lastSignal? } }]` (`markers: true` adds the run's entries and exits) or `{ ref, error }` per row. Draw it at once, label it "verified <dataThrough>", call `algo.rendered()`, then replace it with the on-device pass (`strategy.live` / `states`) and say which entries come from the device's run (those after `dataThrough`). It rejects on a host that doesn't know it: catch and carry on with the normal path.

## Many strategies at once: `algo.strategy.state` (SDK 2.8+)

`algo.strategy.live` is one session per document. For a **board** of many strategies (the platform app `@algo/signals-board`) use the state-only calls:

```js
const rows = await algo.strategy.states(items, onRow)   // items: [{ ref, inputs? }], any number; chunks of 4 so rows arrive as they finish
const some = await algo.strategy.state(items)           // one call, at most 8 items
// each entry: { ref, state } | { ref, error: { code, message } }
```

`state` = `{ kind: 'state' | 'snapshot', position, provisionalPosition, pendingAtClose, lastSignal: { t, side }, levels, dataThrough, close, runMs, barCloseOnly, reason? }`.
One history pass per strategy, **no feed**, no forming-bar re-runs. The host runs 2 (native) / 3 (web) at once, shares a run between identical rows and
reuses a result until the UTC day rolls; a run over 1.5 s sets `barCloseOnly`. Needs the same `uses.strategy` and `uses.sources` as `strategy.live`.
Per-row errors are the codes in the table below, so one row without a data key never fails the rest. See [recipe-signals-board](https://algoiq.xyz/skills/recipe-signals-board.md).

## Exposing a trigger: `lib.level`

A strategy opts in to showing "how far is the next trigger" by naming a price level on its last bar:

```js
lib.level('next-entry', sma50 * 1.01, { kind: 'info' })   // the price at which it would enter
lib.level('next-exit', stop, { kind: 'info' })
```

Names `next-entry` / `next-exit` are the convention; the chip shows `(level − price) / price`. Strategy plots and `lib.level` come with the plots
work (#757): until the core returns `levels`, the update's `levels` is empty and the chip simply has no trigger row. Write the strategy the
way you will once it lands.

## Errors

| `err.code` | Meaning |
| --- | --- |
| `forbidden` | The manifest has no `uses.strategy` |
| `params` | Bad `ref`, an undeclared source, or an extra field (the throttle is not yours to set) |
| `locked` | Paid code the reader may not run |
| `timeout` / `run_failed` | The strategy took too long or threw on this device |
| `rate_limited` | More than 3 subscriptions, too many calls, or (state) more than 40 strategies waiting |
| `source_key` / `source_web` / `source_unavailable` | The strategy's data is not reachable from this device: `live` answers a `snapshot` update, `state` a `snapshot` state (the last verified run), or this error when it was never verified |
