Query editor
-- Caliber prototype v2 on Polymarket (markets resolved Jul 1-14, 2026)-- Fixes: (1) winner = affirmative/negative token name vs condition-level outcome-- (2) min-stake rule: a game counts only if max(buy_usd, sell_usd) >= $1WITH resolved_tokens AS (SELECT token_id, condition_id, MIN(resolved_on_timestamp) AS res_at,MAX(CASEWHEN lower(outcome) = 'yes' AND (token_outcome IN ('Yes','Up') OR token_outcome LIKE 'Over%') THEN 1WHEN lower(outcome) = 'no' AND (token_outcome IN ('No','Down') OR token_outcome LIKE 'Under%') THEN 1ELSE 0 END) AS won,MAX(CASE WHEN token_outcome IN ('Yes','Up','No','Down')OR token_outcome LIKE 'Over%' OR token_outcome LIKE 'Under%'THEN 1 ELSE 0 END) AS mappableFROM polymarket_polygon.market_detailsWHERE resolved_on_timestamp >= TIMESTAMP '2026-07-01'AND resolved_on_timestamp < TIMESTAMP '2026-07-14'AND lower(outcome) IN ('yes', 'no')GROUP BY 1, 2),fills AS (SELECT t.maker AS wallet, r.condition_id, r.res_at, r.won,CASE WHEN t.is_taker_side THEN t.taker_side ELSE t.maker_side END AS side,t.amount, t.shares, COALESCE(t.fee, 0) AS feeFROM polymarket_polygon.market_trades tJOIN resolved_tokens r ON t.asset_id = r.token_idWHERE t.block_month >= DATE '2026-05-01'AND t.shares > 0AND r.mappable = 1),pair AS (SELECT wallet, condition_id, MIN(res_at) AS res_at,SUM(CASE WHEN side = 'BUY' THEN -amount ELSE amount END+ CASE WHEN won = 1 THEN CASE WHEN side = 'BUY' THEN shares ELSE -shares END ELSE 0 END- fee) AS pnl,GREATEST(SUM(CASE WHEN side = 'BUY' THEN shares ELSE 0 END),SUM(CASE WHEN side = 'SELL' THEN shares ELSE 0 END), 0.01) AS basis,GREATEST(SUM(CASE WHEN side = 'BUY' THEN amount ELSE 0 END),SUM(CASE WHEN side = 'SELL' THEN amount ELSE 0 END)) AS stake_usdFROM fillsGROUP BY 1, 2),deltas AS (SELECT wallet, condition_id, pnl,LEAST(1.0, GREATEST(-1.0, pnl / basis)) AS s,CAST(CASE WHEN ROW_NUMBER() OVER (PARTITION BY wallet ORDER BY res_at, condition_id) <= 30THEN 64 ELSE 32 END AS double)* LEAST(1.0, GREATEST(-1.0, pnl / basis)) AS d_rawFROM pairWHERE stake_usd >= 1.0),cred AS (SELECT wallet,LEAST(1.0, n_mkts / 20.0)* LEAST(1.0, 2.0 / (1.0 + POWER(10, (1000 - r_raw) / 400.0))) AS mFROM (SELECT wallet, CAST(COUNT(*) AS double) AS n_mkts, 1000 + SUM(d_raw) AS r_rawFROM deltas GROUP BY 1)),mult AS (SELECT d.condition_id,COALESCE( (SUM(CASE WHEN d.pnl < 0 THEN -d.pnl * c.m ELSE 0 END) + GREATEST(SUM(d.pnl), 0))/ NULLIF(SUM(CASE WHEN d.pnl < 0 THEN -d.pnl ELSE 0 END) + GREATEST(SUM(d.pnl), 0), 0), 0) AS m_a