How to Build a Polymarket Trading Bot After TWAP Implemented
How to Build a Polymarket Trading Bot After TWAP Implemented
如何在 TWAP 机制实施后构建 Polymarket 交易机器人
Polymarket switched its short-duration crypto up/down markets (BTC, ETH, SOL, XRP, and others) to Time-Weighted Average Price (TWAP) resolution on August 7, 2026, at 00:00 UTC. This replaced single-price snapshot settlement with Chainlink-powered averages, sharply reducing last-second manipulation. For bot builders, the change is material. Strategies that relied on predicting or reacting to a single expiry-tick price no longer work the same way. Bots must now incorporate continuous TWAP feeds, model averages over the lookback window, and adapt signals and risk rules accordingly. This article covers the full process of building a production-oriented trading bot in the post-TWAP environment.
Polymarket 于 2026 年 8 月 7 日 00:00 UTC 将其短期加密货币涨跌市场(BTC、ETH、SOL、XRP 等)切换为时间加权平均价格 (TWAP) 结算机制。此举以 Chainlink 驱动的平均价格取代了单一价格快照结算,大幅减少了最后一秒的价格操纵。对于机器人开发者而言,这一变化至关重要。那些依赖于预测或响应单一到期时刻价格的策略已不再适用。机器人现在必须整合连续的 TWAP 数据流,对回溯窗口内的平均值进行建模,并相应地调整信号和风险规则。本文将涵盖在 TWAP 后时代构建生产级交易机器人的全过程。
What Changed
机制变化
-
5-minute markets: Resolve against a 30-second Chainlink TWAP.
-
15-minute and 4-hour markets: Resolve against a 60-second Chainlink TWAP.
-
5 分钟市场:根据 30 秒的 Chainlink TWAP 结算。
-
15 分钟和 4 小时市场:根据 60 秒的 Chainlink TWAP 结算。
Both the opening price (the “price to beat”) and the final settlement price come from the applicable TWAP feed. The averaging window is a trailing lookback immediately before market close. Sustaining a manipulated price across the full window is far more expensive than a single-tick push, which is the core integrity improvement. Liquidity rewards of $1 million were also rolled out across affected markets through August 2026 to support depth during the transition.
开盘价(即“目标价格”)和最终结算价均来自相应的 TWAP 数据源。平均窗口为市场关闭前紧邻的追溯期。在整个窗口期内维持操纵价格的成本远高于单点推高价格,这是此次机制改进的核心。此外,Polymarket 在 2026 年 8 月前向受影响的市场投放了 100 万美元的流动性奖励,以支持过渡期间的市场深度。
Prerequisites
前置条件
-
Python 3.11+ (recommended for most bots) or Node.js 24+
-
Funded Polygon wallet holding pUSD (or USDC.e that can be wrapped/bridged via Polymarket flows) plus a small amount of POL/MATIC for any residual gas
-
Private key for an EOA or appropriate proxy/Safe setup
-
Basic familiarity with async programming, WebSockets, and order-book concepts
-
VPS or always-on host with low-latency connectivity to Polymarket endpoints (US East often works well)
-
Python 3.11+(推荐大多数机器人使用)或 Node.js 24+
-
已充值的 Polygon 钱包,持有 pUSD(或可通过 Polymarket 流程封装/桥接的 USDC.e),以及少量用于支付剩余 Gas 费的 POL/MATIC
-
用于 EOA(外部账户)的私钥或适当的代理/Safe 设置
-
对异步编程、WebSocket 和订单簿概念的基本了解
-
具备低延迟连接至 Polymarket 端点的 VPS 或常驻主机(美国东部节点通常效果良好)
Install the current official SDKs: 安装当前官方 SDK:
# Python (unified client recommended)
pip install polymarket-client
# TypeScript
npm install @polymarket/client
Older py-clob-client / @polymarket/clob-client packages are deprecated; migrate to the unified or v2 clients.
旧版的 py-clob-client / @polymarket/clob-client 软件包已被弃用;请迁移至统一客户端或 v2 客户端。
Accessing Real-Time TWAP Data
获取实时 TWAP 数据
Polymarket exposes Chainlink-computed 30 s and 60 s TWAPs via two paths. The recommended production route is Polymarket’s public Real-Time Data Streaming (RTDS) WebSocket—no Chainlink credentials required.
Polymarket 通过两条路径提供由 Chainlink 计算的 30 秒和 60 秒 TWAP 数据。推荐的生产环境路径是 Polymarket 的公共实时数据流 (RTDS) WebSocket——无需 Chainlink 凭证。
(Code examples omitted for brevity, please refer to the official documentation for implementation details.) (为简洁起见,此处省略代码示例,具体实现细节请参考官方文档。)
Always treat the TWAP value as a high-precision decimal/string; do not coerce to floating-point early.
请务必将 TWAP 值视为高精度的小数/字符串;不要过早将其强制转换为浮点数。
Core Bot Architecture
核心机器人架构
A robust post-TWAP bot separates concerns: 一个稳健的 TWAP 后时代机器人应实现关注点分离:
-
Data Layer — RTDS TWAP stream + CLOB WebSocket (order books, trades, market lifecycle) + Gamma/Data API for discovery and positions.
-
Signal / Strategy Engine — Compares current TWAP trajectory, predicted average at expiry, external spot prices, and Polymarket odds.
-
Risk Engine — Position limits, daily loss caps, max notional per market, inventory skew, kill switches.
-
Execution Layer — Order construction, signing (EIP-712), submission via CLOB, cancellation, and fill confirmation.
-
Monitoring & Logging — Persistent logs, PnL tracking, alerts (Telegram/Discord), and health checks.
-
数据层 — RTDS TWAP 数据流 + CLOB WebSocket(订单簿、交易、市场生命周期)+ 用于发现和持仓的 Gamma/Data API。
-
信号/策略引擎 — 比较当前 TWAP 轨迹、到期时的预测平均值、外部现货价格以及 Polymarket 赔率。
-
风险引擎 — 持仓限额、每日亏损上限、单市场最大名义价值、库存偏离度、熔断开关。
-
执行层 — 订单构建、签名 (EIP-712)、通过 CLOB 提交、撤单及成交确认。
-
监控与日志 — 持久化日志、盈亏追踪、警报(Telegram/Discord)及健康检查。
Strategy Considerations After TWAP
TWAP 后的策略考量
Legacy “last-second sniping” or pure expiry-tick prediction loses effectiveness. Useful post-TWAP approaches include: 传统的“最后一秒狙击”或纯粹的到期时刻价格预测已失去效力。TWAP 后的有效策略包括:
-
Modeling the running TWAP and projecting the final average given recent volatility.
-
Mean-reversion or momentum signals that incorporate the full window rather than a single print.
-
Market-making that earns spread + liquidity rewards while managing inventory against the expected TWAP path.
-
Cross-venue arbitrage that accounts for the averaging window instead of instantaneous price.
-
Probability models that output fair value for the Up/Down token and trade when the order book diverges meaningfully after costs and fees.
-
对运行中的 TWAP 进行建模,并根据近期波动率预测最终平均值。
-
结合整个窗口期而非单一价格点的均值回归或动量信号。
-
在管理库存以匹配预期 TWAP 路径的同时,通过做市赚取点差和流动性奖励。
-
考虑平均窗口而非瞬时价格的跨平台套利。
-
输出涨跌代币公允价值的概率模型,并在订单簿在扣除成本和费用后出现显著偏离时进行交易。
Because both open and close reference TWAP, directional bias must be calculated relative to the opening TWAP, not a single tick.
由于开盘和收盘均参考 TWAP,方向性偏差必须相对于开盘 TWAP 计算,而非单一时刻的价格。
Risk Management Essentials
风险管理要点
-
Hard per-market and portfolio notional limits.
-
Daily / weekly loss circuit breakers that pause or flatten.
-
Maximum open orders and position concentration rules.
-
Inventory-aware quoting (skew quotes when long/short).
-
严格的单市场和投资组合名义价值限制。
-
每日/每周亏损熔断机制,用于暂停交易或平仓。
-
最大挂单量和持仓集中度规则。
-
库存感知报价(在多头/空头持仓时调整报价偏离度)。