Docs·TapeScript

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.

MemberValue
book.bestBid, book.bestAskBest bid / ask price
book.bestBidSize, book.bestAskSizeSize at the best bid / ask
book.midMid price
book.timeTime (ms) of the latest book update
book.tickPrice 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:

JavaScript
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.

MemberValue
orders.countNumber 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
JavaScript
// @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

MemberValue
venue.idVenue id, for example binance, topstepx
venue.hasAggressortrue if trades include the aggressor side
venue.hasDepthtrue if the venue provides order-book data
venue.hasOrderstrue if the venue reports single orders with their id

Constants

ConstantUsed in
BUY, SELL, NONEside of onTrade
BID, ASKside of onDepth and onOrder
ADD, MODIFY, FILL, CANCEL, RESETevent of onOrder
DETECTED, REFILLED, EXHAUSTEDevent 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.

JavaScript
const avg = rolling(200)
function onTrade(price, size) { avg.add(size) }
MemberValue
add(x)Adds a value; removes the oldest when full
clear()Removes all values
sum, mean, min, max, stdOf the values in the window
countNumber of values
fulltrue when it holds n values
lastLatest value

ema(period)

Exponential moving average.

MemberValue
add(x)Adds a value and returns the new average
valueCurrent average
clear()Resets

timeWindow(ms, capacity)

Values from the last ms milliseconds. capacity is optional (default and maximum 10,000 values).

JavaScript
const buys = timeWindow(5000)
function onTrade(price, size, side, time) { if (side === BUY) buys.add(time, size) }
function onInterval(time) { buys.advance(time) }
MemberValue
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, meanOf the values in the window

vwap()

Volume-weighted average price.

MemberValue
add(price, size)Adds a trade
valueCurrent VWAP
volumeTotal size added
reset()Resets

Math

FunctionResult
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

FunctionResult
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

FunctionResult
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.