PolymarketClickHouse Workshops

05 Điều tra biến động

Trả lời bốn câu hỏi vận hành về thị trường bằng SQL ClickHouse tường minh.

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

Điểm khởi đầu

Các bảng thô và bảng một phút đều đang chứa dữ liệu hiện thời.

Câu hỏi 1 — Xác suất hiện tại là bao nhiêu?

SELECT
    m.token_id,
    m.question,
    m.outcome,
    round(argMax(t.midpoint, t.event_at) * 100, 2) AS probability_percent,
    max(t.event_at) AS last_update
FROM polymarket.price_ticks AS t
INNER JOIN
(
    SELECT token_id, question, outcome
    FROM polymarket.markets FINAL
) AS m ON m.token_id = t.token_id
WHERE t.midpoint > 0
  AND t.event_at >= now() - INTERVAL 30 MINUTE
GROUP BY m.token_id, m.question, m.outcome
ORDER BY m.question, m.outcome;

Midpoint là một xác suất mang tính chỉ báo, suy ra từ best bid và best ask, chứ không phải một cam kết về giá có thể giao dịch được.

Câu hỏi 2 — Kết quả nào biến động mạnh nhất?

WITH now() AS current_time
SELECT
    m.token_id,
    m.question,
    m.outcome,
    round(argMaxIf(t.midpoint, t.event_at, t.event_at > current_time - INTERVAL 1 MINUTE) * 100, 2) AS now_percent,
    round(argMaxIf(t.midpoint, t.event_at, t.event_at <= current_time - INTERVAL 5 MINUTE) * 100, 2) AS five_minutes_ago_percent,
    round(now_percent - five_minutes_ago_percent, 2) AS move_points
FROM polymarket.price_ticks AS t
INNER JOIN
(
    SELECT token_id, question, outcome
    FROM polymarket.markets FINAL
) AS m ON m.token_id = t.token_id
WHERE t.midpoint > 0
  AND t.event_at >= current_time - INTERVAL 15 MINUTE
GROUP BY m.token_id, m.question, m.outcome
HAVING now_percent > 0 AND five_minutes_ago_percent > 0
ORDER BY abs(move_points) DESC;

Nếu kết quả rỗng, feed chưa tích lũy đủ năm phút. Hãy tiếp tục với các truy vấn sau và quay lại đây sau.

Câu hỏi 3 — Spread đang rộng hay dữ liệu đã cũ?

SELECT
    m.token_id,
    m.question,
    m.outcome,
    round(argMax(t.best_bid, t.event_at) * 100, 2) AS bid_percent,
    round(argMax(t.best_ask, t.event_at) * 100, 2) AS ask_percent,
    round(ask_percent - bid_percent, 2) AS spread_points,
    dateDiff('second', max(t.event_at), now()) AS age_seconds
FROM polymarket.price_ticks AS t
INNER JOIN
(
    SELECT token_id, question, outcome
    FROM polymarket.markets FINAL
) AS m ON m.token_id = t.token_id
WHERE t.best_bid > 0
  AND t.best_ask > 0
  AND t.event_at >= now() - INTERVAL 30 MINUTE
GROUP BY m.token_id, m.question, m.outcome
ORDER BY spread_points DESC;

Một biến động đi kèm spread rộng hoặc quote cũ thì đáng tin cậy ít hơn so với một thị trường mới và có spread hẹp.

Câu hỏi 4 — Khối lượng giao dịch gần đây có tăng tốc không?

SELECT
    condition_id,
    token_id,
    title,
    outcome,
    round(sumIf(price * size, event_at >= now() - INTERVAL 5 MINUTE), 2) AS current_5m_usd,
    round(sumIf(
        price * size,
        event_at >= now() - INTERVAL 10 MINUTE
          AND event_at < now() - INTERVAL 5 MINUTE
    ), 2) AS previous_5m_usd,
    round(current_5m_usd / greatest(previous_5m_usd, 0.01), 2) AS velocity_ratio
FROM polymarket.trades_clean
WHERE event_at >= now() - INTERVAL 10 MINUTE
GROUP BY condition_id, token_id, title, outcome
ORDER BY current_5m_usd DESC;

Đây là khối lượng khớp lệnh công khai được biểu diễn dưới dạng price * size; nó là phân tích, không phải một khuyến nghị.

Hoàn thành khi

Ít nhất các truy vấn về xác suất hiện tại, spread/độ tươi mới và khối lượng đều trả về không lỗi. Sau năm phút, truy vấn về thị trường biến động mạnh cũng phải trả về dữ liệu.

Tiếp theo: xuất bản dashboard trên Cloud.

Trên trang này

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.

VI