Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions documentation/cookbook/demo-data-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The table tracks **30 currency pairs**: EURUSD, GBPUSD, USDJPY, USDCHF, AUDUSD,

```questdb-sql demo title="Recent core_price updates"
SELECT * FROM core_price
WHERE timestamp IN today()
WHERE timestamp IN '$today'
LIMIT -10;
```

Expand Down Expand Up @@ -116,7 +116,7 @@ SELECT timestamp, symbol,
array_count(bids[1]) as bid_levels,
array_count(asks[1]) as ask_levels
FROM market_data
WHERE timestamp IN today()
WHERE timestamp IN '$today'
LIMIT -5;
```

Expand Down Expand Up @@ -170,7 +170,7 @@ CREATE TABLE 'fx_trades' (

```questdb-sql demo title="Recent FX trades"
SELECT * FROM fx_trades
WHERE timestamp IN today()
WHERE timestamp IN '$today'
LIMIT -10;
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ The PostgreSQL data source recognizes `__text` and `__value` as special column n
**Different filter conditions:**
```sql
-- Filter by time range
WHERE timestamp IN yesterday()
WHERE timestamp IN '$yesterday'

-- Filter by multiple criteria
WHERE symbol LIKE '%USDT' AND price > 1000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ SELECT
sum(CASE WHEN amount <= 1.0 THEN amount END) as small_trade_volume,
sum(amount) as total_volume
FROM trades
WHERE timestamp >= dateadd('d', -1, now())
WHERE timestamp IN '$now - 1d..$now'
AND symbol IN ('BTC-USDT', 'ETH-USDT')
GROUP BY symbol;
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ SELECT
floor(amount / @bucket_size) * @bucket_size AS bucket,
count(*) AS count
FROM trades
WHERE symbol = 'BTC-USDT' AND timestamp IN today()
WHERE symbol = 'BTC-USDT' AND timestamp IN '$today'
GROUP BY bucket
ORDER BY bucket;
```
Expand Down Expand Up @@ -53,7 +53,7 @@ DECLARE @bucket_count := 50

