Polymarket Trading - Self-Evolving Trading Bot
Design a trading strategy with the user, run automated paper trading (dry-run), auto-improve parameters when underperforming, and ask permission to switch to live trading when edge target met.
Supports any market type: politics, sports, crypto prices, science, entertainment, etc.
Critical Rules
- 1. NEVER default to any specific market — always ask user what they want to trade
- NEVER show made-up prices or signals — only real script output
- DRY RUN always on by default — only switch to live after explicit user confirmation
- Auto-improve silently — adjust params and notify user
- NEVER create a strategy or set up crons without explicit user confirmation
Multi-User Support
Each user has their own credentials/{USER_ID}.json:
CODEBLOCK0
Set USER_ID or TELEGRAM_USER_ID env var when calling scripts.
Or set POLYMARKET_PRIVATE_KEY env var.
First-Time User Flow
1. Check credentials
CODEBLOCK1
If not configured, run interactive setup:
CODEBLOCK2
2. Ask what they want to trade
Do NOT assume. Ask:
- - What market type? (politics, sports, crypto, tech, etc.)
- Any specific keywords to focus on?
3. Propose strategy — WAIT FOR CONFIRMATION
Based on the conversation, derive and propose concrete values. Stop and wait for user to confirm before executing anything.
4. Create strategy (only after confirmation)
CODEBLOCK3
5. Set up crons
CODEBLOCK4
Strategy Lifecycle
CODEBLOCK5
Go-live condition: edge = win_rate - avg_entry_price >= min_edge AND INLINECODE5
Strategy Management
CODEBLOCK6
Auto-Improvement Rules
| Condition | Action |
|---|
| edge < minedge - 10% | Raise threshold (fewer but stronger signals) |
| edge < minedge - 3% |
Lower max
entryprice (cheaper entries) |
| 3+ adjustments still failing | Notify user to reconsider |
| edge >= min
edge AND pnl > 0 | Mark pendinglive, notify user |
Signal Methods
| Method | Best for |
|---|
| orderbook_imbalance | Any liquid market with active orderbook |
New methods can be added to scripts/signals.py.
Troubleshooting
| Error | Solution |
|---|
| py-clob-client not installed | INLINECODE7 |
| USER_ID not set |
Add
USER_ID=xxx prefix |
| No config found | Run
python3 {baseDir}/scripts/polymarket.py setup |
| API Error 401 | Re-run setup to re-derive keys |
| No markets found | Broaden keywords or lower min
liquidityusdc |
File Structure
CODEBLOCK7
Polymarket 交易 - 自进化交易机器人
与用户共同设计交易策略,运行自动模拟交易(试运行),在表现不佳时自动优化参数,并在达到优势目标时请求切换到实盘交易。
支持任何市场类型:政治、体育、加密货币价格、科学、娱乐等。
关键规则
- 1. 绝不默认任何特定市场——始终询问用户想要交易什么
- 绝不展示虚构的价格或信号——仅显示真实的脚本输出
- 默认始终开启试运行——仅在用户明确确认后切换到实盘
- 静默自动优化——调整参数并通知用户
- 未经用户明确确认,绝不创建策略或设置定时任务
多用户支持
每个用户拥有自己的 credentials/{USER_ID}.json:
json
{
private_key: 0x...,
funder_address: 0x...,
dry_run: true
}
调用脚本时设置 USERID 或 TELEGRAMUSER_ID 环境变量。
或设置 POLYMARKETPRIVATEKEY 环境变量。
首次用户流程
1. 检查凭证
bash
USERID=$TELEGRAMUSER_ID python3 {baseDir}/scripts/polymarket.py check
如果未配置,运行交互式设置:
bash
USERID=$TELEGRAMUSER_ID python3 {baseDir}/scripts/polymarket.py setup
2. 询问他们想要交易什么
不要假设。询问:
- - 什么市场类型?(政治、体育、加密货币、科技等)
- 是否有任何需要关注的具体关键词?
3. 提出策略——等待确认
基于对话,推导并提出具体数值。在执行任何操作前停止并等待用户确认。
4. 创建策略(仅在确认后)
bash
USERID=$TELEGRAMUSERID python3 {baseDir}/scripts/strategymanager.py create --json {
name: <策略名称>,
market_filter: {
keywords: [<关键词>],
minliquidityusdc: 1000,
maxdaysto_expiry: 30,
mindaysto_expiry: 1
},
signal: {
method: orderbook_imbalance,
params: { threshold: 0.15, maxentryprice: 0.60 }
},
sizing: { maxsizeusdc: 5 },
targets: { minsamplesize: 30, min_edge: 0.05 }
}
5. 设置定时任务
bash
SKILL_PATH={baseDir}
STRATEGY_ID=<步骤4中的ID>
信号扫描:每15分钟
(crontab -l 2>/dev/null; echo
/15 * USER
ID=$TELEGRAMUSER
ID python3 $SKILLPATH/scripts/signal
cron.py $STRATEGYID >> $SKILL
PATH/state/$TELEGRAMUSER
ID.$STRATEGYID.log 2>&1) | crontab -
绩效审查:每天UTC时间09:00
(crontab -l 2>/dev/null; echo 0 9
* USER
ID=$TELEGRAMUSER
ID python3 $SKILLPATH/scripts/review
cron.py $STRATEGYID >> $SKILL
PATH/state/$TELEGRAMUSER_ID.review.log 2>&1) | crontab -
策略生命周期
试运行 → 优化中 → 待实盘 → 实盘
上线条件:优势 = 胜率 - 平均入场价格 >= 最小优势 且 总盈亏 > 0
策略管理
bash
USERID=$TELEGRAMUSERID python3 {baseDir}/scripts/strategymanager.py status
USERID=$TELEGRAMUSERID python3 {baseDir}/scripts/strategymanager.py review <策略ID>
USERID=$TELEGRAMUSERID python3 {baseDir}/scripts/strategymanager.py activate-live <策略ID>
自动优化规则
| 条件 | 操作 |
|---|
| 优势 < 最小优势 - 10% | 提高阈值(更少但更强的信号) |
| 优势 < 最小优势 - 3% |
降低最大入场价格(更便宜的入场点) |
| 3次以上调整仍失败 | 通知用户重新考虑 |
| 优势 >= 最小优势 且 盈亏 > 0 | 标记为待实盘,通知用户 |
信号方法
| 方法 | 最适合场景 |
|---|
| orderbook_imbalance | 任何具有活跃订单簿的流动性市场 |
新方法可添加到 scripts/signals.py。
故障排除
| 错误 | 解决方案 |
|---|
| py-clob-client 未安装 | pip3 install py-clob-client |
| USERID 未设置 |
添加 USERID=xxx 前缀 |
| 未找到配置 | 运行 python3 {baseDir}/scripts/polymarket.py setup |
| API 错误 401 | 重新运行设置以重新派生密钥 |
| 未找到市场 | 扩大关键词范围或降低 min
liquidityusdc |
文件结构
polymarket-trading/
├── SKILL.md
├── scripts/
│ ├── strategy_manager.py # 生命周期:创建/审查/状态/激活实盘
│ ├── signal_cron.py # 定时任务:扫描市场、运行信号、记录交易
│ ├── review_cron.py # 定时任务:每日审查 + 自动优化
│ ├── signals.py # 可插拔的信号方法
│ ├── market_scanner.py # Gamma API 市场发现
│ └── polymarket.py # CLOB API 基础功能
├── credentials/
│ └── {USER_ID}.json
├── strategies/
│ └── {USER_ID}/{策略ID}.json
└── state/
└── {USER_ID}.{策略ID}.perf.json