🎯 Polymarket 初心者入門|予測市場・CLOB・公式Python SDKを無料で触ってみる
「Polymarketで遊んでみたいけど、そもそも仕組みがよく分からない」 「有料のトレーディングツールを買わないと分析できないんでしょ?」
どちらの悩みも、この記事で解決します。認証不要・秘密鍵不要・Python 3.9があれば今日から使える、Polymarketの公式Python SDKハンズオンです。
Polymarketとは
Polymarketは「はい / いいえで答えられる将来の出来事」に、お金を賭けられる予測市場プラットフォームです。例えば:
- 「Anthropic IPO before 2027?(AnthropicはIPOする?)」
- 「Will 2026 be the second-hottest year on record?(2026年は観測史上2位の暑さになる?)」
- 「Will the Democrats win the Ohio governor race in 2026?(オハイオ州知事選を民主党が勝つ?)」
面白いのが、賭けられている価格そのものが「確率」になるところ。
🍱 お弁当屋さんで例えると
「明日このお弁当が完売する?」という質問に、YESが$0.70、NOが$0.30で取引されていたら、市場全体の予想は70%の確率で完売するという意味になります。もし完売したらYESを持ってる人が$1.00で精算され、30%の儲け($0.30 → $1.00)。外れたら0になります。
普通のFXや株と違って、確率そのものが値段になるのがユニークなところ。
CLOBって何やねん
Polymarketのドキュメントを読むとCLOBという単語が100回くらい出てきます。これが初心者殺し。
CLOB = Central Limit Order Book(中央指値注文板)
…って言われてもピンと来ませんよね。
🏷️ フリマアプリで例えると
フリマに「このYESを$0.48で買いたい人10人」「$0.51で売りたい人5人」がいたら、その値段表がCLOBです。普通のFXの板情報とまったく同じ仕組み。「買いたい最高値」と「売りたい最安値」の間の差がスプレッドで、ここが狭いほど流動性が高いイイ市場。
Polymarketは完全にこの板取引で動いてます。だから「今いくら?」は単一の数字じゃなくて板情報として取得するのが正確です。
準備
# Python 3.9以上が必要python --version
# 作業用フォルダを作って仮想環境mkdir poly_demo && cd poly_demopython -m venv .venv
# Windows.venv\Scripts\activate# Mac/Linuxsource .venv/bin/activate
# 公式SDKと必要ライブラリをインストールpip install py-clob-client pandas requests⚠️ Windowsでのつまずき:
print()で絵文字や—を出力するときUnicodeEncodeErrorが出ます。解決策:Bashset PYTHONIOENCODING=utf-8python your_script.py
① 疎通確認(5秒で動く)
# 00_ping.pyfrom py_clob_client.client import ClobClient
client = ClobClient("https://clob.polymarket.com")print("ok:", client.get_ok())print("time:", client.get_server_time())実行結果:
ok: OKtime: 1775886703これだけで疎通完了。Polymarket公式APIに認証なしでつながりました🎉
② アクティブな市場を探す
注意点:普通にget_markets()すると**昔の終わった市場(closed=True)**がドバッと返ってくるので、get_sampling_markets()を使うのがコツです。これは「実際に取引されている報酬付き市場」を返してくれます。
# 01_list_markets.pyfrom py_clob_client.client import ClobClient
client = ClobClient("https://clob.polymarket.com")resp = client.get_sampling_markets(next_cursor="")data = resp.get("data", [])
candidates = []for m in data: if m.get("closed") or not m.get("active"): continue yes = next((t for t in m.get("tokens", []) if t.get("outcome") == "Yes"), None) if not yes or yes.get("price") is None: continue p = yes["price"] if 0.05 < p < 0.95: candidates.append((p, m))
# 中間に近い順(=市場が悩んでる順)candidates.sort(key=lambda x: abs(x[0] - 0.5))
for p, m in candidates[:5]: print(f"YES={p} Q: {m.get('question', '')[:70]}")実行結果(実際のスナップショット):
YES=0.5 Q: Will Scott Wiener receive the most votes in the CA-11 primary?YES=0.495 Q: Anthropic IPO before 2027?YES=0.505 Q: Will Bert Mizusawa be the Republican nominee for Senate in Virginia?YES=0.495 Q: Will Chainlink dip to $6 by December 31, 2026?YES=0.505 Q: Will the Democrats win the Ohio governor race in 2026?😳「Anthropic IPO before 2027?」がまさに五分五分。市場が本気で悩んでる証拠ですね。せっかくなのでこの市場を深掘りしてみましょう。
③ 板情報(Order Book)を見る
市場の「リアルな値段」は板情報にあります。これが分からないと何もできません。
# 02_orderbook.pyfrom py_clob_client.client import ClobClient
client = ClobClient("https://clob.polymarket.com")
# "Anthropic IPO before 2027?" のYESトークンIDYES_TOKEN = "31650083965282390269113502048883725495895331661148956202621012482964650485070"
book = client.get_order_book(YES_TOKEN)
print("[ BIDS 買い注文 ]")for b in sorted(book.bids, key=lambda x: -float(x.price))[:5]: print(f" price={b.price} size={b.size}")
print("\n[ ASKS 売り注文 ]")for a in sorted(book.asks, key=lambda x: float(x.price))[:5]: print(f" price={a.price} size={a.size}")
print(f"\nmidpoint: {client.get_midpoint(YES_TOKEN)}")print(f"best bid: {client.get_price(YES_TOKEN, 'BUY')}")print(f"best ask: {client.get_price(YES_TOKEN, 'SELL')}")print(f"spread : {client.get_spread(YES_TOKEN)}")実行結果:
[ BIDS 買い注文 ] price=0.48 size=50 price=0.47 size=351 price=0.46 size=66
[ ASKS 売り注文 ] price=0.51 size=43 price=0.52 size=245 price=0.53 size=266
midpoint: {'mid': '0.495'}best bid: {'price': '0.48'}best ask: {'price': '0.51'}spread : {'spread': '0.03'}読み方:
- BID(買い注文)の最高値 $0.48 = 「$0.48なら買ってあげる」という人が50株分待ってる
- ASK(売り注文)の最安値 $0.51 = 「$0.51でなら売る」という人が43株分待ってる
- スプレッド $0.03 = その差。狭いほど流動性◎
- midpoint $0.495 = 中値。「AnthropicがIPO前にIPOする確率≒49.5%」ということ
④ 価格履歴をCSVに保存
履歴データは公式SDKに直接メソッドが無いので、公開REST APIを直接叩きます。これも認証不要。
# 03_price_history.pyimport csv, time, requests
YES_TOKEN = "31650083965282390269113502048883725495895331661148956202621012482964650485070"
url = "https://clob.polymarket.com/prices-history"params = { "market": YES_TOKEN, "interval": "1d", # 1h, 1d, 1w, max "fidelity": 60, # 分単位}
r = requests.get(url, params=params, timeout=15)history = r.json().get("history", [])
with open("price_history.csv", "w", newline="", encoding="utf-8") as f: w = csv.writer(f) w.writerow(["timestamp_unix", "datetime_utc", "price"]) for pt in history: dt = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(pt["t"])) w.writerow([pt["t"], dt, pt["p"]])
prices = [p["p"] for p in history]print(f"points={len(history)} min={min(prices)} max={max(prices)} last={prices[-1]}")実行結果:
points=9 min=0.495 max=0.55 last=0.49524時間で$0.55→$0.495まで下がってますね。誰かが「やっぱりIPO無理では」と見て売ったんでしょうか。
つまずきポイント集
| つまずき | 対処 |
|---|---|
UnicodeEncodeError: 'cp932'(Windows) | set PYTHONIOENCODING=utf-8 |
get_markets()で終わった市場ばかり返ってくる | get_sampling_markets()を使う |
| YES価格が0か1ばかり | その市場はもう決着済み。active=True & closed=Falseでフィルタ |
py-clob-clientのインストールでeth-account周りが遅い | Rust依存ビルドが走るため。5〜10分待てば終わる |
| 注文を出そうとしたら秘密鍵を要求される | 読むだけなら秘密鍵不要。注文を出す時だけ必要 |
まとめ
py-clob-clientでPolymarketの市場一覧・板情報・価格履歴が完全無料で取れる- 認証も秘密鍵も不要。
ClobClient("https://clob.polymarket.com")だけで動く get_sampling_markets()で実際に取引されている市場を取得するのがコツ- 板情報のbid/ask/spreadを読めば、その市場の流動性と実質価格が分かる
これがPolymarket分析の基本セットです。ここから先、可視化したり、バックテストしたり、複数ウォレットの追跡をしたり、色々な応用ができます。地味だけど、これが全ての土台です🔥
参考リンク
🎁おすすめツール・サービス
※上記リンクにはプロモーション(広告)が含まれています
量子相場謀略|ホソノP
ブログでは書けない本音のトレード戦略とEA開発の裏側を公開。 データに基づいた検証で、再現性のある手法だけをお届けします。