Scripting Guide: Getting Started
How to write your own scripts in the Script Engine panel - draw indicators, signals, zones and labels on your chart, read live and historical market data, and trigger alerts. Scripts are plain JavaScript, and every feature is available through a single global object called TD.
Quick Start
Have a script drawing on your chart in under a minute
NIFTY 50 on a 5-minute timeframe.// A 20-period simple moving average, drawn on the chart const LENGTH = 20; let closes = [], lastTime = null; TD.onChartBar = (bar) => { // The newest bar is still forming and arrives again each time it moves if (bar.time === lastTime) closes[closes.length - 1] = bar.close; else { closes.push(bar.close); lastTime = bar.time; } if (closes.length > LENGTH) closes.shift(); if (closes.length < LENGTH) return; const sma = closes.reduce((a, b) => a + b, 0) / LENGTH; TD.Chart.plot("SMA20", sma, { color: "#2962FF" }); }; TD.onReset = () => { closes = []; lastTime = null; }; TD.onStart = () => { TD.log("SMA started on " + TD.Chart.getSymbol()); };
SMA started on NIFTY 50.The newest candle is still forming, so onChartBar receives it again every time it moves. The script above replaces that candle's value instead of adding it twice - see Lifecycle Hooks & Chart Bars.
The Script Engine Panel
What each control does, and how the editor helps you as you type
TD.onStop firstTD.log output, errors and engine notices, grouping repeated lines with a counterType TD. for autocomplete on every function, and hover one to read its description. Snippets cover common patterns like TD.onChartBar.
Each running script gets its own colour. Anything you don't colour yourself is drawn in it, and the chart shows a small badge naming each script drawing on it.
Symbol mismatch warning. If your code doesn't mention the symbol on the active chart, the panel asks you to confirm before running - a script fetching RELIANCE data would otherwise silently draw on a NIFTY 50 chart. Click Run Anyway if that's intended, or tick Do not ask again.
Ready-made Templates
Open Script → Templates - a working starting point for the most common scripts
A template that calls a data API still needs that API in your plan - see Plan Access.
How a Script Runs
Knowing the order of events helps you avoid most mistakes
Run clicked
|
+- 1. The top-level code runs once (declare variables, CONFIG, functions, hooks). Top-level await is allowed.
+- 2. TD.onChartBar is called once for EVERY bar already on the chart, oldest first (isHistory = true).
| The last bar is still forming, so it arrives with closed = false.
+- 3. TD.onStart() runs
|
+- ... live: TD.onChartBar(bar, -1, false) every time the newest bar moves, and once more when it closes
+- ... live: TD.on("tick"), TD.on("greeks"), TD.on("bidAsk") and other listeners fire for symbols you subscribed
+- ... symbol or timeframe change, or scroll-back for older bars -> TD.onReset(ctx) -> whole history replayed again (step 2)
|
Stop clicked -> TD.onStop() -> script ends
onChartBar, no listeners, no timers and no TD.Indicators subscription means it finishes and shows === Script N Finished ===await with them, anything that fetches data returns a Promiseawait works - you can await outside a function. The history replay waits for it, and live bars that arrive meanwhile are held until the replay is done. The ~1-second start limit applies only to loading the script, not to the time your awaits takeonChartBar receives bars only. For tick events, call TD.Feed.addSymbols([TD.Chart.getSymbol()])The quick data helpers return clean, named values. They throw on failure, so wrap them in try/catch.
try { const bars = await history("RELIANCE", "5m", 50); // top-level await console.log("Last close:", bars.at(-1).close); } catch (err) { console.warn(err.message); }
Start from a template under Open rather than a blank tab - it wires up the lifecycle hooks correctly, and you only have to change the logic in the middle.