Depth imbalance panel
Depth of book: a panel of how the resting size leans between the best bid levels and the best offer levels, smoothed, with the resting totals in the readout. Where the chart has no depth it shows the tape delta instead.
//@xfscript 1
indicator("Depth Imbalance")
let levels: int = input.int(10, "levels", "Levels per side", 1, 50, 1)
let smooth: int = input.int(5, "smooth", "Smoothing", 1, 50, 1)
let bid_color: color = input.color("#22C55E", "bidColor", "Bids heavier")
let ask_color: color = input.color("#EF4444", "askColor", "Offers heavier")
let has_depth: bool = l2.supported() && l2.available()
let book: float = l2.imbalance(levels)
let tape_lean: float = tape.delta() / tape.volume()
var reading: float = 0
reading := nz(tape_lean, 0)
if has_depth {
reading := nz(book, 0)
}
let smoothed: float = ta.ema(reading, smooth)
plot.panel_range(-1, 1)
plot.histogram(reading, bid_color, ask_color)
plot.panel_line(smoothed, "#E5E7EB", 2, "solid")
plot.panel_hline(0.3, bid_color, "dotted", "Bid heavy")
plot.panel_hline(-0.3, ask_color, "dotted", "Offer heavy")
if has_depth {
plot.label(str.concat("BID ", format.compact(l2.bid_total(levels)), " ASK ", format.compact(l2.ask_total(levels)), " SPREAD ", format.fixed(l2.spread(), 2)), "#E5E7EB")
} else {
plot.label("DEPTH OFF, SHOWING TAPE DELTA", "#6B7280")
}