A sophisticated automated trading bot for Polymarket featuring multiple trading strategies on the Polygon network:
- Spike Strategy - Detects price spikes and executes trades with automatic position management
- YES/NO Arbitrage - Market-neutral box strategy that buys YES+NO when sum < $1.00
# Install dependencies
pip install -r requirements.txt
# Run spike strategy (default)
python main.py --strategy spike
# Run YES/NO arbitrage in dry-run mode
python main.py --strategy yesno_arb --dry-run
# Run YES/NO arbitrage live (after testing!)
DRY_RUN=false python main.py --strategy yesno_arbThe bot implements the following trading strategy:
-
Price Spike Detection
- Monitors price movements across market pairs
- Detects significant price spikes above/below threshold
- Executes trades when spike conditions are met
-
Position Management
- Automatic take-profit and stop-loss execution
- Position size management based on account balance
- Minimum liquidity requirements for trade execution
- Maximum concurrent trades limit
-
Risk Management
- Slippage protection
- Maximum holding time limits
- Minimum liquidity requirements
- Concurrent trade limits
- USDC balance checks
- Multi-pair trading support
- Real-time price monitoring
- Automatic spike detection
- Configurable take-profit and stop-loss levels
- USDC balance management
- Automatic API credential refresh
- Comprehensive logging system
- Thread-safe state management
- Error handling and recovery
- Graceful shutdown handling
- Python 3.7+
- MetaMask wallet with Polygon network
- USDC balance on Polygon network
- Polymarket account
- Creat venv:
python -m venv .venv
.venv/Scripts/activate- Install required Python packages:
pip install web3==6.11.1
pip install python-dotenv==1.0.0
pip install requests==2.31.0
pip install py-clob-client==0.1.0
pip install halo==0.0.31
pip install colorlog==6.7.0
pip install dotenv- Create a
.envfile with your configuration:
# Wallet Configuration
PK=your_private_key_here
YOUR_PROXY_WALLET=your_proxy_wallet_address
BOT_TRADER_ADDRESS=your_trader_address
USDC_CONTRACT_ADDRESS=0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174
POLYMARKET_SETTLEMENT_CONTRACT=0x56C79347e95530c01A2FC76E732f9566dA16E113
# Trading Parameters
trade_unit=3.0
slippage_tolerance=0.02
pct_profit=0.03
pct_loss=-0.025
cash_profit=1.0
cash_loss=-0.75
spike_threshold=0.02
sold_position_time=120
holding_time_limit=3600
price_history_size=100
cooldown_period=10
keep_min_shares=1
max_concurrent_trades=3
min_liquidity_requirement=10.0PK: Your wallet's private keyYOUR_PROXY_WALLET: Your Polymarket proxy wallet addressBOT_TRADER_ADDRESS: Your MetaMask wallet addressUSDC_CONTRACT_ADDRESS: USDC contract address on PolygonPOLYMARKET_SETTLEMENT_CONTRACT: Polymarket settlement contract address
trade_unit: Base trade size in USDCslippage_tolerance: Maximum allowed slippage (e.g., 0.02 for 2%)pct_profit: Take profit threshold (e.g., 0.03 for 3%)pct_loss: Stop loss threshold (e.g., -0.025 for -2.5%)cash_profit: Take profit in USDCcash_loss: Stop loss in USDCspike_threshold: Minimum price movement to trigger tradesold_position_time: Cooldown period between trades (seconds)holding_time_limit: Maximum time to hold a position (seconds)price_history_size: Number of price points to trackcooldown_period: Retry cooldown for failed orders (seconds)keep_min_shares: Minimum shares to keep when sellingmax_concurrent_trades: Maximum number of concurrent tradesmin_liquidity_requirement: Minimum liquidity required to trade (USDC)
- Ensure your
.envfile is properly configured - Run the bot:
python test.pyThe bot maintains detailed logs in the logs directory:
polymarket_bot.log: Main log file with all bot activities- Logs include price updates, trade executions, and error messages
- Color-coded console output for easy monitoring
- Automatic retry mechanism for failed orders
- USDC balance checks before trades
- Comprehensive error handling and logging
- Transaction receipt verification
- Thread-safe state management
- Graceful shutdown handling
- Ensure sufficient USDC balance in your wallet
- Monitor the bot's logs regularly
- The bot maintains minimum shares when selling
- Trades are executed with configurable unit size
- API credentials are refreshed hourly
- Price updates occur every second
- Position checks occur every second
The YES/NO arbitrage strategy attempts to capture spread when the combined price of YES and NO shares falls below $1.00. By purchasing both outcomes simultaneously, you lock in a guaranteed profit at settlement (minus execution costs).
How it works:
Example: Bitcoin price market
- YES ask: $0.48 (betting price goes up)
- NO ask: $0.50 (betting price stays/goes down)
- Sum: $0.98
At settlement, ONE of YES or NO will be worth $1.00.
Since you own both, you receive $1.00 regardless of outcome.
Profit = $1.00 - $0.98 = $0.02 per share (before fees)
The YES/NO arbitrage strategy uses a normal EOA (Externally Owned Account) for trading. The same address serves as both the trading identity and the balance holder.
-
Generate or use an existing EOA private key
-
Add to your
.envfile:PK=your_64_character_hex_private_key EOA_ADDRESS=0xYourDerivedAddress # Optional, derived from PK if not set -
Fund the EOA:
- Transfer USDC to your EOA address on Polygon
- Ensure you have MATIC for gas fees
-
Security best practices:
- Never share or commit your private key
- Use a dedicated trading wallet with limited funds
- The key is only read from
.envat startup - No logging of private key material
When placing a box trade, BOTH legs must fill for the arbitrage to be complete. The hedge manager handles partial fill scenarios:
Perfect execution - profit is locked at settlement.
Unwind Mode (default, HEDGE_MODE=unwind):
- Cancel the unfilled order
- Sell back the filled position at market
- Accept small loss to exit the incomplete position
Complete Mode (HEDGE_MODE=complete):
- Cancel the unfilled order
- Place a more aggressive order for the missing leg (up to
MAX_HEDGE_SLIPPAGE) - Falls back to unwind if aggressive order fails
| Parameter | Default | Description |
|---|---|---|
MAX_SUM_PRICE |
0.99 | Trigger when YES + NO ask ≤ this |
MIN_EDGE |
0.005 | Minimum edge after fees (0.5%) |
TRADE_NOTIONAL_USDC |
10 | USDC per trade |
LEG_SLIPPAGE_BUFFER |
0.002 | Buffer on limit prices (0.2%) |
MIN_LIQUIDITY |
25 | Min $ at best ask |
HEDGE_TIMEOUT_MS |
750 | Wait for fill confirmation |
MAX_HEDGE_SLIPPAGE |
0.01 | Max slippage for hedge (1%) |
TARGET_TAGS |
Crypto,Bitcoin,... | Market filter keywords |
MAX_TIME_TO_CLOSE_MIN |
20 | Only markets closing soon |
DRY_RUN |
true | Log orders without executing |
# Dry run mode (recommended first)
python main.py --strategy yesno_arb --dry-run
# With environment variable
DRY_RUN=true python main.py --strategy yesno_arb
# Live trading (use caution!)
DRY_RUN=false python main.py --strategy yesno_arb
⚠️ WARNING: This is NOT risk-free arbitrage
| Risk | Description | Mitigation |
|---|---|---|
| Partial Fills | One leg fills, other fails | Hedge manager unwinds position |
| Spread Changes | Prices move between order and fill | Slippage buffer on limits |
| Latency | Network delays cause missed opportunities | Use fast RPC, short timeouts |
| Liquidity | Thin markets have high slippage | MIN_LIQUIDITY filter |
| Settlement Risk | Market resolution disputes | Diversify across markets |
| Smart Contract Risk | CLOB or settlement bugs | Use small position sizes |
- Start with dry run mode - verify market discovery and calculations
- Use small notional - set
TRADE_NOTIONAL_USDC=1for initial live tests - Monitor logs closely - watch for hedge triggers and fill issues
- Scale gradually - increase size only after consistent fills
This bot is for educational purposes only. Trading cryptocurrencies and prediction markets involves significant risk. Use at your own risk and never trade with funds you cannot afford to lose.
The YES/NO arbitrage strategy carries execution risk. Partial fills, spread slippage, and settlement disputes can result in losses. Always test thoroughly in dry-run mode before live trading.
[Your License Here]
-
State Management
- Global variables for tracking trades and prices
- Price history management
- Active trade tracking
-
Trading Logic
- Price spike detection
- Order placement with retries
- Take-profit and stop-loss management
- USDC allowance management
-
Main Loop
- Price updates
- Trade detection
- Position management
- API credential refresh