Examples
Book pressure and microprice
Aggregated book snapshots (every 250 ms) summarized per bar: the depth imbalance over the first levels and the distance between the microprice and the mid, both sampled once per second by a clock and averaged.
indicator "Book pressure" { overlay: false, min: -1, max: 1, precision: 2 }
levels = input.int("Levels", 5, min: 1, max: 300)
clock sec = clock.time(1s)
state imbSum = 0
state microSum = 0
state n = 0
on clock sec {
if book.available and book.spread > 0 {
imbSum += book.imbalance(levels)
microSum += (book.microprice - book.mid) / book.spread
n += 1
}
}
plot(n > 0 ? imbSum / n : na, #2962ff, title: "Depth imbalance", width: 2)
plot(n > 0 ? microSum / n : na, #e8c15a, title: "Microprice − mid (in spreads)")
hline(0, color: #888888)
imbSum = 0
microSum = 0
n = 0What to notice:
bookis the latest snapshot and can be read in a clock handler;- the clock samples at a fixed rhythm, so a burst of snapshots does not weigh more than a quiet period;
- the pane is bounded with
min: -1, max: 1.