Skip to content

Commit d0ee55e

Browse files
committed
Update financial indicator recipes to use TICK syntax
Replace legacy dateadd patterns with TICK syntax in 11 recipes: - @lookback := dateadd('M', -1, now()) → @lookback := '$now - 1M..$now' - timestamp > @lookback → timestamp IN @lookback Updated: RSI, MACD, ATR, Stochastic, Donchian Channels, Keltner Channels, OBV, Rate of Change, Realized Volatility, Maximum Drawdown, Bid-Ask Spread Also adds elapsed-time recipe to sidebar.
1 parent 642bf17 commit d0ee55e

12 files changed

Lines changed: 42 additions & 38 deletions

File tree

documentation/cookbook/sql/finance/atr.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You want to measure volatility to set appropriate stop-losses or position sizes.
1515
```questdb-sql demo title="Calculate 14-period ATR"
1616
DECLARE
1717
@symbol := 'EURUSD',
18-
@lookback := dateadd('M', -1, now())
18+
@lookback := '$now - 1M..$now'
1919
2020
WITH with_prev AS (
2121
SELECT
@@ -27,7 +27,7 @@ WITH with_prev AS (
2727
lag(close) OVER (PARTITION BY symbol ORDER BY timestamp) AS prev_close
2828
FROM market_data_ohlc_15m
2929
WHERE symbol = @symbol
30-
AND timestamp > @lookback
30+
AND timestamp IN @lookback
3131
),
3232
true_range AS (
3333
SELECT
@@ -83,6 +83,10 @@ entry_price - 2 * atr AS stop_loss
8383
(account_size * 0.01) / atr AS position_size
8484
```
8585

