ChartnautDocs

The frame and embed.js

The frame is Chartnaut's own backtest screen, in an iframe on your page, held to one session. This page covers the messages underneath; the Terminal SDK section covers everything you can do with it from your code. Add it with the @chartnaut/charts package:

npm install @chartnaut/charts
import { Ready, Terminal, useTerminal } from '@chartnaut/charts/react';

function Backtest({ session }: { session: string }) {
  const terminal = useTerminal();
  return (
    <div style={{ height: 800 }}>
      <Ready on={terminal.status} fallback={<p>Opening…</p>}>
        <button onClick={() => terminal.send('open_panel', { panel: 'trades' })}>Trades</button>
      </Ready>
      <Terminal
        ref={terminal.ref}
        getFrameUrl={({ refresh }) => askYourServerForFrameUrl(session, refresh)}
        interface={{ drawing_rail: false }}
        onSessionEnded={(e) => refreshBoard(e.data.session_id)}
        onError={(e) => !e.refreshing && showProblem(e.data.reason)}
      />
    </div>
  );
}

Without React, createTerminal does the same:

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

const terminal = createTerminal(document.getElementById('chartnaut')!, {
  getFrameUrl: ({ refresh }) => askYourServerForFrameUrl('ses_90211', refresh),
  timezone: 'America/New_York',
});
terminal.send('set_timeframe', { timeframe: '15m' }); // waits until the terminal is ready
const off = terminal.on('session_ended', (e) => refreshBoard(e.data.session_id));

The terminal fills its parent, so give the parent a height. With dev: true a parent with no height gets a console warning.

Without a build step, load the same code from a script tag. It puts Chartnaut.createTerminal on window:

<script src="https://terminal.chartnaut.com/embed.js"></script>
<div id="chartnaut" style="height: 800px"></div>
<script>
  var terminal = Chartnaut.createTerminal(document.getElementById('chartnaut'), {
    frameUrl: frameUrlFromYourServer   // see Two ways in, below
  });
  terminal.on('session_ended', function (e) { refreshBoard(e.data.session_id); });
  terminal.on('needs_link', function () { showConnectButton(); });
</script>

ChartnautFrame, useChartnautFrame and Chartnaut.embed / embed() from before still work. embed() is the plain channel underneath: it sends commands at once and its on() returns the frame, for chaining. React 18 or later is needed only for @chartnaut/charts/react.

Phones are not supported: the frame says "Your app's backtesting works on desktop", with your app's name, and sends device_unsupported.

Load options

createTerminal and <Terminal> send these to the frame as it opens, before it says ready. Nothing is saved.

Option

What it does

interface

Switches for everything around the charts, such as { drawing_rail: false }. Left out: the normal screen

appearance

Your look: a theme recipe ({ preset: 'Default' }) and anything a chartbook sets. It sits on top of the user's own look and is never saved to their chartbook

timezone

The charts' time zone in this frame only, such as America/New_York. The user's own setting is untouched

locale, numberFormat

How numbers are written, such as locale: 'en-GB', numberFormat: { thousandsSeparator: 'space' }

dev

Dev mode: ignored options and switches are named in the console, every command, reply and event is logged, and errors carry a fix line

layout, charts, panels, persist, workspace and dateFormat are taken now and used once charts can be named by id. Until then the frame lists them as ignored.

In React, interface and appearance are live props: change one and only what changed goes to the frame. A re-render with the same values sends nothing. A switch you take out of interface keeps its value, so set it to true or false instead.

Ready and status

Commands you send before the terminal is ready wait, then run in order once it is. After 30 seconds without ready they fail with not_ready. ping and get_session go at once.

terminal.ready is a promise of the ready event. terminal.status is { status, ready, settled, reason }: booting while it opens, ready once it can take commands, refused or error with a reason you can show the user, such as a phone or a plan without backtesting. In React, useTerminal() re-renders when it changes, and <Ready on={terminal.status}> shows its fallback until then.

When the frame reloads, for example after a new frame token, the terminal says hello again, gets its load options again, and your waiting commands run after the new ready.

Pass getFrameUrl instead of frameUrl and the frame looks after an expired token. With neither, the package asks your own server's /api/chartnaut/frame for { "frame_url": "…" }. When it reports needs_link or an error with reason signed_out, the SDK calls getFrameUrl({ refresh: true }) and reloads the frame with the new address. Drop your cached token on that call. It does this once, and not again until the frame has sent ready, so a refused token can't loop. The event that set it off arrives with refreshing: true.

Two ways in

First time. The user isn't linked yet. Make a link and pass its frame_url:

Chartnaut.createTerminal(el, { frameUrl: link.frame_url });

