PolymarketClickHouse Workshops

04 Real-time aggregates

증분 materialized view가 유지하는 1분 호가 중간값 OHLC를 읽습니다.

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

시작 지점

collector가 healthy 상태이고 polymarket.price_ticks에 행이 있습니다.

왜

대시보드 사용자는 같은 1분 시계열을 반복해서 요청합니다. 새 블록이 도착할 때 한 번 계산해 두면 작업이 대시보드 새로 고침마다가 아니라 삽입 시점으로 이동합니다. 원본 테이블은 임의 질의를 위해 그대로 남습니다.

Step 1 — 집계 상태를 올바르게 쿼리

SELECT
    minute,
    token_id,
    round(argMinMerge(open) * 100, 2) AS open_percent,
    round(maxMerge(high) * 100, 2) AS high_percent,
    round(minMerge(low) * 100, 2) AS low_percent,
    round(argMaxMerge(close) * 100, 2) AS close_percent,
    countMerge(updates) AS updates
FROM polymarket.market_midpoints_1m
WHERE minute >= now() - INTERVAL 30 MINUTE
GROUP BY minute, token_id
ORDER BY minute DESC, token_id
LIMIT 30;

argMinState/argMaxState는 뷰가 기록한 것이고, 쿼리는 대응되는 Merge 함수로 이를 최종화합니다. open과 close는 이벤트 시각과 결정적 이벤트 ID를 함께 사용하므로 순서가 뒤바뀐 도착과 같은 밀리초 동률이 일관되게 처리됩니다.

Step 2 — 원본 쿼리와 집계 쿼리가 읽은 행 비교

원본 등가 쿼리를 실행하세요:

SELECT
    toStartOfMinute(event_at) AS minute,
    token_id,
    round(argMin(midpoint, tuple(event_at, event_id)) * 100, 2) AS open_percent,
    round(max(midpoint) * 100, 2) AS high_percent,
    round(min(midpoint) * 100, 2) AS low_percent,
    round(argMax(midpoint, tuple(event_at, event_id)) * 100, 2) AS close_percent,
    count() AS updates
FROM polymarket.price_ticks
WHERE midpoint > 0
  AND event_at >= now() - INTERVAL 30 MINUTE
  AND event_kind IN ('book_snapshot', 'price_change', 'best_bid_ask', 'rest_book')
GROUP BY minute, token_id
ORDER BY minute DESC, token_id
LIMIT 30;

SQL 콘솔에서 두 쿼리의 read rows를 비교하세요. 집계 쪽은 AggregatingMergeTree가 백그라운드에서 결합하는 블록 단위 상태를 읽으며, 보통 모든 원본 업데이트를 스캔하는 것보다 훨씬 적은 행을 읽습니다.

Step 3 — 뷰가 삽입 구동임을 검증

SELECT
    max(minute) AS newest_minute,
    dateDiff('second', newest_minute, now()) AS age_seconds,
    countMerge(updates) AS source_updates
FROM polymarket.market_midpoints_1m;

실시간 또는 픽스처 모드에서 newest_minute은 예약된 리프레시 작업 없이 전진합니다.

완료 조건

  • 집계 쿼리가 OHLC 행을 반환한다;
  • open/high/low/close가 0과 100 사이의 확률이다; 그리고
  • 활성 피드에서 newest_minute이 최신이다.

다음: 시장 움직임을 조사합니다.

이 페이지의 내용

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.

KO