OBV (On-Balance Volume)
On-Balance Volume (OBV) is a cumulative indicator that adds volume on up days and subtracts volume on down days. It shows whether volume is flowing into or out of an asset, often leading price movements.
Problem
You want to confirm price trends with volume or spot divergences where volume doesn't support price movement. Raw volume numbers don't show direction, and comparing volumes across different time periods is difficult.
Solution
Calculate On-Balance VolumeDemo this query
DECLARE
@symbol := 'EURUSD',
@lookback := '$now - 1M..$now'
WITH ohlc AS (
SELECT
timestamp,
symbol,
last(price) AS close,
sum(quantity) AS volume
FROM fx_trades
WHERE symbol = @symbol
AND timestamp IN @lookback
SAMPLE BY 15m ALIGN TO CALENDAR
),
with_direction AS (
SELECT
timestamp,
symbol,
close,
volume,
CASE
WHEN close > lag(close) OVER w THEN volume
WHEN close < lag(close) OVER w THEN -volume
ELSE 0
END AS directed_volume
FROM ohlc
WINDOW w AS (PARTITION BY symbol ORDER BY timestamp)
)
SELECT
timestamp,
symbol,
round(close, 5) AS close,
round(volume, 0) AS volume,
round(sum(directed_volume) OVER (
PARTITION BY symbol
ORDER BY timestamp
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
), 0) AS obv
FROM with_direction
ORDER BY timestamp;
The query:
- Aggregates raw trades into 15-minute bars with closing price and total volume
- Compares each bar's close to the previous close
- Assigns positive volume if price went up, negative if down, zero if unchanged
- Calculates cumulative sum of directed volume
Interpreting results
- OBV rising with price: Uptrend confirmed by volume
- OBV falling with price: Downtrend confirmed by volume
- OBV rising, price flat: Accumulation, potential breakout up
- OBV falling, price flat: Distribution, potential breakout down
- OBV divergence from price: Trend may be weakening
OBV absolute value
The absolute value of OBV is meaningless. What matters is the direction and whether it confirms or diverges from price.
Related documentation