Report single-sided traded shares and taker notional by event and market

This commit is contained in:
2026-12-03 05:15:50 +08:00
parent 7fd67e7c8e
commit fae9f26829
9 changed files with 212 additions and 2 deletions
+25
View File
@@ -0,0 +1,25 @@
-- ?1 selects an event's visible conditions; ?2 selects a single market.
WITH selected(market) AS MATERIALIZED (
SELECT s.market FROM listed_markets s
LEFT JOIN augmented_outcomes a ON a.market=s.market
WHERE s.event=?1 AND (a.role IS NULL OR a.role<>'placeholder')
UNION ALL SELECT id FROM markets WHERE id=?2
), executions AS (
SELECT s.market, f.seq, f.quantity, p.at_ms, f.price AS taker_price
FROM selected s CROSS JOIN orders o ON o.market=s.market AND o.side='buy'
CROSS JOIN fills f ON f.bid=o.seq LEFT JOIN trade_prices p ON p.fill=f.seq
UNION ALL
SELECT s.market, f.seq, f.quantity, f.at_ms,
CASE c.taker_order WHEN f.yes_order THEN f.yes_price
WHEN f.no_order THEN 100-f.yes_price END
FROM selected s CROSS JOIN paired_fills f ON f.market=s.market
LEFT JOIN execution_context c ON c.execution=f.seq
)
SELECT s.market, e.at_ms BETWEEN ?3-86400000 AND ?3 AS recent,
COALESCE(SUM(e.quantity),0),
COALESCE(SUM(e.quantity) FILTER (WHERE c.taker_order IS NOT NULL),0),
COALESCE(SUM(e.quantity*e.taker_price) FILTER (WHERE c.taker_order IS NOT NULL),0),
COALESCE(SUM(e.quantity) FILTER (WHERE c.execution IS NULL),0)
FROM selected s LEFT JOIN executions e ON e.market=s.market
LEFT JOIN execution_context c ON c.execution=e.seq
GROUP BY s.market,recent