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 |
| Your own fetch. |
| 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 |
|---|---|
| A preset ( |
| Each chart by your own id: |
| Panels by id: |
| Switches for everything around the charts, such as |
| A theme preset and your own colours |
| Where the layout is kept: |
| How numbers and times show, for this terminal only |
| Logs every command, reply and event, names ignored options and adds a |
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 |
|---|---|
| One chart's |
|
|
| Adds a script on mount, updates its inputs when they change, removes it on unmount |
| The newest live quote, at most once a frame (live terminals only) |
|
|
|
|
| Open positions, each with its P&L as it moves |
| The latest fills |
| The save status with |
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.
