A comprehensive trading system for Polymarket prediction markets, featuring multiple trading strategies, ML-powered predictions, real-time market monitoring, and a full-stack dashboard for bot status tracking.
This project consists of several independent modules working together:
src/- Core market monitoring and data collection system (BTC 5m Tracker)trade-bot-v2/- Advanced ML-based trading bot with profit-lock enginetrade-bot-v3/- Simple 0.35 limit buy strategy with fixed sell targetstrade-bot-v4/- Dual-side limit buy strategy with stop-loss protectionbackend/- NestJS API server for dashboard datafrontend/- React dashboard for monitoring bot status and performanceml/- XGBoost model training and prediction serviceregime-monitor/- Market regime detection and monitoring
The BTC 5m Tracker is the foundation of the system. It:
- Monitors 5-minute BTC up/down prediction markets on Polymarket
- Tracks wallet activity and identifies "hot wallets" (successful traders)
- Collects real-time market features (orderbook, volume, wallet flows)
- Stores predictions and outcomes in MongoDB
- Writes real-time features to Redis for trading bots
- Can optionally execute trades (legacy trading logic)
Key Components:
MarketMonitor- Main market cycle processorRealtimePriceService- WebSocket price streamingMongoDBClient- Prediction and outcome storageRedisClient- Real-time feature cachePolymarketClient- Gamma API and CLOB integration
Strategy Type: ML-Powered Active Trading with Advanced Profit Management
Key Features:
- ML/Ensemble Predictions: Uses XGBoost model or weighted ensemble (hot wallets + orderbook + volume)
- Velocity Risk Management: Monitors BTC/ETH price velocity to block/reduce trades during high volatility
- Advanced Profit Lock Engine: Multi-stage profit protection with T1/T2 targets, trailing stops, time-based flattening, and collapse protection
- Market Orders: Fast execution using market orders only
- Real-time Price Stream: WebSocket-based top-of-book price updates
Buy Logic:
- Waits for prediction window (default: 2.5-4.5 minutes after market start)
- Requires confidence β₯ 0.65 and price in band (0.4-0.8)
- Can "chase" favorable moves when velocity is strongly favorable
- Velocity guard blocks/reduces size during adverse conditions
Sell Logic:
- T1: Sell 20% at target price (entry + 20% of remaining profit potential)
- T2: Sell 30% at higher target (entry + 50% of remaining profit potential, boosted by favorable velocity)
- Trailing stop: Protects profits after T1 or 30s hold
- Collapse: Sells 50% if price drops significantly (threshold varies by velocity)
- Time flatten: Sells all remaining at 4.5 minutes (configurable)
- Reversal detection: Skips collapse/flatten if velocity suggests price recovery
Best For: Active trading with sophisticated risk management and profit optimization.
Strategy Type: Simple Limit Order Strategy
Key Features:
- GTD Limit Orders: Good-Till-Date limit buy orders at fixed price (0.35)
- Fixed Sell Targets: Simple two-stage profit taking
- Low Maintenance: Minimal logic, runs independently
Buy Logic:
- Places GTD limit buy order at 0.35 on target side (Up/Down/auto)
- Order expires after 2.5 minutes if not filled
- Automatically places new order if previous expires or fills
Sell Logic:
- Sell 50% when mid price reaches 0.4
- Sell remaining 50% when mid price reaches 0.5
- Flattens position if market ends or switches
Best For: Simple, set-and-forget trading with minimal complexity.
Strategy Type: Dual-Side Limit Buy with Stop-Loss
Key Features:
- Both Sides: Places limit buy orders on both Up and Down tokens simultaneously
- Early Entry: Orders placed within first minute of market open
- Stop-Loss Protection: Automatic sell if price drops below 0.15
- Auto-Redeem: Automatically redeems winning positions after market resolution
Buy Logic:
- Places GTC limit buy orders at 0.45 on both Up and Down tokens
- Orders placed only during first 60 seconds of market
- Each side uses independent position tracking
Sell Logic:
- Stop-loss: Sells immediately if mid price < 0.15 (prevents large losses)
- Market end: Flattens all positions before resolution
- Auto-redeem: Redeems winning tokens after condition resolves
Best For: Market-neutral strategies or when you want exposure to both outcomes.
NestJS REST API that provides:
- Current market state and predictions
- Wallet statistics and balance tracking
- ML prediction accuracy metrics
- Historical predictions and results
- Redis state inspection
Endpoints:
GET /api/current-market-state- Live market dataGET /api/ml/current- Current ML predictionGET /api/wallet-stats- Wallet balance and trading statsGET /api/predictions- Historical predictionsGET /api/prediction-accuracy- Model performance metrics
Purpose: Serves data to the frontend dashboard and can be used for external monitoring.
React (Vite) dashboard for real-time bot monitoring:
- Dashboard Page: Live market prices, current ML prediction, wallet stats
- Predictions Page: Historical predictions with accuracy tracking
- Wallets Page: Hot wallet tracking and analysis
- Auto-refresh: Polls backend every 5 seconds
Purpose: Visual monitoring of bot status, predictions, and performance.
XGBoost-based machine learning system:
- Training: Trains on historical predictions with actual outcomes
- Features: Uses market features (hot wallet activity, orderbook imbalance, volume ratios, etc.)
- Prediction Service: HTTP API for real-time predictions
- Auto-retraining: Automatically retrains after each market resolution
Features Used:
- Hot wallet volume and win rates
- Orderbook imbalance
- Volume ratios
- Trade counts
- BTC price delta at prediction time
Purpose: Provides ML predictions to trading bots (especially v2) for better entry decisions.
Market regime detection system for identifying different market conditions.
Purpose: Helps adapt trading strategies based on market volatility and conditions.
- Tracker (
src/) monitors markets every 30 seconds - Collects wallet activity, orderbook data, volume metrics
- Writes real-time features to Redis (
realtime:features:{conditionId}) - Stores predictions and outcomes in MongoDB
- ML service reads from MongoDB for training
- Bot starts and subscribes to current market WebSocket
- Price stream updates best bid/ask in real-time
- Main loop (every 100ms):
- Checks if prediction window has passed
- Reads features from Redis
- Gets ML prediction (or uses ensemble)
- Evaluates buy conditions (price band, confidence, velocity)
- Executes market buy if conditions met
- Monitors position and evaluates profit-lock signals
- Executes sells based on profit-lock engine
- Market switch detected every 5 seconds, bot switches to new market
- Markets resolve and outcomes are recorded
- Resolver triggers auto-training (if enabled)
ml/train.pyloads resolved predictions from MongoDB- Trains XGBoost model on features vs actual outcomes
- Saves model to
ml/artifacts/ - Prediction service loads new model for next predictions
- Node.js 18+
- Python 3.9+ (for ML module)
- MongoDB (for predictions storage)
- Redis (for real-time features)
- Polygon RPC access (for on-chain operations)
- Polymarket CLOB credentials
-
Clone and install dependencies:
git clone <repo> cd Polymarket-AI-Trading-Bot npm install
-
Configure environment:
# Main tracker cp .env.example .env # Edit .env with MongoDB, Redis, CLOB credentials # Trading bot v2 cd trade-bot-v2 cp .env.example .env # Edit .env with trading parameters # Backend cd ../backend cp .env.example .env # Frontend cd ../frontend cp .env.example .env
-
Start services:
# Terminal 1: Tracker (data collection) npm start # Terminal 2: ML Prediction Service cd ml python -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate pip install -r requirements.txt python predict_server.py # Terminal 3: Trading Bot v2 cd trade-bot-v2 npm run dev # Terminal 4: Backend API cd backend npm run start:dev # Terminal 5: Frontend Dashboard cd frontend npm run dev
Markets run in 5-minute windows. Each window:
- Starts at a fixed time (e.g., :00, :05, :10, :15, etc.)
- Has a condition ID that resolves to Up or Down
- Uses BTC price at window start vs window end to determine outcome
Wallets that have shown consistent profitability. The system:
- Tracks wallet win rates
- Monitors their trading activity
- Uses their volume and direction as a prediction signal
Sophisticated profit protection system:
- T1/T2 Targets: Partial profit taking at calculated price levels
- Trailing Stop: Protects profits by following price up
- Collapse Protection: Sells partial position if price drops significantly
- Time Flatten: Ensures position is closed before market end
- Velocity Integration: Adjusts thresholds based on underlying asset volatility
Monitors BTC/ETH price velocity ($/second):
- Blocks buys if velocity is too high and adverse to position
- Reduces size if velocity is moderately high
- Allows wider bands if velocity is strongly favorable
- Tightens profit lock during high volatility
Tracker (src/):
MONGODB_URI- MongoDB connection stringREDIS_HOST,REDIS_PORT- Redis connectionMARKET_SLUG_PREFIX- Market slug pattern (e.g.,btc-updown-5m-)ML_SERVICE_URL- ML prediction service URL
Trading Bot v2:
BUY_PRICE_MIN,BUY_PRICE_MAX- Price band for entries (default: 0.4, 0.8)MIN_CONFIDENCE- Minimum confidence to trade (default: 0.65)BUY_AMOUNT_USD- Position size in USDVELOCITY_ENABLED- Enable velocity risk managementPREDICTION_MIN_ELAPSED_SECONDS- Wait time before trading (default: 150)
Trading Bot v3:
BUY_TARGET_PRICE- Limit buy price (default: 0.35)SELL_T1_PRICE,SELL_T2_PRICE- Sell targets (default: 0.4, 0.5)GTD_LIFETIME_SEC- Order expiration (default: 150)
Trading Bot v4:
BUY_LIMIT_PRICE- Limit buy price for both sides (default: 0.45)SELL_IF_BELOW- Stop-loss threshold (default: 0.15)BUY_WINDOW_SEC- Time window to place orders (default: 60)
Polymarket-AI-Trading-Bot/
βββ src/ # Core tracker (market monitoring, data collection)
β βββ services/ # Market monitor, trading service, etc.
β βββ clients/ # Polymarket, Redis, MongoDB clients
β βββ scripts/ # Utility scripts
βββ trade-bot-v2/ # ML-powered trading bot
β βββ src/
β β βββ strategy/ # Decision logic, profit lock, ML prediction
β β βββ executor/ # Order execution
β β βββ price/ # Price streaming
β β βββ risk/ # Velocity risk management
β βββ docs/ # Strategy documentation
βββ trade-bot-v3/ # Simple 0.35 limit buy strategy
βββ trade-bot-v4/ # Dual-side limit buy strategy
βββ backend/ # NestJS API for dashboard
βββ frontend/ # React dashboard
βββ ml/ # XGBoost training and prediction
β βββ train.py # Model training
β βββ predict_server.py # Prediction API
β βββ artifacts/ # Trained models
βββ regime-monitor/ # Market regime detection
βββ docs/ # Additional documentation
-
Use v2 if you want:
- ML-powered predictions
- Advanced profit management
- Active risk management
- Best for experienced traders
-
Use v3 if you want:
- Simple, predictable strategy
- Low maintenance
- Fixed entry/exit points
- Best for beginners
-
Use v4 if you want:
- Market-neutral exposure
- Early market entry
- Stop-loss protection
- Best for risk-averse strategies
docs/FLOW.md- Detailed system flowdocs/PROFIT_LOCK_ENGINE.md- Profit lock engine specificationdocs/PROFIT_LOCK_LOGIC.md- Profit lock logic detailsdocs/REDIS_STRUCTURE.md- Redis key structuretrade-bot-v2/docs/- v2 strategy documentation
-
Trading Risk: All trading bots execute real trades. Start with small amounts and test thoroughly.
-
Credentials: Store private keys securely. Use proxy wallets when possible.
-
Market Conditions: Strategies may perform differently in different market regimes.
-
ML Model: Requires sufficient training data (50+ resolved predictions minimum).
-
Network: Requires stable connection for WebSocket price streams.
-
Monitoring: Always monitor bot activity, especially during high volatility periods.