Repository: github.com/DEV-OCR/Polymarket-trading-bot
Description: polymarket copy trading bot, polymarket trading bot, polymarket copy trading bot, polymarket trading bot, polymarket copy trading bot, polymarket trading bot, polymarket copy trading bot, polymarket trading bot, polymarket copy trading bot, polymarket trading bot, polymarket copy trading bot, polymarket trading bot, polymarket copy trading bot, polymarket trading bot, polymarket copy trading bot, polymarket trading bot, polymarket copy trading bot, polymarket trading bot
A production-oriented Node.js trading bot for Polymarket short-duration Up/Down markets on Polygon, with both arbitrage and copy-trading workflows.
This project is built for users who need an operational trading runtime instead of a toy script:
- Real CLOB authentication and order execution on Polymarket
- On-chain approvals, merge, and redeem transaction flows
- Real-time order-book monitoring via WebSocket with fallback logic
- Configurable risk limits, circuit breakers, and graceful shutdown behavior
- Dedicated copy-trading path for mirroring selected wallet activity
This repository contains:
- A 5-minute BTC Up/Down arbitrage bot that posts both sides of the market, takes mispriced liquidity, merges matched pairs, and redeems after resolution.
- An integrated wallet-following module that can mirror future buys from a target wallet during the main runtime.
- A dedicated buy-only copy trader under
src/copy/that polls Polymarket's public trade feed and reacts to fresh target-wallet buys.
The codebase is structured for real trading: on-chain approvals, Polymarket CLOB authentication, WebSocket order-book tracking, risk caps, circuit breakers, structured logging, and graceful shutdown handling are all included.
Important: This software places live blockchain and market orders. Use it only if you fully understand the strategy, the wallet setup, the gas implications, and the market risks.
This bot is designed around Polymarket complementary-token markets, especially the recurring btc-updown-5m-* markets.
At a high level, the main arbitrage engine:
- Loads your signer and Polymarket proxy wallet configuration.
- Ensures the required Polygon approvals are in place.
- Derives or refreshes Polymarket CLOB API credentials automatically.
- Discovers the next 5-minute BTC Up/Down market before it opens.
- Posts a symmetric buy ladder on both
UpandDown. - Aggressively buys both sides when the combined ask becomes favorable.
- Merges matched pairs back into USDC when economically sensible.
- Cancels open orders at market close.
- Waits for resolution and redeems winning positions.
The repository also supports copy-trading workflows for users who want to follow a target Polymarket wallet with configurable sizing and risk caps.
The following screenshots show the workflow this bot targets.
- Install dependencies:
npm install- Create your local environment file:
Copy-Item .env.example .envor:
cp .env.example .env- Configure at minimum:
PRIVATE_KEYPROXY_WALLETPOLYGON_RPC(strongly recommended)
- Start with conservative limits and run:
npm start- Dual-sided ladder execution for recurring BTC 5-minute markets.
- Taker arbitrage logic when combined best ask drops below the configured edge threshold.
- On-chain merge and redeem support for Polymarket negative-risk markets.
- Session and per-market risk controls including spend caps and circuit breakers.
- Automatic CLOB credential derivation through Polymarket's documented L1-to-L2 auth flow.
- Live WebSocket order-book tracking for low-latency price updates.
- Optional wallet mirroring for follow-trading workflows.
- Dedicated copy-trading engine with filters for stale trades, price bounds, slippage, and cumulative spend.
- Structured console and file logging through
winston. - Graceful shutdown on
SIGINT/SIGTERM.
There are two primary ways to use this repository.
The main runtime lives in src/index.js.
It is focused on:
- Discovering the next
btc-updown-5mmarket - Posting both-sided ladders at the open
- Executing taker arb when the market offers enough edge
- Merging matched pairs back into USDC
- Redeeming after oracle resolution
This is the core strategy described by the main package.json metadata.
The repo currently contains two copy-related paths:
-
Integrated copy watcher in
src/copy-trader.js
IfTARGET_WALLETis set, the main runtime snapshots that wallet's current positions and then mirrors future increases intotalBought. -
Dedicated buy-only copy trader in
src/copy/
This path usessrc/copy/activityFeed.jsplussrc/copy/copyTrader.jsto detect fresh public trade activity from one or more target wallets and submit capped buy orders quickly.
If your goal is to use the dedicated copy system, read the copy-trading section below carefully and prefer the explicit src/copy/index.js entrypoint.
The core Trader class in src/trader.js implements a market lifecycle roughly like this:
-
Wait for market open
The bot discovers the next market before open, then waits until the trading window is live. -
Post the ladder
It places buy orders across many configured price levels on both outcomes. -
Monitor live best asks
A WebSocket feed keeps the current top-of-book available with REST fallback when needed. -
Fire taker arb orders
WhenbestAskUp + bestAskDown < 1 - TARGET_EDGE, the bot submits paired buy orders. -
Merge matched pairs
If both outcomes have been accumulated in sufficient size, the bot merges them back into USDC. -
Stop buying near market close
It stops adding risk before the window ends, then cancels remaining open orders. -
Wait for resolution and redeem
After the market resolves, the bot redeems any winning positions.
The main strategy includes multiple safety mechanisms:
MAX_SPEND_PER_MARKETMAX_TAKER_FILL_USDCMAX_INVENTORY_IMBALANCE_USDCCOMBINED_ASK_STOPMAX_LOSS_PER_HOUR_USDCSTOP_BUYING_BEFORE_CLOSEMERGE_THRESHOLD_USDC
These controls are not optional decoration. They define how aggressively the bot is allowed to trade and when it must stop.
Before running the bot, make sure you have:
- Node.js 18+
- A Polymarket account
- Your Polygon EOA private key
- Your Polymarket proxy wallet address
- USDC on Polygon
- Enough native MATIC for gas
- Access to a reliable Polygon RPC endpoint
Recommended:
- Use a dedicated wallet for bot operation
- Start with small size caps
- Use a private RPC provider instead of the default public endpoint
- Test copy workflows with
COPY_DRY_RUN=truefirst
npm installThe repo already includes a package-lock.json, so a normal npm install is the intended setup path.
Copy the example environment file.
PowerShell:
Copy-Item .env.example .envBash:
cp .env.example .envThen edit .env with your real wallet and risk settings.
These values are essential for almost every real run:
| Variable | Required | Purpose |
|---|---|---|
PRIVATE_KEY |
Yes | Your Polygon signer used for approvals and order signing |
PROXY_WALLET |
Yes | The Polymarket proxy wallet tied to your account |
POLYGON_RPC |
Strongly recommended | Polygon RPC endpoint used by ethers |
LOG_LEVEL |
No | Console/file logging verbosity |
These parameters shape the main strategy:
| Variable | What it controls |
|---|---|
MAX_SPEND_PER_MARKET |
Total buy-side budget for one 5-minute market |
TARGET_EDGE |
Minimum combined-price edge before firing arb buys |
MERGE_THRESHOLD_USDC |
Minimum matched pair size before merge |
MAX_TAKER_FILL_USDC |
Maximum size for a single taker action |
MAX_INVENTORY_IMBALANCE_USDC |
Inventory imbalance guardrail |
COMBINED_ASK_STOP |
Hard stop if the market becomes too expensive |
MAX_LOSS_PER_HOUR_USDC |
Rolling session loss circuit breaker |
LADDER_LEVELS |
Ladder prices posted on both sides |
LADDER_SIZE_PER_LEVEL_USDC |
Size allocated to each ladder level |
The repo exposes two different copy-trading parameter groups.
| Variable | Purpose |
|---|---|
TARGET_WALLET |
Single target Polymarket wallet to mirror during main runtime |
COPY_TRADE_BUY_PERCENT |
Percent of target spend to mirror |
COPY_TRADE_POLL_MS |
Poll interval for the target positions API |
| Variable | Purpose |
|---|---|
COPY_TARGETS |
Comma-separated list of target wallets |
COPY_SIZE_MODE |
FIXED, MIRROR, or RATIO |
COPY_FIXED_USDC |
Fixed spend per copied trade |
COPY_RATIO |
Spend multiplier when COPY_SIZE_MODE=RATIO |
COPY_MAX_USDC_PER_TRADE |
Hard cap per copied trade |
COPY_MAX_USDC_PER_MARKET |
Hard cap per market |
COPY_MAX_USDC_PER_HOUR |
Rolling hourly cap |
COPY_MAX_USDC_TOTAL |
Session-wide cap |
COPY_MAX_SLIPPAGE |
Maximum price premium above target fill |
COPY_MAX_PRICE |
Upper price filter |
COPY_MIN_PRICE |
Lower price filter |
COPY_STALE_MS |
Reject stale signals |
COPY_POLL_MS |
Poll frequency for target trades |
COPY_ALLOWED_CONDITIONS |
Optional allow-list |
COPY_BLOCKED_CONDITIONS |
Optional block-list |
COPY_DRY_RUN |
Log-only mode without real orders |
The example file includes:
POLY_API_KEYPOLY_API_SECRETPOLY_API_PASSPHRASE
These can be left blank on first run. The client in src/clob.js attempts to derive valid credentials automatically from your signer. If cached credentials become invalid, the client also attempts a refresh flow.
- Install dependencies with
npm install. - Copy
.env.exampleto.env. - Fill in
PRIVATE_KEY,PROXY_WALLET, and a reliablePOLYGON_RPC. - Start with conservative values such as a smaller
MAX_SPEND_PER_MARKET. - Run the bot.
npm startYou can also run the explicit arb script:
npm run arbFor development mode with Node's file watcher:
npm run devor
npm run arb:devIf you want the main runtime to also mirror future buys from a specific target wallet:
- Set
TARGET_WALLETin.env. - Optionally tune
COPY_TRADE_BUY_PERCENTandCOPY_TRADE_POLL_MS. - Launch the main bot as normal.
The bot will snapshot existing positions first, then only react to new buy increases observed after startup.
If you want the dedicated src/copy/ implementation:
- Configure
COPY_TARGETS. - Set conservative caps.
- Prefer starting with
COPY_DRY_RUN=true. - Run the dedicated entrypoint directly.
node src/copy/index.jsThis path is useful when your focus is low-latency buy-following rather than the BTC 5-minute arbitrage lifecycle.
The logger writes:
- Colored structured logs to the console
- JSON logs to
bot.log
This is useful for:
- Live monitoring
- Post-session debugging
- Reviewing order failures
- Investigating market timing and slippage
.
|-- .env.example
|-- package.json
|-- img/
|-- src/
| |-- index.js
| |-- trader.js
| |-- copy-trader.js
| |-- market.js
| |-- clob.js
| |-- onchain.js
| |-- pnl.js
| |-- logger.js
| `-- copy/
| |-- index.js
| |-- activityFeed.js
| |-- copyTrader.js
| `-- config.js
`-- README.md
-
src/index.js
Main bot lifecycle, startup, market loop, graceful shutdown, and optional integrated copy watcher. -
src/trader.js
Per-market state machine for ladder posting, taker arb, merge logic, order cancellation, and redeem flow. -
src/market.js
Polymarket Gamma/Data API helpers for market discovery, wallet positions, and resolution polling. -
src/clob.js
CLOB REST and WebSocket client, auth handling, order signing, and order posting utilities. -
src/onchain.js
Polygon approvals, token balances, merge transactions, and redeem transactions. -
src/copy-trader.js
Integrated target-wallet mirroring based on changes in target positions. -
src/copy/activityFeed.js
High-frequency polling of public trade data for specific wallets. -
src/copy/copyTrader.js
Dedicated copy-trading execution engine with filters and spend accounting.
- Do not start with large sizing.
- Do not run with a wallet you use for unrelated funds.
- Do not assume public RPC endpoints are stable enough for serious production usage.
- Do not skip reading the
.env.examplecomments; they explain the intended meaning of most controls. - Do start with
COPY_DRY_RUN=truefor copy trading. - Do verify your proxy wallet address carefully.
- Do keep enough MATIC available for approvals, merges, and redeems.
Check .env and confirm the required fields are actually populated:
PRIVATE_KEYPROXY_WALLET
For dedicated copy trading, also confirm:
COPY_TARGETS
- Confirm your signer matches the intended Polymarket account.
- Confirm
PROXY_WALLETis the correct proxy wallet for that signer. - Try a stable private Polygon RPC endpoint.
- Leave
POLY_API_*blank if you want the bot to derive fresh credentials automatically.
- Make sure your target wallet address is lowercased correctly in the copy settings.
- Check that the target is placing fresh BUY trades, not only sells or old fills.
- Verify your caps are not too restrictive.
- For dedicated copy mode, make sure
COPY_DRY_RUNis set the way you expect.
- Confirm approvals were granted successfully.
- Confirm you hold enough matched positions to merge.
- Confirm the market is actually resolved before expecting redeem payout.
- Check
bot.logfor the underlying error payload.
If you are launching this repo for the first time, the safest path is:
- Fill
.envwith real wallet values. - Use a private RPC endpoint.
- Lower your spend caps materially.
- Run the main bot in observation mode with small sizing.
- If testing copy logic, enable
COPY_DRY_RUN=truefirst. - Review
bot.logafter a short session before scaling up.
This repository is provided for research and operational use by experienced users. It is not financial advice, and it does not guarantee profit. Real-money trading on Polymarket and Polygon involves market risk, execution risk, smart contract risk, RPC reliability risk, and operational risk. You are fully responsible for how you configure and use this software.



