OHLC bars
Generate OHLC bars from raw trade data. OHLC summarizes price action within each time period: the first trade (open), highest price (high), lowest price (low), and last trade (close).
The problem
You have tick-level trade data and need to aggregate it into standard candlestick bars for charting or technical analysis.
Solution: Use SAMPLE BY with first, max, min, last
SELECT
timestamp,
symbol,
first(price) AS open,
max(price) AS high,
min(price) AS low,
last(price) AS close,
sum(quantity) AS total_volume
FROM fx_trades
WHERE timestamp IN '$today'
SAMPLE BY 1m;
This query:
- Groups trades into 1-minute intervals using
SAMPLE BY - Uses
first()andlast()to capture opening and closing prices - Uses
max()andmin()to capture the price range - Sums volume for each bar
Pre-compute bars with a materialized view
If you query OHLC bars frequently, such as for a dashboard, create a materialized view to pre-compute the aggregation:
CREATE MATERIALIZED VIEW 'fx_trades_ohlc_1m' WITH BASE 'fx_trades' REFRESH IMMEDIATE AS (
SELECT
timestamp,
symbol,
first(price) AS open,
max(price) AS high,
min(price) AS low,
last(price) AS close,
sum(quantity) AS total_volume
FROM fx_trades
SAMPLE BY 1m
) PARTITION BY HOUR TTL 2 DAYS;
QuestDB automatically refreshes the view as new trades arrive. Queries against the view return instantly regardless of the underlying data volume.
OHLC bars reduce tick data by time. To reduce it by price movement instead, keeping the real quotes where the price path turns, see Find significant price moves with SDT.