Builtins: level 2 depth of book

The aggregated book, the same book a heatmap draws, as one snapshot per bar: the newest book the chart recorded inside that bar. Levels are best first: bids descending from the top of the book, offers ascending, so index 0 is always the inside market. Levels with nothing resting are left out.

SignatureReturns
l2.supported() -> boolWhether depth is wired on this chart
l2.available() -> boolWhether a book snapshot landed inside this bar
l2.time() -> floatSnapshot timestamp, epoch milliseconds
l2.bid_count() -> intBid levels in the snapshot, 0 without one
l2.ask_count() -> intOffer levels in the snapshot
l2.bid_price(index: int) -> floatPrice of a bid level, index 0 the best bid
l2.bid_size(index: int) -> floatResting size on it
l2.ask_price(index: int) -> floatPrice of an offer level, index 0 the best offer
l2.ask_size(index: int) -> floatResting size on it
l2.bid_total([depth: int]) -> floatSum of the best depth bid sizes, every level when depth is omitted or 0
l2.ask_total([depth: int]) -> floatThe same on the offer side
l2.imbalance([depth: int]) -> floatBid total minus ask total over their sum, from -1 to 1
l2.size_at(price: float) -> floatResting size at an exact price, zero when the book has none there
l2.spread() -> floatBest offer minus best bid
l2.mid() -> floatMidpoint of the top of book
l2.max_bid_price() -> floatPrice of the bid level with the most resting size
l2.max_bid_size() -> floatIts size
l2.max_ask_price() -> floatPrice of the offer level with the most resting size
l2.max_ask_size() -> floatIts size
  • Live, the chart records the book on each bar from the moment the script starts running on it, up to the newest 5,000 bars, and the newest bar always carries the current book. Bars from before that answer l2.available() false. Reopening the chart starts the history again.
  • A bar with no snapshot answers zero counts and nan for every price and size. An index of -1 answers nan.
  • The max builtins find the biggest resting level on each side of the snapshot, the wall, in one call. Of two equal levels the one nearer the inside market wins. Without a snapshot they answer nan.
  • Totals only walk the levels you ask for, so l2.imbalance(10) is cheap even on a deep book.
  • In replay the book comes from the recorded depth at the playhead and never from after it.
let has_depth: bool = l2.supported() && l2.available()
let lean: float = l2.imbalance(10)
let resting_bid: float = l2.bid_total(5)
let resting_ask: float = l2.ask_total(5)
if has_depth {
  plot.histogram(lean, "#22C55E", "#EF4444")
  plot.label(str.concat("BID WALL ", format.fixed(l2.max_bid_price(), 2), " x ", format.compact(l2.max_bid_size()), "  ASK WALL ", format.fixed(l2.max_ask_price(), 2), " x ", format.compact(l2.max_ask_size())), "#E5E7EB")
}
plot.panel_range(-1, 1)

Všechny vrstvy trhu – od Orderflow po optionflow – v jednom grafu.

Orderflow data institucionální kvality s vestavěným backtestingem a obchodním deníkem. Zapište se na čekací listinu a získejte předběžný přístup.

Získat přístup