home Scripting Guide

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

1
Open a chart and pick a symbol
For example NIFTY 50 on a 5-minute timeframe.
2
Open the Script Engine panel
Then click + to add a new script tab and paste the code below.
// A 20-period simple moving average, drawn on the chart
const LENGTH = 20;
const closes = [];

TD.onChartBar = (bar, index, isHistory) => {
 closes.push(bar.close);
 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.onStart = () => {
 TD.log("SMA started on " + TD.Chart.getSymbol());
};
3
Click Run
A blue SMA line appears on the chart and the console shows SMA started on NIFTY 50.
4
Click Stop when you are done
Drawings stay on the chart until you clear them.

The Script Engine Panel

What each control does, and how the editor helps you as you type

Control · What it does
play_arrow Run - starts the script in the current tab on the active chart
stop Stop - stops the script, running TD.onStop first
auto_fix_normal Clear - removes shapes and indicators this script drew, available only while stopped
save Save - saves the script to your account
folder_open Open - opens a saved script or a ready-made template, some of which need Pro/Power
add_box + (new tab) - adds a script tab, double-click a tab to rename it
terminal Console - shows TD.log output, errors and engine notices, grouping repeated lines with a counter
codeCode editor

Type TD. for autocomplete on every function, and hover one to read its description. Snippets cover common patterns like TD.onChartBar.

paletteColours and badges

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.

warning_amber

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.

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)
 +- 2. TD.onChartBar is called once for EVERY bar already on the chart (isHistory = true)
 +- 3. The chart's symbol is subscribed to live data automatically
 +- 4. TD.onStart() runs
 ¦
 +- … live: TD.onChartBar(bar, -1, false) for each new 1-minute bar
 +- … live: TD.on("tick"), TD.on("greeks"), TD.on("bidAsk") and other event listeners fire
 +- … user changes symbol/timeframe → TD.onReset(ctx) → history replayed again (step 2)
 ¦
Stop clicked → TD.onStop() → script ends
Behaviour · What it means
desktop_windows Tied to one chart - it runs on the chart active when you clicked Run, and keeps drawing there even if you switch charts in a multi-chart layout
security Own sandbox - every script runs in a separate background thread, so a slow script can't freeze the charts
stop_circle Stops itself when idle - no onChartBar, no listeners, no timers means it finishes and shows === Script N Finished ===
layers Drawings persist after Stop - use the Clear button to remove them
hourglass_top Many functions are asynchronous - use await with them, anything that fetches data returns a Promise
tips_and_updates

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.