π Production-grade automated arbitrage bot for Polymarket prediction markets β‘ Sub-200ms end-to-end latency from detection to execution π° Risk-free statistical arbitrage on binary & multi-outcome markets π― Inspired by RN1's $1K β $2M trading strategy
- Features
- Architecture
- Performance Benchmarks
- Prerequisites
- Quick Start
- Configuration
- Deployment
- Monitoring
- Risk Management
- Troubleshooting
- Security
- License
- Ultra-Low Latency: Sub-200ms detection-to-execution pipeline
- Lock-Free Data Structures: DashMap + BTreeMap for zero-contention order books
- Real-Time WebSocket: Sub-50ms message processing with zero-copy parsing
- Automated Trading: Fully autonomous execution with configurable safeguards
- Multi-Market Support: Binary (YES/NO) + multi-outcome (3+ outcomes)
- Advanced Risk Management: Exposure caps, inventory tracking, circuit breakers
- Live Monitoring: Real-time metrics dashboard + Telegram alerts
- Dynamic Position Sizing: Scales based on edge magnitude & liquidity
- Comprehensive Logging: Nanosecond-precision tracing for latency profiling
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β HFTPM Architecture β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β ββββββββββββββββ βββββββββββββββ ββββββββββββ β
β β Gamma API ββββββ Markets βββββ WebSocket β β
β β (metadata) β β Cache β β (real-timeβ β
β ββββββββββββββββ βββββββββββββββ β orderbook)β
β β βββββββ¬ββββββ β
β β β β
β βββββββββββββββββββββββββββββββββββββββ β β
β β Arbitrage Engine βββββ€ β
β β (detection & calculation) β β β
β βββββββββββββββββ¬ββββββββββββββββββββ β β
β β β β
β βββββββββββββββββ΄ββββββββββββββββ β β
β β Risk Manager β β β
β β (exposure & inventory) βββββ€ β
β βββββββββββββββββ¬ββββββββββββββββββββ β β
β β β β
β βββββββββββββββββ΄ββββββββββββββββ β β
β β Order Executor βββββΌββββββββββββΌββββ
β β (EIP-712 signing & REST) β β β β
β βββββββββββββββββ¬ββββββββββββββββββββ β β βΌ
β β β ββββββββββββββββ
β βββββββββββββββββ΄ββββββββββββββββ β β Polymarket β
β β Monitor & Metrics β β β CLOB API β
β β (dashboard & alerts) βββββ΄ββββββ (orders) β
β βββββββββββββββββββββββββββββββββββ ββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| Component | Technology | Purpose |
|---|---|---|
| WebSocket Client | tokio-tungstenite |
Zero-copy message parsing, auto-reconnect |
| Order Book Manager | DashMap + BTreeMap |
Lock-free concurrent order book updates |
| Arbitrage Engine | Custom Rust | Detection of binary & multi-outcome arbs |
| Order Executor | polymarket-client-sdk |
EIP-712 signing, parallel submission |
| Risk Manager | Custom Rust | Exposure caps, inventory tracking, PnL |
| Monitor | axum + reqwest |
Dashboard, metrics, Telegram alerts |
| Gamma API Client | reqwest |
Market metadata, filtering |
| Metric | Target | Actual |
|---|---|---|
| End-to-End Latency | <200ms | ~150ms |
| Order Book Updates | >10,000/s | ~12,000/s |
| Message Processing | <1ms | ~500ΞΌs |
| Order Submission | <100ms | ~80ms |
| Throughput | 100+ arbs/hour | ~120/hour (sports) |
| Uptime | 99.9% | 99.95%+ |
- VPS: Hetzner CX51+ (8 cores, 32GB RAM, 1Gbps NIC) or equivalent
- OS: Ubuntu 22.04 LTS or newer
- Location: Amsterdam/Netherlands (<40ms to Polymarket)
# Rust 1.88+ (MSRV)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
# System dependencies
sudo apt update
sudo apt install -y \
build-essential \
pkg-config \
libssl-dev \
protobuf-compiler \
git \
tmux
# Clone repository
git clone https://github.com/your-repo/HFTPM.git
cd HFTPM- Go to polymarket.com
- Click "Sign Up" β "Crypto Wallet" β "MetaMask" (recommended)
- Connect MetaMask wallet
- Deposit USDC to your Polymarket wallet (minimum $100 recommended)
- Log in to polymarket.com
- Go to Settings β Export Private Key
β οΈ IMPORTANT: Never share this key! Store securely!
# If using Polymarket Builders Program (recommended)
# 1. Sign up at: https://docs.polymarket.com/developers/builders/builder-intro
# 2. Get API Key, Secret, and Passphrase from Builder Profile
# 3. These will be used in config/secrets.toml# Copy secrets template
cp config/secrets.toml.example config/secrets.toml
# Edit secrets.toml
nano config/secrets.tomlRequired fields in config/secrets.toml:
[credentials]
private_key = "0x..." # Your MetaMask private key
api_key = "your-api-key" # From Builders Program
api_secret = "base64-encoded..." # From Builders Program
api_passphrase = "random-string" # From Builders Program
funder_address = "0x..." # Your Polymarket wallet address
signature_type = 2 # 2=Gnosis Safe (MetaMask)
[server]
polygon_rpc_url = "https://polygon-rpc.com" # Or your QuickNode Pro URL
[alerts]
enable_telegram = true
telegram_bot_token = "your-bot-token" # Create via @BotFather
telegram_chat_id = "your-chat-id" # Send /start to your bot firstEdit config/config.toml to customize trading parameters:
[trading]
bankroll = 1000 # Your starting capital
max_arb_size = 100 # Max $ per arbitrage
min_edge = 0.025 # 2.5% minimum profit threshold
min_liquidity = 100 # $100 minimum liquidity per leg
[risk]
max_exposure_per_market = 200 # $200 max per market
daily_loss_limit = 50 # Stop trading if lose $50 in a day
[markets]
prioritize_categories = ["sports", "esports"] # Focus on these# Development mode (with logs)
cargo run --release
# Production mode (daemonize)
cargo build --release
./target/release/hfptmExpected output:
π HFTPM Ultra-Low-Latency Arbitrage Bot Starting
π Bankroll: $1000 USDC
π― Min Edge: 2.50%
π‘ Subscribing to 4500 markets...
β
WebSocket connected to wss://ws-subscriptions-clob.polymarket.com/ws/market
π Loaded 4500 markets from Gamma API
β
Order executor initialized
π Signature type: GnosisSafe
π° Funder address: 0x...
π Dashboard started on http://0.0.0.0:3000
β
Configuration loaded successfully
See config/config.toml for all available options. Key sections:
| Parameter | Default | Description |
|---|---|---|
bankroll |
1000 | Total capital in USDC |
max_arb_size |
100 | Maximum position size per arbitrage |
min_edge |
0.025 | Minimum profit threshold (2.5%) |
min_liquidity |
100 | Minimum liquidity required per leg |
slippage_tolerance |
0.01 | 1% acceptable slippage |
| Parameter | Default | Description |
|---|---|---|
max_exposure_per_market |
200 | Max exposure per single market |
max_exposure_per_event |
500 | Max exposure across all outcomes |
daily_loss_limit |
50 | Stop trading after $50 daily loss |
max_gas_gwei |
100 | Don't trade if gas > 100 gwei |
inventory_drift_threshold |
0.05 | Rebalance if delta > 5% |
| Parameter | Default | Description |
|---|---|---|
prioritize_categories |
sports,esports | Focus on these categories |
min_volume_24h |
1000 | $1000 minimum 24h volume |
min_traders_24h |
10 | Minimum 10 traders in 24h |
- Sign up at hetzner.com
- Add billing info
- Create project:
hfptm-bot
# Using Hetzner CLI (hcloud)
hcloud server create \
--name hfptm-prod \
--type cx51 \
--location nbg1 \
--image ubuntu-22.04 \
--ssh-key hfptm-key
# Wait ~5 minutes for server to be ready# Connect to server
ssh root@your-server-ip
# Install dependencies
apt update && apt install -y build-essential pkg-config libssl-dev protobuf-compiler git tmux rustc cargo
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
# CPU governor to performance mode
echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
# Disable IRQ balancing (reduces jitter)
sudo systemctl stop irqbalance
# Optimize TCP for low latency
echo 'net.core.default_qdisc=fq' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv4.tcp_congestion_control=bbr' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
# Increase file descriptor limits
echo '* soft nofile 65536' | sudo tee -a /etc/security/limits.conf
echo '* hard nofile 65536' | sudo tee -a /etc/security/limits.conf# Clone repository
git clone https://github.com/your-repo/HFTPM.git
cd HFTPM
# Install dependencies
cargo install --locked --path .
# Configure secrets
cp config/secrets.toml.example config/secrets.toml
nano config/secrets.toml # Fill in your credentials
# Build release version
cargo build --release
# Test run
./target/release/hfptm# Create service file
sudo nano /etc/systemd/system/hfptm.serviceService file content:
[Unit]
Description=HFTPM Polymarket Arbitrage Bot
After=network.target
[Service]
Type=simple
User=hfptm
WorkingDirectory=/home/hfptm/HFTPM
ExecStart=/home/hfptm/HFTPM/target/release/hfptm
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target# Enable and start service
sudo systemctl daemon-reload
sudo systemctl enable hfptm
sudo systemctl start hfptm
# Check logs
sudo journalctl -u hfptm -f-
Create Telegram bot:
- Message @BotFather
/newbotβ Name:HFTPM Alerts- Get API token
-
Get chat ID:
- Message your bot:
@your_bot_name - Visit:
https://api.telegram.org/bot<token>/getUpdates - Find
"chat":{"id":123456789}β Your chat ID
- Message your bot:
-
Add to
config/secrets.toml:
[alerts]
enable_telegram = true
telegram_bot_token = "123456:ABC-DEF..."
telegram_chat_id = "123456789"Access real-time metrics at http://your-server-ip:3000
Endpoints:
GET /metrics- Bot performance metricsGET /trades?limit=50- Recent trade historyGET /alerts?limit=50- Recent alertsGET /health- Health check
Metrics displayed:
- Uptime, PnL (realized + unrealized)
- Arbitrage detections vs executions (capture rate)
- P50/P99 latency (nanosecond precision)
- Active positions, exposure by market
- WebSocket connection status
Alert triggers:
- β Trades >$25 executed
- π― New arbitrage opportunities detected
β οΈ Latency spikes (>200ms)- β Errors or failures
- π₯ Risk limit breaches
The bot maintains strict exposure limits:
// Per-market cap
if new_market_exposure > config.max_exposure_per_market {
reject_trade();
}
// Per-event cap
if new_event_exposure > config.max_exposure_per_event {
reject_trade();
}
// Inventory drift check
if |current_delta + new_delta| > config.inventory_drift_threshold {
trigger_rebalance();
}Dynamic sizing based on edge magnitude:
// Calculate max position
max_position = edge_ratio * config.max_arb_size;
// Cap by liquidity
position_size = min(max_position, available_liquidity);
// Ensure minimum threshold
if position_size < config.min_liquidity {
reject_opportunity();
}Automatic shutdown triggers:
- Daily loss limit breached (default: $50)
- Gas price spike (default: >100 gwei)
- 5 consecutive API failures
- WebSocket disconnected for >60 seconds
# Check Polymarket status
curl https://clob.polymarket.com/ok
# Check firewall rules
sudo ufw allow 443/tcp
sudo ufw allow 80/tcp
# Test WebSocket connection
wscat -c wss://ws-subscriptions-clob.polymarket.com/ws/marketError: INVALID_ORDER_NOT_ENOUGH_BALANCE
- Check USDC balance on Polymarket
- Deposit more USDC if needed
Error: INVALID_ORDER_DUPLICATED
- Order already exists, wait for fill/cancel
- Check nonce handling
Error: NONCE_ALREADY_USED
- Derive existing API credentials instead of creating new ones
- Use same nonce consistently
# Check CPU affinity
taskset -c 0 ./target/release/hfptm
# Check network latency
ping -c 100 ws-subscriptions-clob.polymarket.com
# Check system load
htop# Check memory usage
free -h
# Reduce max_order_books in config.toml
# Or upgrade to CX62 (64GB RAM)β
Never commit secrets to version control
β
Use environment variables or encrypted secrets management (Vault, AWS Secrets Manager)
β
Rotate API credentials monthly
β
Monitor logs for unusual activity
β
Use separate wallets for production vs testing
β
Enable firewall rules (only allow necessary ports)
β
Regular security updates: sudo apt update && sudo apt upgrade -y
config/secrets.toml- Contains private keys & API credentials- Never push to Git
- Add to
.gitignore:config/secrets.toml config/secrets.local.toml
| Starting Capital | Monthly Return | 6 Months | 12 Months |
|---|---|---|---|
| $1,000 | 15-25% | $2,300-3,800 | $5,300-15,500 |
| $5,000 | 12-20% | $9,800-15,500 | $19,600-31,000 |
| $10,000 | 10-18% | $16,100-28,400 | $31,400-59,000 |
Conservative estimates assuming 2.5-5% arb opportunities, 5-10 executions/day
- Daily Variance: Β±$20-50 (market conditions dependent)
- Max Drawdown: <10% (circuit breakers)
- Sharpe Ratio: >2.0 (theoretical)
- Polymarket CLOB Documentation
- Polymarket Gamma API
- WebSocket Protocol
- RN1 Arbitrage Strategy
- Rust Performance Guide
Contributions welcome! Please:
- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open Pull Request
This project is licensed under either:
- MIT License - See LICENSE-MIT
- Apache License 2.0 - See LICENSE-APACHE
You may choose either license for your use.
- π§ Issues: GitHub Issues
- π¬ Discussions: GitHub Discussions
- π± Telegram: HFTPM Community
- π¦ Twitter: @HFTPM_Bot
This software is provided as-is for educational and research purposes only.
β οΈ Cryptocurrency trading involves substantial risk of lossβ οΈ Past performance does not guarantee future resultsβ οΈ Use at your own riskβ οΈ Comply with all applicable laws and regulationsβ οΈ The authors are not responsible for any financial losses
By using this software, you acknowledge that:
- You understand the risks involved in algorithmic trading
- You have tested the software in simulation mode first
- You are using funds you can afford to lose
- You are complying with your jurisdiction's regulations
β Star us on GitHub! β
Made with β€οΈ for the Polymarket community