PolymarketClickHouse Workshops

07 AI market analyst

Give a ClickHouse Agent your live market tables, make it detect and investigate a move on its own, then referee its answer with deterministic SQL.

Your computer
macOS terminal: Run workshop commands in Terminal using zsh or bash.

Starting point

The collector is healthy in live, degraded, or fixture mode, the Module 05 queries return, and you are signed in to the organization that owns polymarket-workshop. No new API key or local process is needed: ClickHouse Agents runs in Cloud under the identity you created in Module 00.

Why

Module 05 answered four questions you had already written down. A market does not wait for your question list. The useful shape is an on-call analyst: notice a move, form a hypothesis, write the queries that test it, and report a verdict with its evidence. That loop also fails quietly, so the last two steps referee the agent rather than trust it.

Step 1 — Open ClickHouse Agents and connect this service

open 'https://ai.clickhouse.cloud'

The Cloud console reaches the same place: your service, then ClickHouse agents.

Create an agent named Polymarket analyst, add the ClickHouse tool, point it at polymarket-workshop, and confirm it reports Connected. Without a connected tool the agent guesses at your schema. Paste this as its instructions; every line is there because agents get it wrong without it:

You answer questions about live public prediction-market data in the polymarket database
of this service. Rules:
- Probability is the quote midpoint: polymarket.price_ticks where midpoint > 0, or the
  merged close of polymarket.market_midpoints_1m. A last_trade_price tick is not one.
- market_midpoints_1m holds AggregateFunction states. Read them only through
  argMinMerge(open), maxMerge(high), minMerge(low), argMaxMerge(close) and
  countMerge(updates), grouped by minute, token_id.
- Metadata comes from polymarket.markets FINAL, trades from polymarket.trades_clean.
- One condition_id per market, one token_id per outcome. A Yes move and its No
  counterpart are one event, not two findings.
- Timestamps are UTC and the newest minute is usually still filling.
- Show the SQL you ran and the age of the data behind every number.
- Public-data analysis only. Never give trading advice.

Step 2 — Turn one: let it find the move

Using the ClickHouse tool, find the largest midpoint move in the last 30 minutes of
polymarket.market_midpoints_1m. For each token_id compare the merged close of the most
recent complete minute with the merged close five minutes earlier. Report the question,
the outcome, the token_id, both probabilities in percent, the move in percentage points,
and the two minutes you compared. Show the SQL.

Expected: one named outcome, a signed move in points, and SQL that merges the aggregate states instead of selecting the raw state columns.

Step 3 — Turn two: make it investigate its own finding

Investigate that move before you believe it. From polymarket.price_ticks report the
latest best_bid, best_ask, spread in percentage points, and quote age in seconds for that
token_id. From polymarket.trades_clean compare matched volume as price * size over the
five minutes covering the move against the previous five minutes. Then give a verdict of
corroborated, weakly corroborated, or likely artifact, and name the evidence behind it.

You never said how those three signals combine. Choosing that is what makes this an agent rather than a text-to-SQL box.

Step 4 — Turn three: make it attack its own verdict

List every assumption in that verdict that could be wrong, and for each one the single
query that would falsify it. Run the two you consider most likely to be wrong, then tell
me whether the verdict survives.

A useful answer names the partial newest minute, the quote age, and how few updates sit behind a thin one. Record whether the verdict survived.

Step 5 — Referee the agent with deterministic SQL

Run this in the Cloud SQL console. It answers Step 2 without an agent, so its top row is your reference:

WITH per_minute AS
(
    SELECT
        token_id,
        minute,
        argMaxMerge(close) AS close_midpoint
    FROM polymarket.market_midpoints_1m
    WHERE minute >= now() - INTERVAL 30 MINUTE
      AND minute < toStartOfMinute(now())
    GROUP BY token_id, minute
)
SELECT
    m.question,
    m.outcome,
    p.token_id,
    round(argMax(p.close_midpoint, p.minute) * 100, 2) AS latest_percent,
    round(argMin(p.close_midpoint, p.minute) * 100, 2) AS oldest_percent,
    round(latest_percent - oldest_percent, 2) AS move_points,
    min(p.minute) AS window_start,
    max(p.minute) AS window_end
FROM per_minute AS p
INNER JOIN
(
    SELECT token_id, question, outcome
    FROM polymarket.markets FINAL
) AS m ON m.token_id = p.token_id
GROUP BY m.question, m.outcome, p.token_id
ORDER BY abs(move_points) DESC
LIMIT 5;

The windows differ on purpose: this spans the oldest and newest complete minutes present, while the agent was asked for a five-minute step. Compare token_id, the sign of move_points, and the magnitude, not the decimals. Then check the agent's corroboration numbers against the Spread and freshness and Volume velocity queries saved in Module 06.

Step 6 — Write down what the agent got wrong

At least one of these usually appears. Find yours in the transcript:

  • selecting close or high from market_midpoints_1m without a Merge function, then explaining whatever came back with full confidence;
  • treating the newest, still-filling minute as complete, which exaggerates the last leg;
  • reporting a Yes token and its No token as two independent movers;
  • dropping FINAL on polymarket.markets, so a rediscovered market joins twice;
  • calling the midpoint a tradeable price, or quoting a move without its age.

If its numbers matched the referee exactly, test the partial minute directly: ask whether its newest minute was complete, then compare max(minute) in polymarket.market_midpoints_1m with toStartOfMinute(now()). Equal values mean it was not, and an agent that said otherwise stated something false about live data.

Done when

  • the agent's detection SQL reads market_midpoints_1m through Merge functions;
  • its verdict cites spread, quote age, and trade volume, not price alone;
  • the referee query's top row agrees with the agent on token and direction, or you can explain the difference; and
  • you have written down one thing the agent got wrong or overstated.

Next: wrap up and clean up.

On this page

Track your progress?

Optional. We email a link to confirm your address; progress records once you open it.

Please use your work email address, not a personal one.

Progress tracking also requires accepting the current Terms of Service in Privacy settings.

EN