86+
:::note EMA vs Wilder's smoothing
87+
This recipe uses standard EMA smoothing via `avg(value, 'period', 14)` where α = 2/(N+1). Wilder's original ATR uses α = 1/N, which is more gradual. For exact Wilder smoothing with a 14-period lookback, use `avg(value, 'period', 27)`. Most modern platforms offer both variants.
88+
:::
89+
8690
:::info Related documentation
8791
- [EMA window function](/docs/query/functions/window-functions/reference/#avg)
8892
- [lag() function](/docs/query/functions/window-functions/reference/#lag)

documentation/cookbook/sql/finance/bid-ask-spread.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You want to measure market liquidity and transaction costs. Narrow spreads indic
1515
```questdb-sql demo title="Calculate bid-ask spread metrics"
1616
DECLARE
1717
@symbol := 'EURUSD',
18-
@lookback := dateadd('h', -1, now())
18+
@lookback := '$now - 1h..$now'
1919
2020
SELECT
2121
timestamp,
@@ -27,7 +27,7 @@ SELECT
2727
round((bid_price + ask_price) / 2, 5) AS mid_price
2828
FROM core_price
2929
WHERE symbol = @symbol
30-
AND timestamp > @lookback
30+
AND timestamp IN @lookback
3131
ORDER BY timestamp;
3232
```
3333

@@ -41,7 +41,7 @@ The query calculates:
4141
```questdb-sql demo title="Average spread by time period"
4242
DECLARE
4343
@symbol := 'EURUSD',
44-
@lookback := dateadd('d', -1, now())
44+
@lookback := '$now - 1d..$now'
4545
4646
SELECT
4747
timestamp,
@@ -52,7 +52,7 @@ SELECT
5252
count() AS quote_count
5353
FROM core_price
5454
WHERE symbol = @symbol
55-
AND timestamp > @lookback
55+
AND timestamp IN @lookback
5656
SAMPLE BY 1h
5757
ORDER BY timestamp;
5858
```

documentation/cookbook/sql/finance/donchian-channels.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You want to identify breakout levels and trading ranges. Moving averages smooth
1515
```questdb-sql demo title="Calculate 20-period Donchian Channels"
1616
DECLARE
1717
@symbol := 'EURUSD',
18-
@lookback := dateadd('M', -1, now())
18+
@lookback := '$now - 1M..$now'
1919
2020
SELECT
2121
timestamp,
@@ -42,7 +42,7 @@ SELECT
4242
)) / 2, 5) AS middle_channel
4343
FROM market_data_ohlc_15m
4444
WHERE symbol = @symbol
45-
AND timestamp > @lookback
45+
AND timestamp IN @lookback
4646
ORDER BY timestamp;
4747
```
4848

documentation/cookbook/sql/finance/keltner-channels.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ You want volatility bands that adapt to market conditions but are smoother than
1212

1313
## Solution
1414

15-
```questdb-sql demo title="Calculate Keltner Channels with 20 EMA and 2x ATR"
15+
```questdb-sql demo title="Calculate Keltner Channels (20-period EMA ± 2× ATR)"
1616
DECLARE
1717
@symbol := 'EURUSD',
18-
@lookback := dateadd('M', -1, now())
18+
@lookback := '$now - 1M..$now'
1919
2020
WITH with_prev AS (
2121
SELECT
@@ -27,7 +27,7 @@ WITH with_prev AS (
2727
lag(close) OVER (PARTITION BY symbol ORDER BY timestamp) AS prev_close
2828
FROM market_data_ohlc_15m
2929
WHERE symbol = @symbol
30-
AND timestamp > @lookback
30+
AND timestamp IN @lookback
3131
),
3232
with_tr AS (
3333
SELECT
@@ -50,7 +50,7 @@ with_indicators AS (
5050
symbol,
5151
close,
5252
avg(close, 'period', 20) OVER (PARTITION BY symbol ORDER BY timestamp) AS ema20,
53-
avg(tr, 'period', 10) OVER (PARTITION BY symbol ORDER BY timestamp) AS atr
53+
avg(tr, 'period', 20) OVER (PARTITION BY symbol ORDER BY timestamp) AS atr
5454
FROM with_tr
5555
)
5656
SELECT
@@ -66,7 +66,7 @@ ORDER BY timestamp;
6666

6767
The query:
6868
1. Calculates True Range accounting for gaps
69-
2. Applies EMA smoothing to both price (20-period) and ATR (10-period)
69+
2. Applies 20-period EMA smoothing to both price and ATR
7070
3. Creates bands at ±2 ATR from the EMA
7171

7272
## Interpreting results

documentation/cookbook/sql/finance/macd.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You want to identify trend changes and momentum shifts. Simple moving averages l
1515
```questdb-sql demo title="Calculate MACD with signal line and histogram"
1616
DECLARE
1717
@symbol := 'EURUSD',
18-
@lookback := dateadd('M', -1, now())
18+
@lookback := '$now - 1M..$now'
1919
2020
WITH ema AS (
2121
SELECT
@@ -26,7 +26,7 @@ WITH ema AS (
2626
avg(close, 'period', 26) OVER (PARTITION BY symbol ORDER BY timestamp) AS ema26
2727
FROM market_data_ohlc_15m
2828
WHERE symbol = @symbol
29-
AND timestamp > @lookback
29+
AND timestamp IN @lookback
3030
),
3131
macd_line AS (
3232
SELECT

documentation/cookbook/sql/finance/maximum-drawdown.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You want to measure downside risk beyond simple volatility. Standard deviation t
1515
```questdb-sql demo title="Calculate rolling maximum drawdown"
1616
DECLARE
1717
@symbol := 'EURUSD',
18-
@lookback := dateadd('M', -1, now())
18+
@lookback := '$now - 1M..$now'
1919
2020
WITH with_peak AS (
2121
SELECT
@@ -29,7 +29,7 @@ WITH with_peak AS (
2929
) AS running_peak
3030
FROM market_data_ohlc_15m
3131
WHERE symbol = @symbol
32-
AND timestamp > @lookback
32+
AND timestamp IN @lookback
3333
),
3434
with_drawdown AS (
3535
SELECT

documentation/cookbook/sql/finance/obv.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ You want to confirm price trends with volume or spot divergences where volume do
1515
```questdb-sql demo title="Calculate On-Balance Volume"
1616
DECLARE
1717
@symbol := 'EURUSD',
18-
@lookback := dateadd('M', -1, now())
18+
@lookback := '$now - 1M..$now'
1919
2020
WITH with_direction AS (
2121
SELECT
2222
timestamp,
2323
symbol,
2424
close,
25-
quantity AS volume,
25+
total_volume AS volume,
2626
CASE
27-
WHEN close > lag(close) OVER (PARTITION BY symbol ORDER BY timestamp) THEN quantity
28-
WHEN close < lag(close) OVER (PARTITION BY symbol ORDER BY timestamp) THEN -quantity
27+
WHEN close > lag(close) OVER (PARTITION BY symbol ORDER BY timestamp) THEN total_volume
28+
WHEN close < lag(close) OVER (PARTITION BY symbol ORDER BY timestamp) THEN -total_volume
2929
ELSE 0
3030
END AS directed_volume
3131
FROM fx_trades_ohlc_1m
3232
WHERE symbol = @symbol
33-
AND timestamp > @lookback
33+
AND timestamp IN @lookback
3434
)
3535
SELECT
3636
timestamp,

documentation/cookbook/sql/finance/rate-of-change.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You want a simple momentum indicator that shows how fast price is changing. Raw
1515
```questdb-sql demo title="Calculate 12-period Rate of Change"
1616
DECLARE
1717
@symbol := 'EURUSD',
18-
@lookback := dateadd('M', -1, now())
18+
@lookback := '$now - 1M..$now'
1919
2020
SELECT
2121
timestamp,
@@ -29,7 +29,7 @@ SELECT
2929
) AS roc
3030
FROM market_data_ohlc_15m
3131
WHERE symbol = @symbol
32-
AND timestamp > @lookback
32+
AND timestamp IN @lookback
3333
ORDER BY timestamp;
3434
```
3535

documentation/cookbook/sql/finance/realized-volatility.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You want to measure how volatile an asset has actually been, either for risk man
1515
```questdb-sql demo title="Calculate 20-period realized volatility (annualized)"
1616
DECLARE
1717
@symbol := 'EURUSD',
18-
@lookback := dateadd('M', -1, now())
18+
@lookback := '$now - 1M..$now'
1919
2020
WITH returns AS (
2121
SELECT
@@ -25,7 +25,7 @@ WITH returns AS (
2525
ln(close / lag(close) OVER (PARTITION BY symbol ORDER BY timestamp)) AS log_return
2626
FROM market_data_ohlc_15m
2727
WHERE symbol = @symbol
28-
AND timestamp > @lookback
28+
AND timestamp IN @lookback
2929
),
3030
with_stats AS (
3131
SELECT
@@ -51,15 +51,15 @@ SELECT
5151
symbol,
5252
round(close, 5) AS close,
5353
round(log_return * 100, 4) AS return_pct,
54-
round(sqrt(mean_sq_return - mean_return * mean_return) * sqrt(252 * 96) * 100, 2) AS realized_vol_annualized
54+
round(sqrt(mean_sq_return - mean_return * mean_return) * sqrt(365 * 96) * 100, 2) AS realized_vol_annualized
5555
FROM with_stats
5656
ORDER BY timestamp;
5757
```
5858

5959
The query:
6060
1. Calculates log returns: `ln(close / previous_close)`
6161
2. Computes rolling standard deviation using variance formula
62-
3. Annualizes by multiplying by `sqrt(trading_periods_per_year)` (252 days × 96 fifteen-minute periods = 24,192)
62+
3. Annualizes by multiplying by `sqrt(periods_per_year)` (365 days × 96 fifteen-minute periods = 35,040 for the 24/7 simulated data)
6363

6464
## Interpreting results
6565

@@ -69,8 +69,7 @@ The query:
6969
- **Realized < Implied**: Options may be expensive
7070

7171
:::note Annualization factor
72-
For 15-minute data with 24/7 trading: `sqrt(365 * 96) ≈ 187`
73-
For daily data with ~252 trading days: `sqrt(252) ≈ 15.87`
72+
The demo FX data is simulated continuously (24/7, including weekends), so the annualization factor uses `365 * 96` (365 days × 96 fifteen-minute periods per day). For real FX markets (24/5), use `252 * 96`. For daily data, use `sqrt(252) ≈ 15.87`.
7473
:::
7574

7675
:::info Related documentation

documentation/cookbook/sql/finance/rsi.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You want to identify when an asset may be overbought or oversold based on recent
1515
```questdb-sql demo title="Calculate 14-period RSI with EMA smoothing"
1616
DECLARE
1717
@symbol := 'EURUSD',
18-
@lookback := dateadd('M', -1, now())
18+
@lookback := '$now - 1M..$now'
1919
2020
WITH changes AS (
2121
SELECT
@@ -25,7 +25,7 @@ WITH changes AS (
2525
close - lag(close) OVER (PARTITION BY symbol ORDER BY timestamp) AS change
2626
FROM market_data_ohlc_15m
2727
WHERE symbol = @symbol
28-
AND timestamp > @lookback
28+
AND timestamp IN @lookback
2929
),
3030
gains_losses AS (
3131
SELECT
@@ -68,8 +68,8 @@ The query:
6868
- **RSI = 50**: Neutral momentum
6969
- **Divergence**: When price makes new highs but RSI doesn't, it may signal weakening momentum
7070

71-
:::note RSI smoothing
72-
Traditional RSI uses Wilder's smoothing (equivalent to EMA with period 2N-1). The `avg(value, 'period', 14)` function uses standard EMA where α = 2/(14+1). For exact Wilder smoothing, use `avg(value, 'period', 27)` for a 14-period RSI.
71+
:::note EMA vs Wilder's smoothing
72+
This recipe uses standard EMA smoothing via `avg(value, 'period', 14)` where α = 2/(N+1). Traditional RSI (Wilder's) uses α = 1/N, which is more gradual. For exact Wilder smoothing with a 14-period lookback, use `avg(value, 'period', 27)`. Most modern platforms offer both variants.
7373
:::
7474

7575
:::info Related documentation

0 commit comments

Comments
 (0)