Docs·TapeScript

Language

TapeScript syntax — variables, functions, control flow, operators, numbers, strings, arrays and objects, and the JavaScript features that are not supported.

TapeScript uses a subset of JavaScript syntax. Code outside the subset is reported as an error with a line number before the script starts.

Program structure

A script contains variable declarations, function declarations and top-level statements. Top-level statements run once when the script starts. Functions named onTrade, onDepth, onBbo and onInterval are called on market events. See Events.

JavaScript
// @name Delta Counter

let delta = 0
const line = series("Delta", { pane: "bottom" })

function onTrade(price, size, side, time) {
  if (side === BUY) delta += size
  else if (side === SELL) delta -= size
  line.add(time, delta)
}

Variables

KeywordRules
letCan be reassigned.
constMust have a value; cannot be reassigned. The contents of a const array or object can still change.
  • Top-level variables keep their value between events for as long as the script runs.
  • Variables declared in a function or block are local to it.
  • A name can be declared once per scope and cannot reuse a built-in name (size, max, book, …).

Functions

JavaScript
function bps(a, b) {
  return (a - b) / b * 10000
}
  • Functions are declared at the top level only, in any order.
  • A function can use its parameters, its local variables and all top-level variables.
  • Missing arguments are undefined. Extra arguments are ignored.
  • A line break directly after return ends the statement.

Control flow

JavaScript
if (size > big) { … } else if (size > medium) { … } else { … }

for (let i = 0; i < levels.length; i++) { … }

while (queue.length > 0) { … }

break and continue work in for and while loops. The conditional operator cond ? a : b is supported.

Types

TypeExampleNotes
Number42, 0.25, -1e364-bit floating point
String"BUY", 'wall'.length, s[0], join with +; up to 10,000 characters
Booleantrue, false
null / undefinednullA missing object key reads as undefined
Array[1, 2, 3]Zero-based; up to 10,000 items
Object{ price: 1, size: 2 }String keys; up to 10,000 keys

Array methods: push(x, …), pop(), shift(), indexOf(x), includes(x), slice(start, end), .length.

Object helpers: keys(), has(), remove(), size() — see Built-ins.

Example — total size per price:

JavaScript
const hits = {}

function onTrade(price, size) {
  const k = "" + price
  hits[k] = (hits[k] || 0) + size
}

Operators

KindOperators
Arithmetic+ - * / % **
Comparison< > <= >=
Equality=== !==; == and != behave the same as === and !==
Logical&& || ! ??
Assignment= += -= *= /= %= **= ++ --

Arithmetic and comparison operators take numbers. + also joins strings. Using an array or object in arithmetic is an error.

Not supported

class, arrow functions (=>), new, spread (...), template strings, regular expressions, this, try / catch / throw, import, and browser or Node globals. Only the built-ins are available; any other name is reported as Unknown name.

Comments

// line comments and /* … */ block comments. Comment lines that start with // @ are directives.