| name | polymarket-openclaw-trading-bot |
| description | Build and operate AI-powered trading bots for Polymarket prediction markets with CLOB arbitrage on BTC 5m/15m windows |
| triggers | ["how do I set up a Polymarket trading bot","create a bot to trade BTC 5 minute markets on Polymarket","configure OpenClaw AI decision engine for Polymarket","implement Polymarket CLOB arbitrage strategy","debug Polymarket trading bot order execution","set up wallet authentication for Polymarket bot","run a TypeScript prediction market trading bot","configure trade strategies for Polymarket short-duration markets"] |
Polymarket OpenClaw Trading Bot
Skill by ara.so — Hermes Skills collection.
A TypeScript/Node.js trading bot for Polymarket prediction markets using the CLOB (Central Limit Order Book) API. Specializes in short-duration BTC markets (5m/15m windows) with rule-based strategies, optional OpenClaw AI decision layer, strict risk management, and real-time market polling.
What This Bot Does
- Market Selection: Automatically constructs Polymarket market slugs from coin + period (e.g., BTC + 5m)
- Price Polling: Fetches real-time UP/DOWN outcome token prices from Gamma/CLOB API
- Strategy Execution: Runs configurable
trade_1 or trade_2 strategies with entry/exit rules
- OpenClaw AI Layer: Optional deterministic decision engine for signal experimentation
- Order Management: Submits market orders via CLOB v2 with retry logic and cooldowns
- Authentication: L1 wallet signing to derive API credentials, L2 authenticated trading client
- Risk Controls: Cooldowns, position limits, emergency swaps, balance validation
Installation
git clone https://github.com/Predictly-MCP-Labs/polymarket-openclaw-ai-btc-arbitrage-trading-bot.git
cd polymarket-openclaw-ai-btc-arbitrage-trading-bot
npm install
Prerequisites:
- Node.js ≥ 20.6
- Polygon wallet with private key
- USDC balance on Polygon for trading
- Polymarket account with funder/proxy address
Environment Configuration
Create .env file (never commit this):
POLYMARKET_PRIVATE_KEY=0x...
POLYMARKET_FUNDER_ADDRESS=0x...
POLYMARKET_SIGNATURE_TYPE=POLY_PROXY
Environment Variable Reference:
POLYMARKET_PRIVATE_KEY: Wallet private key for signing CLOB operations (required)
POLYMARKET_FUNDER_ADDRESS: Address holding trading collateral (required)
PROXY_WALLET_ADDRESS: Alternate alias for funder address
POLYMARKET_SIGNATURE_TYPE: Signature method for your account type
Strategy Configuration (trade.toml)
The bot uses TOML for strategy configuration:
strategy = "trade_1"
trade_usd = 10.0
max_retries = 3
entry_buy_cooldown_sec = 30
[market]
market_coin = "btc"
market_period = "5"
[trade_1]
buy_range_start_pct = 40
buy_range_end_pct = 60
exit_time_pct = 80
exit_price_min_pct = 55
emergency_swap_enabled = false
[trade_2]
entry_ratio_min = 1.05
entry_ratio_max = 1.25
exit_ratio_threshold = 0.95
exit_time_emergency_pct = 90
emergency_swap_enabled = true
[openclaw]
enabled = false
mode = "deterministic"
min_edge_bps = 50
max_spread_bps = 200
lookback_points = 12
Running the Bot
npm run dev
npm run build
npm start
Key Code Patterns
Initialize CLOB Client and Authenticate
import { ClobClient } from "@polymarket/clob-client";
import { Wallet } from "ethers";
const wallet = new Wallet(process.env.POLYMARKET_PRIVATE_KEY!);
const clobClient = new ClobClient(
"https://clob.polymarket.com",
137,
wallet,
{
funderAddress: process.env.POLYMARKET_FUNDER_ADDRESS,
signatureType: parseInt(process.env.POLYMARKET_SIGNATURE_TYPE || "1")
}
);
const apiCreds = await clobClient.deriveApiKey();
console.log("API Key:", apiCreds.apiKey);
Fetch Market Data
import { getMarket } from "./services/gamma";
const coin = config.market.market_coin;
const period = config.market.market_period;
const slug = `will-btc-close-higher-in-${period}-minutes`;
const market = await getMarket(slug);
console.log("Market ID:", market.id);
console.log("Condition ID:", market.condition_id);
console.log("Tokens:", market.tokens);
Poll Prices and Execute Strategy
import { Trade } from "./trade/trade";
const trade = new Trade(
clobClient,
config.trade_usd,
config.max_retries,
config.entry_buy_cooldown_sec
);
await trade.updatePrices(
market.tokens[0].token_id,
market.tokens[1].token_id
);
import { make_trading_decision } from "./trade/decision";
const decision = await make_trading_decision(
trade,
config.strategy,
config.trade_1,
config.trade_2,
market.end_date_iso
);
console.log("Decision:", decision);
Place Market Order
const buyResult = await trade.buy(
market.tokens[0].token_id,
"UP"
);
if (buyResult.success) {
console.log("Buy successful:", buyResult.orderId);
} else {
console.error("Buy failed:", buyResult.error);
}
const sellResult = await trade.sell("UP");
if (sellResult.success) {
console.log("Sell successful:", sellResult.orderId);
}
Strategy Decision Logic (trade_1)
import { TradeAction } from "./trade/decision";
function evaluateTrade1Strategy(
trade: Trade,
config: Trade1Config,
endTimeIso: string
): TradeAction {
const now = Date.now();
const endTime = new Date(endTimeIso).getTime();
const timeElapsedPct = ((now - (endTime - 5 * 60 * 1000)) / (5 * 60 * 1000)) * 100;
const upPrice = trade.upPrice;
const downPrice = trade.downPrice;
if (!trade.hasBought) {
const priceInRange =
upPrice >= config.buy_range_start_pct &&
upPrice <= config.buy_range_end_pct;
if (priceInRange) {
return { action: "BUY_UP", reason: "Price in entry range" };
}
return { action: "HOLD", reason: "Waiting for entry conditions" };
}
const shouldExitTime = timeElapsedPct >= config.exit_time_pct;
const shouldExitPrice = upPrice >= config.exit_price_min_pct;
if (shouldExitTime && shouldExitPrice) {
return { action: "CLOSE_POSITION", reason: "Time and price exit met" };
}
return { action: "HOLD", reason: "Holding position" };
}
OpenClaw Decision Integration
import { getOpenClawDecision } from "./trade/openclaw";
if (config.openclaw.enabled) {
const openclawDecision = await getOpenClawDecision(
trade,
{
upPrice: trade.upPrice,
downPrice: trade.downPrice,
timeToExpiry: (new Date(market.end_date_iso).getTime() - Date.now()) / 1000,
hasBought: trade.hasBought,
positionSide: trade.positionSide
},
config.openclaw
);
console.log("OpenClaw signal:", openclawDecision.action);
console.log("Reason:", openclawDecision.reason);
}
Error Handling with Retry Logic
import { retryOnTransient } from "./utils/retry";
async function placeOrderWithRetry(
clobClient: ClobClient,
tokenId: string,
side: "BUY" | "SELL",
size: number,
maxRetries: number
) {
return retryOnTransient(
async () => {
const order = await clobClient.createMarketOrder({
tokenID: tokenId,
side,
amount: size.toString()
});
const result = await clobClient.postOrder(order);
return result;
},
maxRetries,
(error) => {
const isTransient =
error.message.includes("ECONNRESET") ||
error.message.includes("429") ||
error.message.includes("5xx");
return isTransient;
}
);
}
Common Trading Workflows
Setup and Run for BTC 5-Minute Markets
- Configure
.env with wallet credentials
- Edit
trade.toml:
[market]
market_coin = "btc"
market_period = "5"
strategy = "trade_1"
trade_usd = 5.0
- Run:
npm run dev
- Monitor logs for entry/exit signals
Switch to 15-Minute Markets
Change only the period in trade.toml:
[market]
market_period = "15"
Enable OpenClaw AI Decision Layer
[openclaw]
enabled = true
mode = "deterministic"
min_edge_bps = 50
max_spread_bps = 200
lookback_points = 12
Run Multiple Strategies Simultaneously
Run separate processes with different configs:
cp trade.toml trade-5m.toml
CONFIG_PATH=trade-5m.toml npm run dev
cp trade.toml trade-15m.toml
CONFIG_PATH=trade-15m.toml npm run dev
Module Reference
| Module | Path | Purpose |
|---|
| Entry point | src/index.ts | Bootstrap, auth, market loop |
| CLOB service | src/services/clob.ts | Wallet, signer, client setup |
| Gamma API | src/services/gamma.ts | Market metadata by slug |
| Config | src/config/toml.ts | TOML parsing and validation |
| Env validation | src/config/validateEnv.ts | Zod schema for env vars |
| Slug builder | src/config/slug.ts | Coin + period → market slug |
| Decision engine | src/trade/decision.ts | Strategy routing (trade_1/trade_2) |
| Price polling | src/trade/prices.ts | Quote fetching and display |
| Trade execution | src/trade/trade.ts | Buy/sell, cooldowns, balances |
| OpenClaw | src/trade/openclaw.ts | AI decision layer |
| Retry logic | src/utils/retry.ts | Transient error handling |
| Error messages | src/utils/tradingErrorMessage.ts | Human-friendly errors |
Troubleshooting
"Invalid BytesLike" Error
Cause: Malformed POLYMARKET_PRIVATE_KEY
Fix: Ensure private key starts with 0x and is 64 hex characters:
POLYMARKET_PRIVATE_KEY=0xabcdef1234567890...
Orders Not Filling
Possible causes:
- Insufficient USDC balance on funder address
- Market moved before order posted
- Spread too wide (
max_spread_bps exceeded)
Debug:
const balance = await clobClient.getBalance();
console.log("USDC balance:", balance.usdc);
console.log("Current spread:", Math.abs(trade.upPrice - trade.downPrice));
Entry Cooldown Blocking Trades
Expected behavior: After a failed buy, bot waits entry_buy_cooldown_sec before retrying
Adjust: Lower cooldown in trade.toml:
entry_buy_cooldown_sec = 10
OpenClaw Not Triggering
Check:
[openclaw].enabled = true in trade.toml
- Mode is
"deterministic" or "http" with valid endpoint
- Logs show "OpenClaw signal:" messages
Debug:
console.log("OpenClaw config:", config.openclaw);
console.log("Edge BPS:", calculatedEdge);
API Authentication Failures
Symptoms: "401 Unauthorized" or "Invalid signature"
Fix:
- Verify
POLYMARKET_SIGNATURE_TYPE matches your account:
EOA for standard wallets
POLY_PROXY for Polymarket proxy accounts
- Regenerate API credentials:
const newCreds = await clobClient.createApiKey();
Market Not Found
Error: Market slug not found
Cause: Coin/period combination doesn't match active Polymarket markets
Fix: Verify active markets at polymarket.com and adjust:
[market]
market_coin = "btc"
market_period = "5"
Advanced Patterns
Custom Decision Logic
Extend decision engine with custom strategy:
export function customDecision(
trade: Trade,
customConfig: any
): TradeAction {
const momentum = calculateMomentum(trade.priceHistory);
if (momentum > customConfig.momentum_threshold) {
return { action: "BUY_UP", reason: "Strong upward momentum" };
}
return { action: "HOLD", reason: "Waiting for momentum" };
}
import { customDecision } from "./customStrategy";
if (config.strategy === "custom") {
return customDecision(trade, config.custom);
}
Monitor Multiple Markets
const markets = [
{ coin: "btc", period: "5" },
{ coin: "eth", period: "5" },
{ coin: "btc", period: "15" }
];
for (const market of markets) {
const slug = buildSlug(market.coin, market.period);
const marketData = await getMarket(slug);
runMarketLoop(marketData, config);
}
Backtesting Strategy
import { Trade } from "./trade/trade";
async function backtestStrategy(
historicalData: PricePoint[],
config: TomlConfig
) {
const results = [];
for (const dataPoint of historicalData) {
const trade = new Trade(mockClient, config.trade_usd, 1, 0);
trade.upPrice = dataPoint.upPrice;
trade.downPrice = dataPoint.downPrice;
const decision = await make_trading_decision(
trade,
config.strategy,
config.trade_1,
config.trade_2,
dataPoint.endTime
);
results.push({ time: dataPoint.time, decision });
}
return results;
}
Safety and Compliance
- Start small: Use low
trade_usd values initially
- Paper trading: Test strategies without real orders first
- Risk limits: Respect
max_retries and cooldowns
- Market risk: 5m/15m windows move fast; fills may not match signals
- Regulatory: Ensure compliance with local prediction market regulations
- API limits: CLOB has rate limits; bot includes retry backoff
Key Search Terms
Polymarket trading bot, Polymarket CLOB API, Polymarket arbitrage bot, BTC 5 minute Polymarket, BTC 15 minute Polymarket, TypeScript prediction market bot, Polymarket API credentials, OpenClaw AI trading, Polymarket wallet setup, CLOB order execution