Query editor
-- Per-order USD notional vs effective spread (bps), by ticker (plus pooled "All")WITH marketables AS (SELECTsource,month,ticker,CAST(order_type_code AS INTEGER) AS order_type_code,CAST(order_size_code AS INTEGER) AS order_size_code,CAST(num_covered_orders AS BIGINT) AS num_orders,CAST(shares_total AS BIGINT) AS shares_total,CAST(avg_effective_spread_usd AS DOUBLE) AS avg_eff_spread_usd,CAST(avg_realized_spread_usd AS DOUBLE) AS avg_real_spread_usdFROM dune.rafal0x.dataset_tradfi_spreadsWHERE CAST(order_type_code AS INTEGER) IN (11, 12) -- market + marketable limit),with_ref AS (SELECTm.*,p.ref_price_usdFROM marketables mJOIN dune.rafal0x.dataset_ref_prices pON p.month = m.month AND p.ticker = m.ticker),-- Shares-weight within (source, ticker, size_code) across any row splitsper_source AS (SELECTsource,month,ticker,order_size_code,ref_price_usd,SUM(shares_total) AS shares_total,SUM(num_orders) AS num_orders,-- Effective in $: ABS() before weightingSUM(shares_total * ABS(avg_eff_spread_usd)) / NULLIF(SUM(shares_total),0) AS eff_spread_usd,-- Realized in $: signed (kept for optional secondary plot)SUM(shares_total * avg_real_spread_usd) / NULLIF(SUM(shares_total),0) AS real_spread_usdFROM with_refGROUP BY 1,2,3,4,5),-- Convert to per-order notional and bpsper_source_pts AS (SELECTsource,month,ticker,order_size_code,-- per-order notional (USD)(CAST(shares_total AS DOUBLE) / NULLIF(num_orders,0)) * ref_price_usd AS est_notional_usd,(eff_spread_usd / ref_price_usd) * 10000 AS eff_spread_bps,(real_spread_usd / ref_price_usd) * 10000 AS real_spread_bpsFROM per_source),-- Pooled across wholesalers ("All"), still by (ticker, size_code)pooled AS (SELECT