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
- Compute what you draw from the bars (series with one value per bar), or record single values inside your loop with
i. - Call
lib.plot,lib.plotShape,lib.hline,lib.band,lib.bgcolororlib.level(backtest). - Submit and verify as usual (recipe-backtest-verify). AlgoIQ's run computes the plots.
- Show it in a post:
{ "ref": "@algo/backtest-chart", "params": { "strategy": "<id>@v1" } }(widgets).
The calls, REST
Submit the strategy (the code that calls lib.plot is in the example below):
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), with code set to the module above:
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.