MMTMMT Docs

Intrabar order flow

React to individual large trades, annotate the candle that received them, and aggregate signed volume by chart bar.

This example uses the trade handler for per-print work and the candle handler for bar-level output. It shows the useful division between event data and chart data without turning the chart into an unreadable tape display.

BTC chart with sparse large-trade markers and a signed large-print delta pane
script "Intrabar Order Flow"

data chart = subscribe(data.ohlcv)
data tape = subscribe(data.trades, minSize: 150.0)

pane flowPane = pane(title: "Large-print Delta", height: 0.24)

plot (
  barDelta = plot.histogram(title: "Large-print Delta", on: flowPane, showLabel: false)
  averageDelta = plot.line(title: "20-bar Average", color: color.withAlpha(color.gray, 180), width: 1, on: flowPane)
)

state (
  markers = entities.markerPool(max: 160)
  deltas = rolling<float>(20)
  signedVolume = 0.0
  sequence = 0
)

on tape.trade(tr) {
  let signed = tr.isBuy ? tr.size : -tr.size
  signedVolume += signed
  sequence += 1

  let sideColor = tr.isBuy ? color.green : color.red
  markers.get(sequence).set(
    x: tr.time,
    y: tr.price,
    shape: tr.isBuy ? shape.up : shape.down,
    color: color.withAlpha(sideColor, 175),
    size: math.clamp(2.5 + tr.size / 400.0, 2.5, 6.0),
  )
}

on chart.close {
  deltas.push(signedVolume)
  barDelta.plot(
    signedVolume,
    color: signedVolume >= 0.0 ? color.withAlpha(color.green, 210) : color.withAlpha(color.red, 210),
  )
  averageDelta.plot(deltas.avg())
  signedVolume = 0.0
}

Filter before the handler runs

minSize: 150.0 keeps the handler focused on prints that matter to this indicator. Tune the threshold for the market; the rest of the script does not need another size check.

Aggregate events into candles

Every qualifying trade updates signedVolume. When the chart candle closes, the script plots that total, pushes it into a rolling window, and resets it for the next candle. This pattern works for trade count, notional, liquidations, and other event streams.

Keep chart annotations selective

Marker size grows with trade size but is clamped to a narrow range. That preserves the information without covering the candles.

On this page