documentation/cookbook/sql/finance/rate-of-change.md
Rate of Change (ROC) measures the percentage change in price between the current price and the price N periods ago. It oscillates around zero, with positive values indicating upward momentum and negative values indicating downward momentum.
You want a simple momentum indicator that shows how fast price is changing. Raw price differences don't account for the price level, making comparison across assets difficult.
DECLARE
@symbol := 'EURUSD',
@lookback := '$now - 1M..$now'
WITH with_lag AS (
SELECT
timestamp,
symbol,
close,
lag(close, 12) OVER (PARTITION BY symbol ORDER BY timestamp) AS close_12_ago
FROM market_data_ohlc_15m
WHERE symbol = @symbol
AND timestamp IN @lookback
)
SELECT
timestamp,
symbol,
round(close, 5) AS close,
round(close_12_ago, 5) AS close_12_ago,
round((close - close_12_ago) / close_12_ago * 100, 4) AS roc
FROM with_lag
WHERE close_12_ago IS NOT NULL
ORDER BY timestamp;
The formula: ROC = ((Close - Close N periods ago) / Close N periods ago) × 100
:::note Period selection Common ROC periods:
:::info Related documentation