Back to Questdb

Aggressor volume imbalance

documentation/cookbook/sql/finance/aggressor-volume-imbalance.md

latest1.1 KB
Original Source

Calculate the imbalance between buy and sell aggressor volume to analyze order flow. The aggressor is the party that initiated the trade by crossing the spread.

Problem: Measure order flow imbalance

You have trade data with a side column indicating the aggressor (buyer or seller), and want to measure the imbalance between buying and selling pressure.

Solution: Aggregate by side and calculate ratios

questdb-sql
WITH volumes AS (
  SELECT
    symbol,
    SUM(CASE WHEN side = 'buy' THEN amount ELSE 0 END) AS buy_volume,
    SUM(CASE WHEN side = 'sell' THEN amount ELSE 0 END) AS sell_volume
  FROM trades
  WHERE timestamp IN '$yesterday'
    AND symbol IN ('ETH-USDT', 'BTC-USDT', 'ETH-BTC')
)
SELECT
  symbol,
  buy_volume,
  sell_volume,
  ((buy_volume - sell_volume)::double / (buy_volume + sell_volume)) * 100 AS imbalance
FROM volumes;

The imbalance ranges from -100% (all sell) to +100% (all buy), with 0% indicating balanced flow.

:::info Related documentation