Built-ins
All TapeScript built-ins — order book, single orders, venue, constants, rolling windows (rolling, ema, timeWindow, vwap), math, objects and time.
This page lists every built-in name. Any name that is not a built-in or declared in the script is an error. Hover a built-in in the editor to see the same information.
Drawing functions (series, marker, box, hline) are on Drawing. alert, sound, speak and log are on Alerts, sound & voice.
Book
book holds the current order book of the chart's symbol.
| Member | Value |
|---|---|
book.bestBid, book.bestAsk | Best bid / ask price |
book.bestBidSize, book.bestAskSize | Size at the best bid / ask |
book.mid | Mid price |
book.time | Time (ms) of the latest book update |
book.tick | Price height of one book row; 0 until the book arrives |
book.sizeAt(price) | Size on the book row that contains price; 0 if none |
sizeAt accepts any price inside a row, so a trade price can be passed directly:
function onTrade(price, size, side, time) {
if (side === SELL && book.sizeAt(price) > size * 3) marker(time, price, "ABS")
}
The book is empty on venues without order-book data.
Orders
orders holds every resting order by its exchange id, on venues that report single orders (market-by-order). An id comes from onOrder or onIceberg.
| Member | Value |
|---|---|
orders.count | Number of resting orders |
orders.has(id) | true while the order rests in the book |
orders.size(id) | Size the order still shows; 0 if gone |
orders.price(id) | Price of the order; 0 if gone |
orders.side(id) | BID or ASK; NONE if gone |
orders.placedAt(id) | Time (ms) the order was first seen |
orders.isIceberg(id) | true if the order refilled after trading |
orders.queueAhead(id) | Size resting ahead of the order at its price |
orders.countAt(price) | Number of orders resting at price |
// @input minSize: float = 50 "Min order size"
function onOrder(event, id, side, price, size, time) {
if (event === ADD && size >= input.minSize && orders.queueAhead(id) === 0) {
marker(time, price, "FRONT " + size)
}
}
orders is empty on venues without market-by-order data.
Venue
| Member | Value |
|---|---|
venue.id | Venue id, for example binance, topstepx |
venue.hasAggressor | true if trades include the aggressor side |
venue.hasDepth | true if the venue provides order-book data |
venue.hasOrders | true if the venue reports single orders with their id |
Constants
| Constant | Used in |
|---|---|
BUY, SELL, NONE | side of onTrade |
BID, ASK | side of onDepth and onOrder |
ADD, MODIFY, FILL, CANCEL, RESET | event of onOrder |
DETECTED, REFILLED, EXHAUSTED | event of onIceberg |
Rolling windows
Use these instead of looping over arrays of past values. Create them at the top level and update them in callbacks.
rolling(n)
The last n values, n from 1 to 10,000.
const avg = rolling(200)
function onTrade(price, size) { avg.add(size) }
| Member | Value |
|---|---|
add(x) | Adds a value; removes the oldest when full |
clear() | Removes all values |
sum, mean, min, max, std | Of the values in the window |
count | Number of values |
full | true when it holds n values |
last | Latest value |
ema(period)
Exponential moving average.
| Member | Value |
|---|---|
add(x) | Adds a value and returns the new average |
value | Current average |
clear() | Resets |
timeWindow(ms, capacity)
Values from the last ms milliseconds. capacity is optional (default and maximum 10,000 values).
const buys = timeWindow(5000)
function onTrade(price, size, side, time) { if (side === BUY) buys.add(time, size) }
function onInterval(time) { buys.advance(time) }
| Member | Value |
|---|---|
add(time, x) | Adds a value; removes values older than ms |
advance(time) | Removes values older than ms without adding one |
clear() | Removes all values |
sum, count, mean | Of the values in the window |
vwap()
Volume-weighted average price.
| Member | Value |
|---|---|
add(price, size) | Adds a trade |
value | Current VWAP |
volume | Total size added |
reset() | Resets |
Math
| Function | Result |
|---|---|
abs(x) | Absolute value |
sign(x) | -1, 0 or 1 |
floor(x), ceil(x), round(x) | Round down, up, to nearest |
sqrt(x) | Square root |
exp(x) | e to the power x |
ln(x) | Natural logarithm |
pow(a, b) | a to the power b |
min(a, b, …), max(a, b, …) | Smallest, largest |
isNum(x) | true for a finite number |
To round to two decimals: round(x * 100) / 100.
Objects
| Function | Result |
|---|---|
keys(obj) | Array of keys |
has(obj, key) | true if the key exists |
remove(obj, key) | Deletes the key |
size(objOrArray) | Number of keys or array length |
Delete keys you no longer need. See Limits.
Time
| Function | Result |
|---|---|
now() | Time (ms) of the latest event the script received |
isHistory() | true while the script replays stored data (see History) |
Inputs
input.<name> returns the value of a // @input. See Inputs & directives.