The frame shows Sign in with Chartnaut and nothing else. The user signs in and approves in a new window, and the frame opens the session.

Every later open. The user is linked and granted frame:instant. Your server asks for their frame token, naming the session to open:

curl -X POST https://api.chartnaut.com/partner/v1/users/ext:user-8812/frame-token \
  -H "Authorization: Bearer cn_partner_live_XXXX" \
  -H "Idempotency-Key: ft-user-8812-ses_90211" \
  -H "Content-Type: application/json" \
  -d '{ "session": "ses_90211" }'
{
  "frame_token": "cnft_…",
  "expires_at": "2026-11-18T09:12:00Z",
  "frame_url": "https://terminal.chartnaut.com/embed/yourname?api=https%3A%2F%2Feu-api.chartnaut.com&session=ses_90211#ft=cnft_…",
  "api_host": "https://eu-api.chartnaut.com"
}

Pass frame_url to createTerminal. A frame token lasts 24 hours, and asking again returns the same one while it has more than an hour left, so cache it. The frame opens straight away, with no window.

api_host is the API host the user linked through, so the frame reaches their region; frame_url already carries it as api=. You can also pass the parts, { partner: 'yourname', session: 'ses_90211', frameToken: 'cnft_…' }, but that address has no api=, so the frame uses the host it last signed in on. Prefer frame_url.

Without the script

Render the iframe yourself:

<iframe
  src="https://terminal.chartnaut.com/embed/yourname?api=https%3A%2F%2Feu-api.chartnaut.com&session=ses_90211#ft=cnft_…"
  allow="clipboard-write; fullscreen"
  style="border: 0; width: 100%; height: 800px"></iframe>

The token goes after #ft=. A fragment is never sent to a server, so it stays out of logs and referrers.

Where it can run

Only on the origins you list in the portal under Settings → Frame origins, such as https://app.yoursite.com. Every other site is refused by the browser. The frame keeps its own sign-in, separate from the user's normal Chartnaut sign-in, and it only exists inside frames on your site.

Chrome, Edge, Firefox and Safari 26.2 or later are supported. On older Safari the frame offers Open in Chartnaut in a new tab.

Events sent to your page

The frame posts messages to your page only, never to any other origin. With the package or the script, terminal.on('order_filled', fn) hands you { type: 'order_filled', data: { session_id: 'ses_90211', position_id: 'pos_7', order_id: 'ord_9', … } }; terminal.on('*', fn) gets every event. on returns a function that stops listening, and terminal.once hears the next one only. Without it, check that event.origin is https://terminal.chartnaut.com, event.source is your iframe, and event.data.source is chartnaut. Like webhooks, events are hints: read the session for the facts.

Every event looks like this:

{ "source": "chartnaut", "type": "order_filled", "v": 1, "boot": "b_8f2c1a9d0e", "seq": 412,
  "event_id": "evt_8f2c1a9d0e_412", "time": 1789471800,
  "session_id": "ses_90211", "position_id": "pos_7", "order_id": "ord_9" }
  • boot is one id per frame load. A new boot means the frame reloaded.

  • seq counts 1, 2, 3… within a boot with no gaps, so a jump means you missed an event. event_id is unique: drop an event you have already seen.

  • time is when it happened, in unix seconds. In a backtest it is the replay's time.

  • Ids are strings with a prefix (ses_, pos_, ord_, brk_). Store and compare them; don't parse them.

The sign-in step sends hello, needs_link, signed_in, device_unsupported and its own error, with session_id when the frame's address named a session. Once the session is open, the backtest screen sends the rest.

Event

When

Fields

hello

The frame loaded. Always the first event of a boot

page: embed (sign-in) or charts

needs_link

No usable token and no usable link: show your connect button and make a new link

signed_in

The user approved in the window and the frame signed in

device_unsupported

Opened on a phone

ready

The session is open

session_id, commands: the commands this frame takes

order_placed

An order was placed: entry, stop or target

session_id, position_id, order_id

order_changed

An order's price moved

session_id, position_id, order_id

order_cancelled

An order was cancelled

session_id, position_id, order_id

order_filled

An order filled: entry, stop or target

session_id, position_id, order_id; with sessions:read, price and size

position_closed

A position closed, all or part

session_id, position_id, partial; with sessions:read, exit_price, size and realized_pnl

account_changed

Balance or equity moved, at most once a second

session_id, at: replay time in unix seconds; with sessions:read, balance, equity and open_pnl

rule_breached

A rule breach the server recorded

session_id, breach_id, rule_type, action, at: the bar's replay time

session_locked, session_unlocked

You locked or unlocked the session

session_id

session_ended

The session ended

session_id, status: ended, breached or completed_to_target

