ChartnautDocs

Scripts

Any script a Chartnaut chart can run, your code can add: a built-in indicator by name, a definition or insight from the user's library, your app's own scripts, or source code you send, in Chartnaut's scripting language or Pine Script.

Add one

import { createTerminal } from "@chartnaut/charts";

const terminal = createTerminal(document.getElementById("terminal")!, {
  charts: { main: { symbol: "NAS100", timeframe: "15m" } },
});
const main = terminal.chart("main");

async function addScripts() {
  const rsi = await main.scripts.add("RSI", { length: 14 }); // a built-in, by name
  const breakout = await main.scripts.add({ id: "flw_4" }); // a definition from the library
  const mid = await main.scripts.add({ source: '//@version=5\nindicator("Mid")\nplot(hl2)', language: "pine" });

  await rsi.update({ inputs: { length: 21 } });
  await mid.hide();
  breakout.on("event", (e) => console.log("Breakout at", e.data.start_time));
}
addScripts();

What add takes:

You pass

What it adds

"RSI"

A built-in, by name

"ind_61", { id: "flw_4", version: 3 }

A script from the user's library or your app, by id (ind_ indicators, flw_ definitions, ins_ insights), optionally pinned to a version

{ source, language: "pine" }

Pine Script v5 or v6, run without saving

{ source }

Chartnaut code, run without saving

add resolves with a script handle once the script is drawn. { wait: "settled" } waits until it has calculated its whole history. Someone else's script from the community opens Chartnaut's consent sheet first, so add waits for the person's answer.

On the handle

What it does

update({ inputs, style, profile })

Changes its settings; only what you pass changes

hide(), show(), remove()

values({ from, to }), values({ at })

Its outputs over a window, or at one moment

ready({ wait: "settled" })

Resolves once it's ready, or settled

on("event" | "ready" | "error" | "change" | "removed")

Its events

status, instance, inputs, visible

Where it is now. instance is its si_ id on this chart

In React

useScript adds a script when the component mounts, updates its inputs when they change and removes it when the component goes:

import { useState } from "react";
import { Ready, useChart, useScript, useTerminal } from "@chartnaut/charts/react";

export function RsiControl() {
  const terminal = useTerminal();
  const main = useChart(terminal, "main");
  const [length, setLength] = useState(14);
  const rsi = useScript(main.chart, "RSI", { length });

  return (
    <Ready on={rsi} fallback={<span>Adding RSI…</span>}>
      <input type="number" value={length} onChange={(e) => setLength(Number(e.target.value))} />
    </Ready>
  );
}

Read the values

import { createTerminal } from "@chartnaut/charts";

const terminal = createTerminal(document.getElementById("terminal")!, {
  charts: { main: { symbol: "NAS100", timeframe: "1h" } },
});

async function lastRsi() {
  const rsi = await terminal.chart("main").scripts.add("RSI", { length: 14 }, { wait: "settled" });
  const out = await rsi.values({ from: 1789400000, to: 1789471800 });
  const points = out.series[0]?.points ?? [];
  console.log("Last RSI", points[points.length - 1]?.value);
}
lastRsi();

values answers series (an indicator's lines), events (a definition's), markers and layers, never past the replay clock. In a backtest, the script's numbers stop where the replay is: nothing from the future reaches your code.

React to events, and hold the replay

A definition fires event each time its setup happens on a closed bar. In a backtest, { hold: true } pauses the replay on that bar until your handler is done, so a drill can ask a question before the next bar:

import { createTerminal } from "@chartnaut/charts";

const terminal = createTerminal(document.getElementById("terminal")!, {
  charts: { main: { symbol: "NAS100", timeframe: "5m" } },
});

async function drill() {
  const breakout = await terminal.chart("main").scripts.add({ id: "flw_4" });
  breakout.on(
    "event",
    async (e) => {
      const answer = await askTrader("Breakout. Long, short or wait?");
      await saveToJournal({ at: e.data.start_time, answer });
    },
    { hold: true },
  );
  await terminal.replay.play({ speed: 4 });
}
drill();

Only script events and fills can hold. If your handler throws, the replay stays paused and the error goes to the console. The terminal gives up after 60 seconds and stays paused. In a partner session the hold needs the session's page_control to allow the replay.

terminal.scripts.on("event", ...) hears every script on every chart; chart.scripts.on("added", ...) hears the scripts the person adds themselves.

Check and save your own code

import { createTerminal } from "@chartnaut/charts";

const terminal = createTerminal(document.getElementById("terminal")!, {});
const source = '//@version=5\nindicator("Mid")\nplot(hl2)';

async function check() {
  const result = await terminal.scripts.validate(source, "pine");
  console.log(result);
  await terminal.scripts.save(source, "Mid", "pine"); // into the person's library, private
}
check();

Your app's scripts

Indicators, definitions and insights you publish to your app show in the script picker under your app's name, only inside your app, and never in the community. You can keep their source hidden: they run, but nobody can open them. Publish them from your server with cn.scripts.publish (@chartnaut/node) or the partner API's /scripts routes. Any developer plan can publish them.

cn.scripts.publish({ script }) takes the script's id. chartnaut ls lists it, and chartnaut push prints it after a save:

chartnaut push ./acme-vwap
# pushed acme-vwap@1
# id: ind_1298