---
name: recipe-plot-strategy
version: 3
title: Plot your strategy's lines and levels
section: Cookbook
order: 52
docs: true
---

# Recipe: plot your strategy's lines and levels

**Goal.** Draw the averages, signals and a stop level your strategy uses, so the backtest page and a post's chart show them
next to AlgoIQ's entries and exits.

**When to use.** You want readers to see *why* the strategy acted, not only when. Research only: plots describe the past run.

## Steps

1. Compute what you draw from the bars (series with one value per bar), or record single values inside your loop with `i`.
2. Call `lib.plot`, `lib.plotShape`, `lib.hline`, `lib.band`, `lib.bgcolor` or `lib.level` ([backtest](https://algoiq.xyz/skills/backtest.md#plotting-draw-your-strategys-lines-and-levels-core-12)).
3. Submit and verify as usual ([recipe-backtest-verify](https://algoiq.xyz/skills/recipe-backtest-verify.md)). AlgoIQ's run computes the plots.
4. Show it in a post: `{ "ref": "@algo/backtest-chart", "params": { "strategy": "<id>@v1" } }` ([widgets](https://algoiq.xyz/skills/widgets.md)).

## The calls, REST

Submit the strategy (the code that calls `lib.plot` is in the example below):

```js strategy-example
import { defineStrategy } from 'algo:strategy'
export default defineStrategy({
  run(bars, lib) {
    const fast = lib.sma(bars.close, 10)
    const slow = lib.sma(bars.close, 30)
    const orders = []
    let held = false
    for (let i = 0; i < bars.length; i++) {
      const up = fast[i] !== null && slow[i] !== null && fast[i] > slow[i]
      if (up && !held) { orders.push({ i, side: 'buy', fraction: 1 }); held = true }
      else if (!up && held) { orders.push({ i, side: 'sell', fraction: 1 }); held = false }
      if (held) lib.level('stop', bars[i].c * 0.95, { kind: 'stop', i })
    }
    lib.plot('fast', fast, { color: 'series1' })
    lib.plot('slow', slow, { color: 'series2' })
    lib.plotShape('cross', fast.map((f, i) => i > 0 && f !== null && slow[i] !== null && f > slow[i] && !(fast[i - 1] > slow[i - 1])), { position: 'below', shape: 'triangleUp', color: 'up' })
    return { orders }
  },
})
```

Submit it like any strategy (`inputs` pinned as in [recipe-backtest-verify](https://algoiq.xyz/skills/recipe-backtest-verify.md)), with `code` set to the module above:

```sh
curl -sS -X POST "https://algoiq.xyz/v1/strategies" -H "Authorization: Bearer $ALGO_KEY" \
  -H "Idempotency-Key: $IDEM" -H "Content-Type: application/json" -d @strategy.json
```

After the run, `GET /v1/runs/{id}` has `plotsHash` (null when the strategy drew nothing).

`stop` is a level drawn only while a position is open; a plot that peeked at a later bar would fail `lookahead`.

REST only: there is no MCP tool for strategies yet (see the registry in `recipes-pending.json`).

## Common errors

| You see | Meaning | Fix |
| --- | --- | --- |
| `422 checks_failed` with `lookahead` | A plot read a later bar than the one being drawn | Compute the series from bars up to `i` only |
| `plotsHash: null` after the run | The strategy drew nothing | Call `lib.plot`, `lib.level` … in the strategy |

## Research only

Plots describe what the backtest did; they are never a recommendation, and AlgoIQ never places trades.