session_opened_elsewhere

The user opened the same session in Chartnaut, so this frame stopped driving it

session_id

error

The frame can't go on

see below

For one trade the order is always order_placed, then order_filled, then position_closed.

Prices, P&L and balances go in events only when the user granted you sessions:read. Without it, order_filled, position_closed and account_changed carry ids only. A price is a number on the market's tick grid; size is { "quantity": 2, "unit": "contracts" } (lots, contracts or units); P&L and balances are { "amount": 412.5, "currency": "USD" }.

When the frame reloads, the package and the script re-read the session with get_session and send resynced, with previous_boot, missed (true when events were seen to be missing) and session, the get_session answer. Nothing is replayed: read the session from your server if you need the numbers.

The names before November 2026 (fill, order, close, account, breach, session_end, locked, unlocked, continued_elsewhere) still work in terminal.on(). They listen to the new names and warn once in the console. The frame itself sends only the new names.

An error from the sign-in step carries code only when this browser can't keep the frame's sign-in: partitioned_unsupported (Safari before 26.2) or cookie_not_stored. The frame then offers Open in Chartnaut. Its other errors, such as frames being paused for you, carry no code.

An error from the backtest screen carries reason:

reason

What happened

signed_out

The frame's sign-in ended. Get a new frame token

wrong_mode

The session isn't a backtest this screen opens

before_history

The session's day is before the history the user's plan includes

invalid_session

No session, or one this account can't open

no_access

The user's plan doesn't include backtesting, or the address asked for mode=live and your app has no live access

error

Anything else

Commands to the frame

Your page can change what the frame shows: open a panel, switch the timeframe, show another of the session's markets. terminal.send(name, args) returns a promise of the result, and rejects with a ChartnautCommandError whose code says why:

const session = await terminal.send('get_session');      // { session_id: 'ses_90211', status: 'active', ready: true }
await terminal.send('set_timeframe', { timeframe: '15m' });
await terminal.send('set_symbol', { symbol: 'NAS100' });
await terminal.send('open_panel', { panel: 'journal' });

Command

Args

Result

ping

none

version, page (embed before the session opens, charts after), ready, boot, commands

get_session

none

session_id, status, ready. Ids and status only, never money. get_state is its old name and answers the same

open_panel

panel: trades, journal or none

panel. Opens the frame's Trades or Journal panel, or closes it

set_timeframe

timeframe: 1m, 5m, 15m, 30m, 1h, 2h, 4h, 1d or 1w

timeframe, changed. Sets the first chart's timeframe

set_symbol

symbol: one of the session's markets, such as NAS100

symbol, changed. Shows it on the first chart. focus_instrument with instrument is its old name and still works

ping and get_session work as soon as the frame's page has loaded. The others need the session open: createTerminal and <Terminal> hold them until ready. Sent with embed() before then, they answer not_ready, or time out while the frame is still loading a page. set_symbol takes only the markets you created the session with, and is refused in a blind session.

code

Why

unknown_command

No command by that name

invalid_args

A missing or wrong arg, such as a timeframe the chart doesn't offer. Args the frame doesn't know are ignored

not_ready

The session isn't open in the frame yet

not_allowed

Not for this session: another market, a blind session, or the session is open on another page

rate_limited

More than 20 commands in a second

failed

The frame couldn't do it

timeout

No answer within 5 seconds (set timeoutMs to change it)

Every error also has a message to read, reason where the code alone doesn't say why, and docs, a link to what the code means. In dev mode it has fix, one line on what to change. Check a code in either spelling: e.is('notAllowed') or e.code === 'not_allowed'.

Commands only change the screen. Nothing sent from your page can trade, move or play the replay, lock or end a session, change the user's settings, or show what a blind session hides. Do those from your server with the sessions API.

Without the script, post to the iframe yourself, with the terminal as the target origin:

iframe.contentWindow.postMessage(
  { source: 'chartnaut-partner', v: 1, type: 'command', id: 'cmd-1', name: 'open_panel', args: { panel: 'trades' } },
  'https://terminal.chartnaut.com'
);

The frame answers to your page's origin with { source: 'chartnaut', type: 'reply', id: 'cmd-1', ok: true, result }, or ok: false with error: { code, message, docs }. Send init with your load options right after the frame's hello, as the package does. It takes a command only from the window that holds the frame, on one of your frame origins, in exactly this shape. Anything else gets no answer.

What the user sees

Chartnaut's backtest screen in the user's own theme, with Powered by Chartnaut on the chart. It stays on the one session: no app navigation, session switcher, broker accounts or upgrade prompts. When you lock a session the controls freeze and show who locked it; when it ends, a summary card shows the result.