Market LabDocs

Orderbook

Normalized orderbook snapshots for Market Lab scripts.

type Orderbook = {
  exchange: string
  symbol: string
  timestamp_ms: number
  bids: OrderbookLevel[]
  asks: OrderbookLevel[]
}

type OrderbookLevel = {
  price: number
  quantity: number
}

Bids and asks are ordered best price first and limited by the configured depth.

export function onData(ctx, input, history) {
  const book = history.source('btc@orderbook@bulkf', 0)
  if (!book) return

  const spread = ctx.study.spread(book)
  const imbalance = ctx.study.imbalance(book, { depth: 20 })

  return {
    metrics: {
      spread_bps: spread.spread_bps,
      imbalance: imbalance.imbalance,
    },
  }
}