ChartnautDocs

React

@chartnaut/charts/react gives you one component, <Terminal>, and hooks that read the terminal as React state. React 18 or later. It works in Next.js (as a client component) and in plain Vite.

<Terminal>

"use client";
import { Terminal, useTerminal } from "@chartnaut/charts/react";

export function Desk() {
  const terminal = useTerminal();
  return (
    <div style={{ height: 720 }}>
      <Terminal
        ref={terminal.ref}
        layout="2h"
        charts={{ main: { symbol: "NAS100", timeframe: "5m" }, htf: { symbol: "NAS100", timeframe: "4h" } }}
        interface={{ drawing_rail: false }}
        appearance={{ preset: "Carbon" }}
        timezone="America/New_York"
        onFill={(e) => console.log("Filled", e.data.order_id)}
        onSessionEnded={(e) => console.log("Session over", e.data.status)}
      />
    </div>
  );
}

It fills its parent, so the parent needs a height. With dev on, a parent with no height gets a warning in the console.

Where the terminal gets its address:

Prop

What it does

none

Asks your own server at /api/chartnaut/frame, which frameRoute from @chartnaut/node answers (Users)

getFrameUrl={({ refresh }) => ...}

Your own fetch. refresh is true when the terminal refused the last address's sign-in and needs a fresh one

frameUrl

A frame address you already have

What it opens with. Change layout, charts, interface or appearance later and the terminal follows: the SDK sends only what changed, as one change. The other props are read when the terminal opens.

Prop

What it sets

layout

A preset ("1", "2h", "2v", "3", "1+2", "1+3", "2x2", "2+3", "3x2", "2x4") or your own split tree. Layout and charts

charts

Each chart by your own id: { main: { symbol, timeframe, type } }

panels

Panels by id: { book: { kind: "order-book" } }

interface

Switches for everything around the charts, such as onlyCharts

appearance

A theme preset and your own colours

persist

Where the layout is kept: "device", "account", "none" or { load, save }. Workspace and persist

locale, timezone, numberFormat

How numbers and times show, for this terminal only

dev

Logs every command, reply and event, names ignored options and adds a fix line to errors

Events as props: onReady, onFill, onPositionClosed, onBreach, onSessionEnded, onError and onEvent (every event). Changing a callback never reloads anything.

The terminal handle

useTerminal() gives you the terminal: pass its ref to <Terminal>, and use it anywhere in the component. It has everything the terminal object has, and re-renders the component when its status moves from booting to ready. Calls you make before the terminal is ready wait for it.

Hooks

Each hook reads once the terminal is ready, then follows the terminal's events. Nothing polls, and a hook re-renders only when its own part changes.

Hook

Gives you

useChart(terminal, id?)

One chart's symbol, timeframe, type, status and its handle. No id: the chart in focus

useLayout(terminal)

charts (ids in layout order), focusedChart, panels, the tree

useScript(chart, source, inputs?)

Adds a script on mount, updates its inputs when they change, removes it on unmount

useQuote(terminal, symbol)

The newest live quote, at most once a frame (live terminals only)

useReplay(terminal)

time, playing, speed and the replay's controls

useAccount(terminal)

balance, equity, openPnl

usePositions(terminal)

Open positions, each with its P&L as it moves

useFills(terminal, { last })

The latest fills

useWorkspace(terminal)

The save status with persist={{ load, save }}

import { useChart, useLayout, useTerminal } from "@chartnaut/charts/react";

export function ChartTabs() {
  const terminal = useTerminal();
  const { charts, focusedChart } = useLayout(terminal);
  const focused = useChart(terminal);

  return (
    <nav>
      {charts.map((id) => (
        <button key={id} aria-pressed={id === focusedChart} onClick={() => terminal.chart(id).focus()}>
          {id}
        </button>
      ))}
      <span>{focused.symbol}</span>
    </nav>
  );
}

Hooks take the terminal from useTerminal(). To use one in a child component, pass the terminal down as a prop or through your own context.

Waiting for things to be ready

Every handle and hook carries a status: booting, loading, calculating, ready, or refused and error with a reason. <Ready> shows its children once everything listed is ready, a fallback before, and the reason when something fails:

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

export function RsiReadout() {
  const terminal = useTerminal();
  const main = useChart(terminal, "main");
  const rsi = useScript(main.chart, "RSI", { length: 14 });

  return (
    <Ready on={[main, rsi]} settled fallback={<span>Calculating…</span>}>
      <button onClick={async () => console.log(await rsi.values({ at: Math.floor(Date.now() / 1000) }))}>
        Read RSI
      </button>
    </Ready>
  );
}

settled waits until the numbers won't shift any more: the script has calculated its whole history.

Next.js

Use <Terminal> in a client component ("use client"). On the server it renders an empty box and loads the terminal in the browser. The route it asks for its address is one file: see Getting started.

Without React

Everything here has a plain JavaScript version: createTerminal(element, options) returns the same terminal object. See The terminal object.