PolyClaw
Trading-enabled Polymarket skill for OpenClaw. Browse markets, manage wallets, execute trades, and track positions.
Features
- - Market Browsing - Search and browse Polymarket prediction markets
- Wallet Management - Env-var based wallet configuration
- Trading - Buy YES/NO positions via split + CLOB execution
- Position Tracking - Track entry prices, current prices, and P&L
- Hedge Discovery - LLM-powered covering portfolio discovery via logical implications
Quick Start
First, install dependencies (from skill directory):
CODEBLOCK0
First-Time Setup (Required for Trading)
Before your first trade, set Polymarket contract approvals (one-time, costs ~0.01 POL in gas):
CODEBLOCK1
This submits 6 approval transactions to Polygon. You only need to do this once per wallet.
Browse Markets
CODEBLOCK2
Output options:
- - Default output is a formatted table (good for display)
- Use
--full flag for full question text without truncation - Use
--json flag via scripts/markets.py --json trending for structured JSON output
Wallet Management
CODEBLOCK3
The wallet is configured via the POLYCLAW_PRIVATE_KEY environment variable.
Trading
CODEBLOCK4
Positions
CODEBLOCK5
Hedge Discovery
Find covering portfolios - pairs of market positions that hedge each other via contrapositive logic.
CODEBLOCK6
Output options:
- - Default output is a formatted table showing Tier, Coverage, Cost, Target, and Cover
- Use
--json flag for structured JSON output - Use
--min-coverage 0.90 to filter by minimum coverage (default 0.85) - Use
--tier 1 to filter by tier (1=best, default 2)
Coverage tiers:
- - Tier 1 (HIGH): >=95% coverage - near-arbitrage opportunities
- Tier 2 (GOOD): 90-95% - strong hedges
- Tier 3 (MODERATE): 85-90% - decent but noticeable risk
- Tier 4 (LOW): <85% - speculative (filtered by default)
LLM model: Uses nvidia/nemotron-nano-9b-v2:free via OpenRouter. Model selection matters — some models find spurious correlations while others (like DeepSeek R1) have output format issues. Override with --model <model_id> if needed.
Automation Tools
Portfolio Live Tracking
Real-time position tracking via Polymarket Data API. Groups positions by (slug, outcome) and displays YES/NO side, current value, and P&L with cost basis from portfolio.json.
CODEBLOCK7
- - Fetches live on-chain positions from INLINECODE11
- Shows outcome side (YES/NO), current price, and unrealized P&L
- No web3 dependency — uses REST API only
Auto Redeem
Automatically detects and redeems settled (resolved) markets. Checks payoutDenominator on-chain via the CTF contract. When a market resolves, submits a redeemPositions transaction through the NegRiskAdapter.
CODEBLOCK8
- - Requires
web3 (use .venv/bin/python3, not system python) - Tracks redeemed positions in
redeem_state.json to avoid duplicates - Needs
CHAINSTACK_NODE and POLYCLAW_PRIVATE_KEY env vars
Discipline Scanner
Automated take-profit scanner. Sells positions that are up 20%+ with slippage protection. Configurable via cfo_params.json (DISCIPLINE_TP_PCT key).
CODEBLOCK9
- - Skips positions worth less than $1
- Take-profit threshold defaults to 20% (configurable)
Enhanced API (polyclaw_api.py)
CLI bridge for external integrations (e.g., Wells TG Bot via SSH). Provides structured JSON output for programmatic use.
CODEBLOCK10
- -
portfolio is the recommended command for live position data (reads Data API, not local DB) - INLINECODE22 requires
web3 — use INLINECODE24 - INLINECODE25 reads local DB and can be stale — prefer
portfolio for accuracy
Security
For the MVP, the private key is stored in an environment variable for simplicity and Claude Code compatibility.
Security Warning: Keep only small amounts in this wallet. Withdraw regularly to a secure wallet.
Environment Variables
| Variable | Required | Description |
|---|
| INLINECODE27 | Yes (trading) | Polygon RPC URL |
| INLINECODE28 |
Yes (hedge) | OpenRouter API key for LLM hedge discovery |
|
POLYCLAW_PRIVATE_KEY | Yes (trading) | EVM private key (hex, with or without 0x prefix) |
|
HTTPS_PROXY | Recommended | Rotating residential proxy for CLOB (e.g., IPRoyal) |
|
CLOB_MAX_RETRIES | No | Max CLOB retries with IP rotation (default: 5) |
Security Warning: Keep only small amounts in this wallet. Withdraw regularly to a secure wallet. The private key in an env var is convenient for automation but less secure than encrypted storage.
Trading Flow
- 1. Split Position - USDC.e is split into YES + NO tokens via CTF contract
- Sell Unwanted - The unwanted side is sold via CLOB order book
- Result - You hold the wanted position, recovered partial cost from selling unwanted
Example: Buy YES at $0.70
- - Split $100 USDC.e → 100 YES + 100 NO tokens
- Sell 100 NO tokens at ~$0.30 → recover ~$27 USDC.e
- Net cost: ~$73 for 100 YES tokens (entry: $0.73)
Polymarket Contracts (Polygon Mainnet)
- - USDC.e: INLINECODE32
- CTF (Conditional Tokens): INLINECODE33
- CTF Exchange: INLINECODE34
Dependencies
Install with uv (from skill directory):
CODEBLOCK11
Limitations
- - Trading requires wallet approval setup (one-time)
- CLOB sells may fail if liquidity is insufficient
CLOB Cloudflare Blocking
Polymarket's CLOB API uses Cloudflare protection that blocks POST requests from many IPs, including datacenter IPs and some residential ISPs. This affects the "sell unwanted tokens" step.
Solution: Residential proxy with retry logic
The recommended setup uses a rotating residential proxy (e.g., IPRoyal, BrightData). The CLOB client automatically retries with new IPs until one works:
CODEBLOCK12
With this setup, CLOB orders typically succeed within 5-10 retries as the proxy rotates through IPs until finding an unblocked one.
Alternative workarounds:
- 1. Use
--skip-sell — Keep both YES and NO tokens, sell manually on polymarket.com - No proxy — Split still works; only CLOB sell is affected
If CLOB fails after all retries, your split still succeeded. The output tells you how many tokens to sell manually.
Troubleshooting
"No wallet available"
Set the
POLYCLAW_PRIVATE_KEY environment variable:
CODEBLOCK13
"Insufficient USDC.e"
Check balance with
uv run python scripts/polyclaw.py wallet status. You need USDC.e (bridged USDC) on Polygon.
"CLOB order failed"
The CLOB sell may fail due to:
- - Insufficient liquidity at the sell price
- IP blocked by Cloudflare (try proxy)
Your split still succeeded - you have the tokens, just couldn't sell unwanted side.
"Approvals not set"
First trade requires contract approvals. Run:
CODEBLOCK14
License
MIT
PolyClaw
支持交易的 Polymarket 技能,适用于 OpenClaw。浏览市场、管理钱包、执行交易、追踪持仓。
功能特性
- - 市场浏览 - 搜索和浏览 Polymarket 预测市场
- 钱包管理 - 基于环境变量的钱包配置
- 交易 - 通过拆分 + CLOB 执行买入 YES/NO 头寸
- 持仓追踪 - 追踪入场价格、当前价格和盈亏
- 对冲发现 - 基于 LLM 的逻辑蕴含覆盖投资组合发现
快速开始
首先,安装依赖(从技能目录):
bash
cd {baseDir}
uv sync
首次设置(交易必需)
首次交易前,设置 Polymarket 合约授权(一次性操作,Gas 费约 0.01 POL):
bash
uv run python scripts/polyclaw.py wallet approve
此操作将向 Polygon 提交 6 笔授权交易。每个钱包只需执行一次。
浏览市场
bash
按交易量查看热门市场
uv run python scripts/polyclaw.py markets trending
搜索市场
uv run python scripts/polyclaw.py markets search election
市场详情(返回包含所有字段的完整 JSON)
uv run python scripts/polyclaw.py market
输出选项:
- - 默认输出为格式化表格(适合展示)
- 使用 --full 标志显示完整问题文本(不截断)
- 使用 --json 标志(通过 scripts/markets.py --json trending)获取结构化 JSON 输出
钱包管理
bash
查看钱包状态(地址、余额)
uv run python scripts/polyclaw.py wallet status
设置合约授权(一次性)
uv run python scripts/polyclaw.py wallet approve
钱包通过 POLYCLAWPRIVATEKEY 环境变量配置。
交易
bash
买入 50 美元 YES 头寸
uv run python scripts/polyclaw.py buy YES 50
买入 25 美元 NO 头寸
uv run python scripts/polyclaw.py buy NO 25
持仓
bash
列出所有持仓及盈亏
uv run python scripts/polyclaw.py positions
对冲发现
寻找覆盖投资组合——通过逆否逻辑相互对冲的市场头寸对。
bash
扫描热门市场寻找对冲机会
uv run python scripts/polyclaw.py hedge scan
扫描匹配查询的市场
uv run python scripts/polyclaw.py hedge scan --query election
分析特定市场的对冲关系
uv run python scripts/polyclaw.py hedge analyze id1> id2>
输出选项:
- - 默认输出为格式化表格,显示层级、覆盖率、成本、目标和覆盖项
- 使用 --json 标志获取结构化 JSON 输出
- 使用 --min-coverage 0.90 按最低覆盖率过滤(默认 0.85)
- 使用 --tier 1 按层级过滤(1=最佳,默认 2)
覆盖率层级:
- - 第 1 层(高): >=95% 覆盖率——接近套利机会
- 第 2 层(良好): 90-95%——强对冲
- 第 3 层(中等): 85-90%——不错但存在明显风险
- 第 4 层(低): <85%——投机性(默认过滤)
LLM 模型: 通过 OpenRouter 使用 nvidia/nemotron-nano-9b-v2:free。模型选择很重要——某些模型会找到虚假相关性,而其他模型(如 DeepSeek R1)存在输出格式问题。如有需要,可使用 --model 覆盖。
自动化工具
投资组合实时追踪
通过 Polymarket 数据 API 进行实时持仓追踪。按 (slug, outcome) 分组持仓,显示 YES/NO 方向、当前价值以及基于 portfolio.json 成本基础的盈亏。
bash
uv run python portfolio_live.py
- - 从 data-api.polymarket.com 获取实时链上持仓
- 显示结果方向(YES/NO)、当前价格和未实现盈亏
- 无需 web3 依赖——仅使用 REST API
自动赎回
自动检测并赎回已结算(已解决)的市场。通过 CTF 合约在链上检查 payoutDenominator。当市场解决时,通过 NegRiskAdapter 提交 redeemPositions 交易。
bash
手动运行
cd {baseDir} && source .env && .venv/bin/python3 autoredeemcheck.py
定时任务(每 15 分钟)
/15 * cd /path/to/polyclaw && source .env && .venv/bin/python3 autoredeemcheck.py >> /var/log/polyclaw-redeem.log 2>&1
- - 需要 web3(使用 .venv/bin/python3,而非系统 python)
- 在 redeemstate.json 中追踪已赎回持仓以避免重复
- 需要 CHAINSTACKNODE 和 POLYCLAWPRIVATEKEY 环境变量
纪律扫描器
自动止盈扫描器。卖出涨幅超过 20% 的持仓,并带有滑点保护。可通过 cfoparams.json(DISCIPLINETP_PCT 键)配置。
bash
手动运行
cd {baseDir} && export $(grep -v ^# .env | xargs) && .venv/bin/python3 discipline_scanner.py
定时任务(每 30 分钟)
/30 * cd /path/to/polyclaw && export $(grep -v ^# .env | xargs) && .venv/bin/python3 discipline_scanner.py >> /var/log/polyclaw-discipline.log 2>&1
- - 跳过价值低于 1 美元的持仓
- 止盈阈值默认为 20%(可配置)
增强 API(polyclaw_api.py)
用于外部集成(例如通过 SSH 的 Wells TG Bot)的 CLI 桥接。提供结构化 JSON 输出供程序化使用。
bash
python3 polyclaw_api.py portfolio # 所有未平仓持仓及实时价格(JSON)
python3 polyclaw_api.py summary # 投资组合文本摘要
python3 polyclaw_api.py balance # CLOB + 链上钱包余额
python3 polyclaw_api.py risk # 当前风险规则
python3 polyclawapi.py riskcheck # 交易前风险检查
python3 polyclaw_api.py swap auto # 将所有非 USDC.e 代币兑换为 USDC.e
python3 polyclaw_api.py swap status # 显示代币余额
- - portfolio 是获取实时持仓数据的推荐命令(读取数据 API,而非本地数据库)
- balance 需要 web3——使用 .venv/bin/python3
- summary 读取本地数据库,可能已过时——建议使用 portfolio 以确保准确性
安全性
对于 MVP 版本,私钥存储在环境变量中,以简化操作并兼容 Claude Code。
安全警告: 此钱包仅存放少量资金。定期将资金提取到安全钱包。
环境变量
| 变量 | 必需 | 描述 |
|---|
| CHAINSTACKNODE | 是(交易) | Polygon RPC URL |
| OPENROUTERAPI_KEY |
是(对冲) | 用于 LLM 对冲发现的 OpenRouter API 密钥 |
| POLYCLAWPRIVATEKEY | 是(交易) | EVM 私钥(十六进制,带或不带 0x 前缀) |
| HTTPS_PROXY | 推荐 | 用于 CLOB 的轮换住宅代理(例如 IPRoyal) |
| CLOBMAXRETRIES | 否 | 带 IP 轮换的最大 CLOB 重试次数(默认:5) |
安全警告: 此钱包仅存放少量资金。定期将资金提取到安全钱包。环境变量中的私钥便于自动化,但安全性低于加密存储。
交易流程
- 1. 拆分头寸 - 通过 CTF 合约将 USDC.e 拆分为 YES + NO 代币
- 卖出不需要的 - 通过 CLOB 订单簿卖出不需要的一方
- 结果 - 您持有想要的头寸,通过卖出不需要的一方收回部分成本
示例:以 0.70 美元买入 YES
- - 拆分 100 USDC.e → 100 YES + 100 NO 代币
- 以约 0.30 美元卖出 100 NO 代币 → 收回约 27 US