A sophisticated automated trading bot for Polymarket that detects price spikes and executes trades on the Polygon network. The bot implements a spike detection strategy with automatic position management and risk controls.
The 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
.env
file 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.0
PK
: 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
.env
file is properly configured - Run the bot:
python test.py
The 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
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.
[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