EquationDB

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.

  1. 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
    Try it →
  2. 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
    Try it →
  3. 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
    Try it →
  4. 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 DESC
    Try it →
  5. Beta 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
    Try it →
  6. 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
    Try it →
  7. 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
    Try it →
  8. 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
    Try it →
  9. 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
    Try it →
  10. 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 15
    Try it →
  11. Cluster 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
    Try it →
  12. 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
    Try it →

Run it on your own question

Sign in and press Try it on any step, or ask for a short demo on your data.

Request a trial

More financial markets use cases