documentation/cookbook/sql/finance/bid-ask-spread.md
The bid-ask spread is the difference between the best ask (lowest sell price) and best bid (highest buy price). It represents the cost of immediately executing a round-trip trade and is a key measure of market liquidity.
You want to measure market liquidity and transaction costs. Narrow spreads indicate liquid markets with low trading costs, while wide spreads suggest illiquidity or market stress.
DECLARE
@symbol := 'EURUSD',
@lookback := '$now - 1h..$now'
SELECT
timestamp,
symbol,
round(bid_price, 5) AS bid,
round(ask_price, 5) AS ask,
round(ask_price - bid_price, 6) AS spread_absolute,
round((ask_price - bid_price) / ((bid_price + ask_price) / 2) * 10000, 2) AS spread_bps,
round((bid_price + ask_price) / 2, 5) AS mid_price
FROM core_price
WHERE symbol = @symbol
AND timestamp IN @lookback
ORDER BY timestamp;
The query calculates:
DECLARE
@symbol := 'EURUSD',
@lookback := '$now - 1d..$now'
SELECT
timestamp,
symbol,
round(avg((ask_price - bid_price) / ((bid_price + ask_price) / 2) * 10000), 2) AS avg_spread_bps,
round(min((ask_price - bid_price) / ((bid_price + ask_price) / 2) * 10000), 2) AS min_spread_bps,
round(max((ask_price - bid_price) / ((bid_price + ask_price) / 2) * 10000), 2) AS max_spread_bps,
count() AS quote_count
FROM core_price
WHERE symbol = @symbol
AND timestamp IN @lookback
SAMPLE BY 1h
ORDER BY timestamp;
:::note Spread conventions (venue-dependent) Typical spreads vary a lot by venue, instrument, and session:
Treat these as rough guidelines, not fixed thresholds. :::
:::info Related documentation