How to build a Polymarket trading bot.
Polymarket is a prediction-market exchange with deep liquidity, low fees, and a public CLOB API — which makes it one of the cleanest sandboxes a retail trader can use to forward-test an automated strategy. This guide walks through building a real bot end-to-end, then proving it works.
What you'll build
A Python bot that pulls live order-book data from Polymarket's CLOB, applies a mean-reversion edge with risk caps, places limit orders via signed API requests, and logs every fill so you can forward-test it against the ScriptSamurai leaderboard.
1. What you need before you write a line
Most bots fail before they run because the setup is sloppy. Get these right first:
- — A funded Polymarket account and a Polygon wallet you control the private key for.
- — Python 3.11+, with
py-clob-client,web3, andpandasinstalled. - — API credentials generated through the CLOB client (key, secret, passphrase).
- — A clear, written hypothesis. "Buy low, sell high" is not a hypothesis.
pip install py-clob-client web3 pandas python-dotenv2. Connect to the CLOB
Polymarket's CLOB (Central Limit Order Book) is where orders actually rest. Authenticate once, cache the credentials, and reuse them.
from py_clob_client.client import ClobClient
from py_clob_client.constants import POLYGON
import os
HOST = "https://clob.polymarket.com"
KEY = os.environ["PK"] # wallet private key
FUNDER = os.environ["FUNDER_ADDRESS"] # proxy wallet (USDC)
client = ClobClient(HOST, key=KEY, chain_id=POLYGON,
signature_type=2, funder=FUNDER)
client.set_api_creds(client.create_or_derive_api_creds())3. Define an edge worth testing
Prediction markets misprice in predictable ways: late liquidity flows, headline overreactions, and thin order books near 0¢/100¢. Pick one and write it down as a rule. Example — fade short-term spikes greater than 4¢ in markets with at least $50k of open interest.
def signal(book, history):
mid = (book["best_bid"] + book["best_ask"]) / 2
drift = mid - history["mid_5m"].mean()
if drift > 0.04: # spiked up — fade it
return ("SELL", mid - 0.005)
if drift < -0.04: # spiked down — fade it
return ("BUY", mid + 0.005)
return None4. Place orders with discipline
Use limit orders. Cap position size as a percentage of bankroll, not in dollars. Always set a max-loss kill switch — the most expensive bug is one that keeps trading after the edge has died.
from py_clob_client.clob_types import OrderArgs, OrderType
def place(client, token_id, side, price, size):
order = client.create_order(OrderArgs(
token_id=token_id, price=price, size=size, side=side,
))
return client.post_order(order, OrderType.GTC)
MAX_POSITION_PCT = 0.02 # 2% of bankroll per market
DAILY_LOSS_KILL = 0.05 # 5% drawdown → stop trading5. Forward-test before you trust it
A backtest is a story you tell yourself. A forward test is evidence. Run the bot live with small size for at least 30 days, log every fill with timestamps and book snapshots, and compute Sharpe, win rate, and max drawdown on the live results — not the historical sim.
This is what ScriptSamurai's leaderboard ranks. Same conditions for every builder, no cherry-picked windows.
6. Common mistakes
- Overfitting to one market. An edge that only works on one election market isn't an edge.
- Ignoring resolution risk. A market that resolves YES wipes any short position. Size accordingly.
- Trusting the backtest. Slippage, latency, and partial fills all show up live and nowhere else.
- No kill switch. Always have a hard daily loss limit that exits all positions.
7. Frequently asked questions
- Is running a Polymarket trading bot legal?
- Polymarket is available in most jurisdictions but restricted in some (including the US). You are responsible for confirming access is legal where you live and that automated trading complies with Polymarket's terms of service.
- How much capital do I need to start?
- You can technically start with tens of dollars, but forward-testing an edge needs enough sample size that fees and slippage don't dominate. Most builders start between $200 and $1,000 of USDC on Polygon.
- Do I need to know machine learning to build a profitable bot?
- No. Most durable edges on prediction markets come from clean execution and disciplined risk — not model complexity. A simple mean-reversion or liquidity-provision rule that survives forward-testing beats an over-fit ML model every time.
- How long should I forward-test before trusting the bot?
- At least 30 days of live trading with small size. That's the minimum window the ScriptSamurai leaderboard uses to rank strategies, because it captures enough varied market conditions to expose the ones that only worked in the backtest.
- What's the biggest risk when running a Polymarket bot?
- Resolution risk. A market that resolves against a short position wipes out the full notional, not just a percentage. Always size positions based on worst-case resolution, not average P&L.
Built one? Prove it in the dojo.
Submit your Polymarket bot to the forward-test arena. Same rules for everyone. The leaderboard does the talking.