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.
| Signature | Returns |
|---|---|
| l2.supported() -> bool | Whether depth is wired on this chart |
| l2.available() -> bool | Whether a book snapshot landed inside this bar |
| l2.time() -> float | Snapshot timestamp, epoch milliseconds |
| l2.bid_count() -> int | Bid levels in the snapshot, 0 without one |
| l2.ask_count() -> int | Offer levels in the snapshot |
| l2.bid_price(index: int) -> float | Price of a bid level, index 0 the best bid |
| l2.bid_size(index: int) -> float | Resting size on it |
| l2.ask_price(index: int) -> float | Price of an offer level, index 0 the best offer |
| l2.ask_size(index: int) -> float | Resting size on it |
| l2.bid_total([depth: int]) -> float | Sum of the best depth bid sizes, every level when depth is omitted or 0 |
| l2.ask_total([depth: int]) -> float | The same on the offer side |
| l2.imbalance([depth: int]) -> float | Bid total minus ask total over their sum, from -1 to 1 |
| l2.size_at(price: float) -> float | Resting size at an exact price, zero when the book has none there |
| l2.spread() -> float | Best offer minus best bid |
| l2.mid() -> float | Midpoint of the top of book |
| l2.max_bid_price() -> float | Price of the bid level with the most resting size |
| l2.max_bid_size() -> float | Its size |
| l2.max_ask_price() -> float | Price of the offer level with the most resting size |
| l2.max_ask_size() -> float | Its 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)