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.
// @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
| Keyword | Rules |
|---|---|
let | Can be reassigned. |
const | Must 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
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
returnends the statement.
Control flow
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
| Type | Example | Notes |
|---|---|---|
| Number | 42, 0.25, -1e3 | 64-bit floating point |
| String | "BUY", 'wall' | .length, s[0], join with +; up to 10,000 characters |
| Boolean | true, false | |
| null / undefined | null | A 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:
const hits = {}
function onTrade(price, size) {
const k = "" + price
hits[k] = (hits[k] || 0) + size
}
Operators
| Kind | Operators |
|---|---|
| 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.