WITH raw_data AS (
SELECT price, amount FROM trades
WHERE symbol = 'BTC-USDT' AND timestamp IN today()
WHERE symbol = 'BTC-USDT' AND timestamp IN '$today'
),
bucket_size AS (
SELECT (max(price) - min(price)) / (@bucket_count - 1) AS bucket_size FROM raw_data
Expand Down Expand Up @@ -83,7 +83,7 @@ SELECT
FROM trades
WHERE symbol = 'BTC-USDT'
AND amount > 0.000001 -- optional. Just adding here for easier visualization
AND timestamp IN today()
AND timestamp IN '$today'
GROUP BY bucket
ORDER BY bucket;
```
Expand All @@ -104,7 +104,7 @@ SELECT
END AS bucket,
count(*) AS count
FROM trades
WHERE symbol = 'BTC-USDT' AND timestamp IN today()
WHERE symbol = 'BTC-USDT' AND timestamp IN '$today'
GROUP BY bucket;
```

Expand All @@ -119,7 +119,7 @@ SELECT
floor(amount / @bucket_size) * @bucket_size AS bucket,
count(*) AS count
FROM trades
WHERE symbol = 'BTC-USDT' AND timestamp IN today()
WHERE symbol = 'BTC-USDT' AND timestamp IN '$today'
SAMPLE BY 1h
ORDER BY timestamp, bucket;
```
Expand Down
4 changes: 2 additions & 2 deletions documentation/cookbook/sql/advanced/local-min-max.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ SELECT timestamp, bid_price,
min(bid_price) OVER (ORDER BY timestamp RANGE 1 second PRECEDING) AS min_price,
max(bid_price) OVER (ORDER BY timestamp RANGE 1 second PRECEDING) AS max_price
FROM core_price
WHERE timestamp >= dateadd('m', -1, now()) AND symbol = 'EURUSD';
WHERE timestamp IN '$now - 1m..$now' AND symbol = 'EURUSD';
```

This returns the minimum and maximum bid price from the 1 second preceding each row.
Expand All @@ -35,7 +35,7 @@ SELECT p.timestamp, p.bid_price,
FROM core_price p
WINDOW JOIN core_price pp ON symbol
RANGE BETWEEN 1 second PRECEDING AND 1 second FOLLOWING
WHERE p.timestamp >= dateadd('m', -1, now()) AND p.symbol = 'EURUSD';
WHERE p.timestamp IN '$now - 1m..$now' AND p.symbol = 'EURUSD';
```

This returns the minimum and maximum bid price from 1 second before to 1 second after each row.
Expand Down
4 changes: 2 additions & 2 deletions documentation/cookbook/sql/advanced/pivot-with-others.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ You want to pivot data so that specific symbols (like EURUSD, GBPUSD, USDJPY) be
```questdb-sql demo title="Aggregated data per symbol"
SELECT timestamp, symbol, SUM(bid_volume) AS total_bid
FROM core_price
WHERE timestamp IN today()
WHERE timestamp IN '$today'
SAMPLE BY 1m
LIMIT 20;
```
Expand Down Expand Up @@ -55,7 +55,7 @@ SELECT timestamp,
SUM(CASE WHEN symbol NOT IN ('EURUSD', 'GBPUSD', 'USDJPY')
THEN bid_volume END) AS OTHERS
FROM core_price
WHERE timestamp IN today()
WHERE timestamp IN '$today'
SAMPLE BY 1m
LIMIT 5;
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ SELECT timestamp, bid_price,
LEAD(bid_price, 4) OVER () AS next_4,
LEAD(bid_price, 5) OVER () AS next_5
FROM core_price
WHERE timestamp >= dateadd('m', -1, now()) AND symbol = 'EURUSD';
WHERE timestamp IN '$now - 1m..$now' AND symbol = 'EURUSD';
```

## How it works
Expand Down
2 changes: 1 addition & 1 deletion documentation/cookbook/sql/advanced/sankey-funnel.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ WITH PrevEvents AS (
timestamp,
lag(timestamp) OVER (PARTITION BY visitor_id ORDER BY timestamp) AS prev_ts
FROM
events WHERE timestamp > dateadd('d', -7, now())
events WHERE timestamp IN '$now - 7d..$now'
AND metric_name = 'page_view'
), VisitorSessions AS (
SELECT *,
Expand Down
6 changes: 3 additions & 3 deletions documentation/cookbook/sql/advanced/top-n-plus-others.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ WITH totals AS (
symbol,
count() as total
FROM trades
WHERE timestamp >= dateadd('d', -1, now())
WHERE timestamp IN '$now - 1d..$now'
),
ranked AS (
SELECT
Expand Down Expand Up @@ -160,7 +160,7 @@ Results in three groups: top 5 individual, ranks 6-10 combined, rest combined.
WITH totals AS (
SELECT symbol, count() as total
FROM trades
WHERE timestamp >= dateadd('d', -1, now())
WHERE timestamp IN '$now - 1d..$now'
),
ranked AS (
SELECT *, rank() OVER (ORDER BY total DESC) as ranking
Expand Down Expand Up @@ -196,7 +196,7 @@ WITH totals AS (
side,
count() as total
FROM trades
WHERE timestamp >= dateadd('d', -1, now())
WHERE timestamp IN '$now - 1d..$now'
),
ranked AS (
SELECT
Expand Down
4 changes: 2 additions & 2 deletions documentation/cookbook/sql/advanced/unpivot-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ WITH pivoted AS (
CASE WHEN side = 'buy' THEN price END as buy,
CASE WHEN side = 'sell' THEN price END as sell
FROM trades
WHERE timestamp >= dateadd('m', -5, now())
WHERE timestamp IN '$now - 5m..$now'
AND symbol = 'ETH-USDT'
),
unpivoted AS (
Expand Down Expand Up @@ -114,7 +114,7 @@ WITH sensor_data AS (
humidity,
pressure
FROM sensors
WHERE timestamp >= dateadd('h', -1, now())
WHERE timestamp IN '$now - 1h..$now'
)
SELECT timestamp, sensor_id, 'temperature' as metric, temperature as value FROM sensor_data
WHERE temperature IS NOT NULL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ WITH volumes AS (
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()
WHERE timestamp IN '$yesterday'
AND symbol IN ('ETH-USDT', 'BTC-USDT', 'ETH-BTC')
)
SELECT
Expand Down
4 changes: 2 additions & 2 deletions documentation/cookbook/sql/finance/bollinger-bands.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ WITH OHLC AS (
last(price) AS close,
sum(quantity) AS volume
FROM fx_trades
WHERE symbol = 'EURUSD' AND timestamp IN yesterday()
WHERE symbol = 'EURUSD' AND timestamp IN '$yesterday'
SAMPLE BY 15m
), stats AS (
SELECT
Expand Down Expand Up @@ -109,7 +109,7 @@ WITH OHLC AS (
sum(quantity) AS volume
FROM fx_trades
WHERE symbol IN ('EURUSD', 'GBPUSD')
AND timestamp IN yesterday()
AND timestamp IN '$yesterday'
SAMPLE BY 15m
), stats AS (
SELECT
Expand Down
10 changes: 5 additions & 5 deletions documentation/cookbook/sql/finance/bollinger-bandwidth.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ When BandWidth drops to historically low levels, the bands are in a "squeeze". P
```questdb-sql demo title="Calculate Bollinger BandWidth with range position"
DECLARE
@symbol := 'BTC-USDT',
@history_start := dateadd('M', -6, now()),
@display_start := dateadd('M', -1, now())
@history := '$now - 6M..$now',
@display := '$now - 1M..$now'

WITH daily_ohlc AS (
SELECT
Expand All @@ -38,7 +38,7 @@ WITH daily_ohlc AS (
last(close) AS close
FROM trades_ohlc_15m
WHERE symbol = @symbol
AND timestamp > @history_start
AND timestamp IN @history
SAMPLE BY 1d
),
bands AS (
Expand Down Expand Up @@ -102,11 +102,11 @@ SELECT
round(bandwidth, 4) AS bandwidth,
round((bandwidth - min_bw) / (max_bw - min_bw) * 100, 1) AS range_position
FROM with_range
WHERE timestamp > @display_start
WHERE timestamp IN @display
ORDER BY timestamp;
```

The query first aggregates 15-minute candles into daily OHLC, then calculates standard 20-day Bollinger Bands. This matches the traditional approach where SMA20 represents roughly one month of trading. The 6-month lookback (`@history_start`) establishes the historical range, while `@display_start` limits output to the last month. Standard deviation uses the variance formula `sqrt(avg(x²) - avg(x)²)`.
The query first aggregates 15-minute candles into daily OHLC, then calculates standard 20-day Bollinger Bands. This matches the traditional approach where SMA20 represents roughly one month of trading. The 6-month lookback (`@history`) establishes the historical range, while `@display` limits output to the last month. Standard deviation uses the variance formula `sqrt(avg(x²) - avg(x)²)`.

The `range_position` shows where current BandWidth falls within the 6-month range: 0% means at the historical minimum, 100% at the maximum. This works well for identifying squeeze conditions since you're comparing against historical extremes.

Expand Down
6 changes: 3 additions & 3 deletions documentation/cookbook/sql/finance/liquidity-comparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ You have order book snapshots for multiple instruments and want to compare which
WITH latest_books AS (
SELECT timestamp, symbol, bids, asks
FROM market_data
WHERE timestamp IN today()
WHERE timestamp IN '$today'
LATEST ON timestamp PARTITION BY symbol
)
SELECT
Expand Down Expand Up @@ -49,7 +49,7 @@ SELECT
last((L2PRICE(100_000, asks[2], asks[1]) - L2PRICE(100_000, bids[2], bids[1])) /
((L2PRICE(100_000, asks[2], asks[1]) + L2PRICE(100_000, bids[2], bids[1])) / 2)) * 10_000 AS spread_bps
FROM market_data
WHERE timestamp IN today()
WHERE timestamp IN '$today'
AND symbol IN ('EURUSD', 'GBPUSD', 'USDJPY')
SAMPLE BY 1h
ORDER BY timestamp, symbol;
Expand All @@ -63,7 +63,7 @@ See how execution costs scale with order size:
WITH latest_books AS (
SELECT symbol, bids, asks
FROM market_data
WHERE timestamp IN today()
WHERE timestamp IN '$today'
LATEST ON timestamp PARTITION BY symbol
)
SELECT
Expand Down
2 changes: 1 addition & 1 deletion documentation/cookbook/sql/finance/ohlc.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SELECT
last(price) AS close,
sum(quantity) AS total_volume
FROM fx_trades
WHERE timestamp IN today()
WHERE timestamp IN '$today'
SAMPLE BY 1m;
```

Expand Down
2 changes: 1 addition & 1 deletion documentation/cookbook/sql/finance/rolling-stddev.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ WITH stats AS (
AVG(price) OVER (PARTITION BY symbol ORDER BY timestamp) AS rolling_avg,
AVG(price * price) OVER (PARTITION BY symbol ORDER BY timestamp) AS rolling_avg_sq
FROM fx_trades
WHERE timestamp IN yesterday() AND symbol = 'EURUSD'
WHERE timestamp IN '$yesterday' AND symbol = 'EURUSD'
)
SELECT
timestamp,
Expand Down
6 changes: 3 additions & 3 deletions documentation/cookbook/sql/finance/tick-trin.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ WITH with_previous AS (
SELECT timestamp, symbol, price,
LAG(price) OVER (PARTITION BY symbol ORDER BY timestamp) AS prev_price
FROM fx_trades
WHERE timestamp IN today()
WHERE timestamp IN '$today'
),
classified AS (
SELECT symbol,
Expand Down Expand Up @@ -64,7 +64,7 @@ WITH daily_stats AS (
last(price) AS current_price,
sum(quantity) AS total_volume
FROM fx_trades
WHERE timestamp IN today()
WHERE timestamp IN '$today'
SAMPLE BY 1d
),
classified AS (
Expand Down Expand Up @@ -94,7 +94,7 @@ WITH candles AS (
last(price) AS close_price,
sum(quantity) AS total_volume
FROM fx_trades
WHERE timestamp IN today()
WHERE timestamp IN '$today'
SAMPLE BY 5m
),
with_previous AS (
Expand Down
4 changes: 2 additions & 2 deletions documentation/cookbook/sql/finance/volume-profile.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ SELECT
round(SUM(quantity), 2) AS volume
FROM fx_trades
WHERE symbol = 'EURUSD'
AND timestamp IN today()
AND timestamp IN '$today'
ORDER BY price_bin;
```

Expand All @@ -31,7 +31,7 @@ For consistent histograms across different price ranges, calculate the tick size
WITH raw_data AS (
SELECT price, quantity
FROM fx_trades
WHERE symbol = 'EURUSD' AND timestamp IN today()
WHERE symbol = 'EURUSD' AND timestamp IN '$today'
),
tick_size AS (
SELECT (max(price) - min(price)) / 49 as tick_size
Expand Down
5 changes: 2 additions & 3 deletions documentation/cookbook/sql/finance/volume-spike.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ Use the `LAG` window function to retrieve the previous candle's volume, then com

```questdb-sql demo title="Detect volume spikes exceeding 2x previous volume"
DECLARE
@anchor_date := timestamp_floor('30s', now()),
@start_date := dateadd('h', -7, @anchor_date),
@range := '$now - 7h..$now',
@symbol := 'EURUSD'
WITH candles AS (
SELECT
timestamp,
symbol,
sum(quantity) AS volume
FROM fx_trades
WHERE timestamp >= @start_date
WHERE timestamp IN @range
AND symbol = @symbol
SAMPLE BY 30s
),
Expand Down
Loading