Data Sources
Order Book
Order book snapshots with per-level accessors and native depth helpers.
Legacy Scripting v2
These docs remain available while the v2 engine is supported. For new scripts, use Scripting v3.
data.BOOK exposes the current order book snapshot for the subscribed exchange, symbol, and timeframe.
data.BOOK supports aggregated exchange strings such as "binancef:bybitf".
const book = subscribe(data.BOOK)Functions
Snapshot Metadata
book.unix(offset?) -> number
book.binSize(offset?) -> number
book.numLevels(offset?) -> number
book.numBids(offset?) -> number
book.numAsks(offset?) -> numberTop of Book
book.bestBid(offset?) -> number
book.bestAsk(offset?) -> number
book.mid(offset?) -> numberPer-Level Accessors
book.askPrice(level, offset?) -> number
book.bidPrice(level, offset?) -> number
book.askSize(level, offset?) -> number
book.bidSize(level, offset?) -> numberNative Helpers
book.depth(refPrice, outerPct, opts?, offset?) -> BookDepth | null
book.imbalance(refPrice, outerPct, opts?, offset?) -> BookImbalance | nullBatch Helpers
book.depthMany(refPrice, requests, offset?) -> Array<BookDepth | null>
book.imbalanceMany(refPrice, requests, offset?) -> Array<BookImbalance | null>Parameters:
offset?: optional bar offset on the BOOK subscription clocklevel: zero-based level indexrefPrice: finite reference price greater than0outerPct: percent distance fromrefPrice, finite and>= 0
BOOK Snapshot Accessors Were Removed
book.data() and book.dataView() are deprecated for BOOK subscriptions and now throw a migration error. Use the accessors and native helpers on this page instead.
depth() and imbalance() Options
{
innerPct?: number,
mode?: "cumulative" | "differential",
unit?: "quote" | "base",
distanceDecay?: number
}innerPct: inner exclusion band fordifferentialmodemode:"cumulative"includes everything from the inside out;"differential"excludes the inner bandunit:"quote"reports quote-side value,"base"reports base quantitydistanceDecay: optional weighting factor that reduces farther levels
depthMany() / imbalanceMany() Request Shape
{
outerPct: number,
opts?: {
innerPct?: number,
mode?: "cumulative" | "differential",
unit?: "quote" | "base",
distanceDecay?: number
}
}Both batch helpers share one refPrice across all requests and return results in request order.
Result Shapes
// BookDepth
{
unix: number,
binSize: number,
bidValue: number,
askValue: number,
bidQty: number,
askQty: number,
bidLevels: number,
askLevels: number
}
// BookImbalance
{
unix: number,
bid: number,
ask: number,
ratio: number
}Helpers return null when the requested slot is missing.
Example
//@version=2
indicator("Book Imbalance", false)
const book = subscribe(data.BOOK)
function onBar(index) {
const mid = book.mid()
if (!(mid > 0)) return
const imbalance = book.imbalance(mid, 1.5, {
innerPct: 0.5,
mode: "differential",
unit: "quote"
})
if (!imbalance) return
plotHistogram("Imbalance", imbalance.ratio, {
color: imbalance.ratio >= 0 ? color.teal : color.orange
})
}