TrueData TrueData Sheets
search
Searching...
functions Formulas
menu_book Pages
manage_search Content Match
search_off

No results for ""

Loading Content
code

Custom Scripting

The TrueData Scripting Engine uses JavaScript as its base language. Scripts combine standard JavaScript with the proprietary TD namespace provided by the platform. The environment is event-driven and built for live market data streaming, sheet automation, option chain analysis, historical data retrieval, and real-time dashboards.

How It Works — 4 Steps

1

Register Events First

Call TD.on(...) at the top level of the script — never inside a hook. This is the order the runtime expects.

2

Define onStart

Subscribe symbols with await TD.Feed.addSymbols(...) and write sheet headers inside TD.onStart.

3

React to Events

Handle tick, bar1min, bar5min, bidAsk, and greeks as they stream in.

4

Clean Up in onStop

Always call await TD.Feed.removeSymbols(...) to prevent orphaned subscriptions and memory leaks.

Namespaces

Each namespace below is an object containing multiple related methods, called as TD.Namespace.method().

TD.Feed

Subscribe and unsubscribe symbols to live WebSocket streams. addSymbols, removeSymbols.

TD.Sheet

Read and write cells. Only four methods exist: setCell, setCellRange, getCell, getCellRange.

TD.Historical

Past bars, ticks, gainers/losers. One-shot REST calls, inside onStart only.

TD.OptionChain

Option chain data and Greeks for any expiry. One-shot REST, inside onStart only.

TD.Analytics

Top movers by index, industry, OI, and volume. One-shot REST, inside onStart only.

TD.UI

showAlert(message, type) — surface a success, error, info, or warning toast.

Event Listener

Not a namespace — a single function that registers a callback for a named event. Always called at the top level of the script, never inside a hook.

TD.on(eventName, callback)

Listen to real-time events: tick, bar1min, bar5min, bidAsk, heartbeat, greeks.

Lifecycle Hooks

Properties you assign on TD — the runtime discovers and calls them by name. Not invoked directly by your code.

TD.onStart

Called once when the script starts. Subscribe symbols, fetch initial data, write headers.

TD.onStop

Called once when the script stops. Always unsubscribe symbols added in onStart.

TD.onError

Called on any unhandled runtime error. If undefined, errors are silently discarded.

Utility

A single standalone debug function — not a namespace, not an event, not a hook.

TD.log(...)

Debug output. Appears in the console panel and is preserved in the session log.

Your First Script — Copy, Paste, Click Start

help Paste this into the script editor and click Start. You should see ticks in the console immediately. Notice TD.on('tick', ...) is registered at the top — before any hook is defined.
const SYMBOLS = ["NIFTY", "RELIANCE"];

// Register events at the top level — never inside a hook
TD.on("tick", (data) => {
  // data[0] is the symbolId, NOT the symbol name — see Field Reference
  const ltp = parseFloat(data[2]); // all values arrive as strings
  TD.log("LTP: " + ltp);
});

TD.onStart = async () => {
  try {
    const response = await TD.Feed.addSymbols(SYMBOLS);
    if (!response.success) {
      TD.log("Subscribe failed: " + response.message);
      return;
    }
    TD.log("Subscribed: " + JSON.stringify(response.symbollist));
  } catch (err) {
    TD.log("Start error: " + err.message);
  }
};

TD.onStop = async () => {
  await TD.Feed.removeSymbols(SYMBOLS);
};

TD.onError = (err) => {
  TD.log("Error: " + err.message);
};
warning Before writing anything more complex, read the Strict Rules page — it covers the 16 most common mistakes that cause silent failures.
Truedata Icon Sheets by Truedata

Empowering traders and analysts with high-performance real-time market data directly within their spreadsheets.

© 2026 TrueData Market Data Services LLP. All rights reserved.