Replay, orders and the account
In a backtest your code can run the replay, trade, and read the account. Orders go through the same backtest engine as Chartnaut's own order ticket: the same prices, the same rules. None of this reaches a broker.
Your code can use these in the user's own backtests, and in a partner session whose page_control allows it: "replay" for the replay, "replay_and_orders" for both. Otherwise they are refused with not_allowed and reason: "partner_session". terminal.can("orders.place") and terminal.can("replay.control") say which before you try.
Users can't start a backtest inside your terminal. Your server opens one for them by key: see Users.
The replay
import { createTerminal } from "@chartnaut/charts";
const terminal = createTerminal(document.getElementById("terminal")!, {
charts: { main: { symbol: "NAS100", timeframe: "5m" } },
});
async function openingHour() {
await terminal.replay.jumpTo("2026-09-15T09:30-04:00");
await terminal.replay.play({ speed: 4 }); // bars a second, 0.5 to 10
await terminal.replay.until("2026-09-15T10:30-04:00");
await terminal.replay.pause();
console.log("Stopped at", terminal.replay.time);
}
openingHour();Call | What it does |
|---|---|
| Plays at |
| Steps 1 to 500 closed bars |
| 0.5 to 10 |
| Jumps. Refused with open positions unless |
| Resolves when the replay reaches |
| Where it is now. |
Events: tick (at most 10 a second), play, pause, speed, and end when the session ends. To pause on a script's event until your code is done, see holding the replay.
Orders
import { createTerminal } from "@chartnaut/charts";
const terminal = createTerminal(document.getElementById("terminal")!, {
charts: { main: { symbol: "NAS100", timeframe: "5m" } },
});
async function trade() {
const pos = await terminal.orders.buy("NAS100", { quantity: { quantity: 2, unit: "contracts" }, stop: 21480, target: 21620, clientId: "setup-41" });
pos.on("pnl", (e) => console.log(`${e.data.r}R`));
await pos.moveStop(21500);
await pos.close({ half: true });
// Or let the terminal size it: lose 1% of the balance if the stop is hit.
await terminal.orders.sell("EURUSD", { risk: "1%", stop: 1.095 });
}
trade();buy and sell take a size or a risk:
Option | What it does |
|---|---|
| A size, always with its unit. Order sizes |
| Sized by the terminal so hitting |
| Prices |
| A resting order; market by default |
| A retry with the same id, even after a reload, answers the first order instead of placing another |
Each resolves with a position handle: moveStop, moveTarget, setProtection, close() (all of it, { half: true } rounded down to the size step, or { quantity }), get(), and events pnl (at most 4 a second), fill and closed.
Also on terminal.orders: place (the raw order), modify, cancel, cancelAll (resting entries; stops and targets stay), closeAll, list, position(id) for a position you already have the id of, and events placed, fill, changed, cancelled, closed, locked, unlocked and refused. await terminal.orders.next("fill") resolves with the next one.
Your own risk lock
import { createTerminal } from "@chartnaut/charts";
const terminal = createTerminal(document.getElementById("terminal")!, {});
terminal.account.on("change", (e) => {
const open = e.data.open_pnl?.amount ?? 0;
if (open < -300 && !terminal.orders.locked) void terminal.orders.lock("Daily loss limit reached");
});While locked, every new order is refused with locked_by_app, from your code and from the terminal's order ticket, and the ticket shows your reason. Closing, moving a stop or target and cancelling still work: a lock never keeps someone in a losing trade. unlock() lifts it. The lock lives in the terminal's page, so a reload or another session clears it: lock again on load if you still want it. Rules a partner sets on a session are separate and live on the server.
The account
import { createTerminal } from "@chartnaut/charts";
const terminal = createTerminal(document.getElementById("terminal")!, {});
async function report() {
const account = await terminal.account.get();
const open = await terminal.account.positions();
const closed = await terminal.account.trades({ from: 1789400000 });
console.log(account, open.length, "open,", closed.length, "closed");
}
report();account.get() reads the balance, equity and open P&L. positions() and trades() read once; watchPositions (or usePositions in React) keeps the open positions live, with each position's P&L merged in as it moves. Everything is cut at the replay clock.
Prices and P&L reach your code only when the user granted the read permission. Without it, events carry ids only.
An auto-journal
import { createTerminal } from "@chartnaut/charts";
const terminal = createTerminal(document.getElementById("terminal")!, {
charts: { main: { symbol: "NAS100", timeframe: "5m" } },
});
terminal.orders.on("closed", async (e) => {
const image = await terminal.screenshot({ chart: "main" });
await saveToJournal({ trade: e.data, image: image.image });
});Events are hints. For a record that matters, run the backtest as a session your server creates (cn.sessions.create) and read the trade with cn.sessions.trades(id) once the event arrives, or when session.ended reaches your webhook. A backtest your page opens by key belongs to the user: your server can't read its trades, so the page's own record is the one you keep.
