Code example
Iceberg Detection
Detect a trade that fills more size than the pre-trade displayed size at a level, as a chart indicator.
// Book sizes captured on the previous tick (state persists across runs of the script)
const prevAsks = (state.prevAsks as Record<number, number>) ?? {}
const prevBids = (state.prevBids as Record<number, number>) ?? {}
// Buy-aggressor trades lift the ask: more filled than was shown means hidden sell limits
for (const b of trades.buy) {
const shown = prevAsks[b.price] ?? 0
if (shown > 0 && b.size > shown) {
const hiddenLimits = b.size - shown
console.log("sell iceberg", "px", b.price, "traded", b.size, "shown", shown)
const radius = Math.min(3 + Math.sqrt(hiddenLimits), 24)
chart.mark({ price: b.price, color: "rgba(0, 200, 0, 0.35)", radius })
}
}
// Sell-aggressor trades hit the bid: more filled than was shown means hidden buy limits
for (const s of trades.sell) {
const shown = prevBids[s.price] ?? 0
if (shown > 0 && s.size > shown) {
const hiddenLimits = s.size - shown
console.log("buy iceberg", "px", s.price, "traded", s.size, "shown", shown)
const radius = Math.min(3 + Math.sqrt(hiddenLimits), 24)
chart.mark({ price: s.price, color: "rgba(255, 0, 0, 0.35)", radius })
}
}
// Snapshot this tick's book so the next tick can compare traded vs shown size
const askSnapshot: Record<number, number> = {}
for (const a of book.ask) askSnapshot[a.price] = a.size
state.prevAsks = askSnapshot
const bidSnapshot: Record<number, number> = {}
for (const b of book.bid) bidSnapshot[b.price] = b.size
state.prevBids = bidSnapshotThe circles drawn by chart.mark are the visible result on the chart. console.log output appears in your browser's devtools console, not in the app.