Quant research in SQL & ML
For quant researchers, data scientists, risk teams used to SQL and Python
The SQL your team already writes (CTEs, window functions, GROUP BY, joins) with EquationDB's indicators, statements and events as columns, finance functions built in, and models trained and scored in the database.
- Financial markets
- 12 steps
- Runs on live data
How EquationDB answers it
Each step is one query on live data. Run them in order, or open any one and adapt it: change the symbol, the station, the dates or the filter.
Sector breadth dashboard
GROUP BY and CASE over live indicators: trend health by sector in one statement.
SELECT sector, count(*) AS stocks, avg(pe) AS avg_pe, median(rsi(14)) AS median_rsi, sum(CASE WHEN close > sma(200) THEN 1 ELSE 0 END) * 100.0 / count(*) AS pct_above_200d FROM top500 GROUP BY sector ORDER BY pct_above_200d DESC
Momentum leaders per sector
Window ranking with QUALIFY replaces a subquery.
SELECT symbol, sector, roc(63) AS m3, rank() OVER (PARTITION BY sector ORDER BY roc(63) DESC) AS rk FROM top500 QUALIFY rk <= 2 ORDER BY sector, rk
Risk-adjusted leaders (engine functions)
Rolling Sharpe, Sortino, drawdown and VaR are engine functions: they screen 500 stocks instantly.
GET top500 SELECT symbol, sector, sharpe(252) AS sharpe_1y, sortino(252) AS sortino_1y, max_dd(252) AS mdd_1y, var_hist(252, 0.99) AS var99 SORT sharpe_1y DESC LIMIT 20
Asset-class scorecard
The same metrics as SQL aggregates over any history you choose.
SELECT symbol, sharpe(ret) AS sharpe, sortino(ret) AS sortino, max_drawdown(close) AS max_dd, cagr(close) AS cagr, value_at_risk(ret, 0.99) AS var_99 FROM bars WHERE symbol IN ('SPY', 'QQQ', 'TLT', 'GLD', 'IWM') AND timestamp >= '2021-01-01' GROUP BY symbol ORDER BY sharpe DESCBeta and alpha vs the market
CTEs and a join on time, then CAPM statistics as aggregates.
WITH s AS (SELECT timestamp, ret FROM bars WHERE symbol = 'NVDA' AND timestamp >= '2024-01-01'), m AS (SELECT timestamp, ret AS mkt FROM bars WHERE symbol = 'SPY' AND timestamp >= '2024-01-01') SELECT beta(s.ret, m.mkt) AS beta, alpha(s.ret, m.mkt) AS alpha_pct, corr(s.ret, m.mkt) AS corr, tracking_error(s.ret, m.mkt) AS te FROM s JOIN m ON s.timestamp = m.timestamp
Intraday VWAP
SAMPLE BY with FILL, named windows and LIMIT -n on 1-minute bars.
WITH ohlcv AS (SELECT timestamp, max(high) AS high, min(low) AS low, last(close) AS close, sum(volume) AS volume FROM bars WHERE symbol = 'AAPL' AND timestamp IN '$today' SAMPLE BY 15m FILL(PREV, PREV, PREV, 0)), sampled AS (SELECT timestamp, close, volume, ((high + low + close) / 3) * volume AS traded_value FROM ohlcv) SELECT timestamp, close, volume, SUM(traded_value) OVER w / SUM(volume) OVER w AS vwap FROM sampled WINDOW w AS (ORDER BY timestamp) LIMIT -8
Volume at price
DECLARE variables and bucketing: today's volume profile.
DECLARE @bucket := 0.5 SELECT floor(close / @bucket) * @bucket AS price, count(*) AS minutes, sum(volume) AS volume FROM bars WHERE symbol = 'AAPL' AND timestamp IN '$today' AND tf = '1m' GROUP BY price ORDER BY price
Option value across the market
Black–Scholes as a function, fed with each stock's realized volatility.
SELECT symbol, close, rv_cc(20) AS vol_20d, bs_call(close, round(close * 1.05), 30 / 365.0, 0.045, rv_cc(20) / 100) AS call_5pct_otm, bs_delta(close, round(close * 1.05), 30 / 365.0, 0.045, rv_cc(20) / 100, 'call') AS delta FROM top500 WHERE sector = 'Technology' ORDER BY mcap DESC LIMIT 10
Train a volatility forecast
In-database regression with a time-ordered 70/30 split and out-of-sample R².
CREATE OR REPLACE MODEL vol_fc TYPE linear TARGET fwd_vol AS SELECT timestamp, rv_cc(20) AS vol20, rv_cc(60) AS vol60, abs(ret) AS absret, stddev(ret) OVER (ORDER BY timestamp ROWS BETWEEN 1 FOLLOWING AND 20 FOLLOWING) * sqrt(252) AS fwd_vol FROM bars WHERE symbol = 'SPY' AND timestamp >= '2005-01-01' QUALIFY count(ret) OVER (ORDER BY timestamp ROWS BETWEEN 1 FOLLOWING AND 20 FOLLOWING) = 20
Score the forecast on every stock
predict() is a function: run the model created above across the market.
SELECT symbol, rv_cc(20) AS vol_now, predict('vol_fc', rv_cc(20), rv_cc(60), abs(ret)) AS vol_next_month FROM top500 ORDER BY vol_next_month DESC LIMIT 15Cluster the market into styles
k-means on momentum, volatility and valuation; then GROUP BY predict('styles', …) to profile each cluster.
CREATE OR REPLACE MODEL styles TYPE kmeans K 4 AS SELECT symbol, roc(63) AS momentum, rv_cc(20) AS volatility, pe FROM top500 WHERE pe > 0 AND pe < 100
Events joined with fundamentals
Any EquationDB statement is a table: this week's rarest events with each company's revenue growth.
SELECT e.symbol, e.event, e.rarity, f.revenue_yoy, f.period FROM (EVENTS * IN top500 LAST 5d WHERE rarity >= 95) e LEFT JOIN (FINANCIALS IN top500 SELECT revenue_yoy, period WHERE period <> 'FY') f ON e.symbol = f.symbol ORDER BY e.rarity DESC LIMIT 20
Run it on your own question
Sign in and press Try it on any step, or ask for a short demo on your data.
More financial markets use cases
- Finding alphaQuant researchers, systematic PMs
- Factor & cross-sectional researchQuant PMs, factor and risk teams
- Discovery & backtestingQuant researchers, systematic strategy teams
- Relationships & contagionRisk managers, macro and relative-value PMs
- Themes & supply chainThematic investors, sector analysts and anyone mapping who sells to whom