Python High-Frequency Trading Infrastructure for Prediction Markets
A high-frequency trading (HFT) market making system built for Polymarket's prediction markets. This system uses real-time orderbook analysis, signal generation, and automated execution to provide liquidity while maintaining profitability through intelligent market making strategies.
Key Features:
- Real-time Market Making: Automated bid-ask spread management with dynamic pricing
- Signal-Based Trading: BPS (Basis Points Spread) threshold monitoring for entry signals
- Intelligent Hedging: Automatic position hedging to manage risk exposure
- Performance Optimization: CPU affinity control, GC management, and async architecture
- Risk Management: Position limits, profit margins, and automated trade execution
- WebSocket Integration: Real-time market data streaming with automatic reconnection
┌─────────────────────────────────────────────────────────┐
│ TRADING ENGINE │
├─────────────────────────────────────────────────────────┤
│ OrderBook │ Signal Generator │ Risk Manager │
│ ├─ WebSocket │ ├─ BPS Analysis │ ├─ Position │
│ ├─ Real-time │ ├─ Threshold │ │ Limits │
│ │ Updates │ │ Monitoring │ ├─ Profit │
│ └─ Price Data │ └─ Entry Signals │ │ Margins │
│ │ │ └─ Exposure │
├─────────────────────────────────────────────────────────┤
│ EXECUTION LAYER │
│ Market Maker │ Hedging System │ Monitoring │
│ ├─ Order │ ├─ Fill │ ├─ Logging │
│ │ Management │ │ Detection │ ├─ Performance │
│ ├─ Spread │ ├─ Automatic │ │ Metrics │
│ │ Calculation │ │ Hedging │ ├─ Health │
│ └─ P&L Tracking │ └─ State Mgmt │ │ Checks │
│ │ │ └─ Alerts │
└─────────────────────────────────────────────────────────┘
1. Main Trading Engine (main.py)
The core trading loop that orchestrates all system components:
2. OrderBook Management (utils/orderbook.py)
Real-time market data processing with WebSocket connections:
- WebSocket Integration: Connects to Polymarket's real-time data feed
- Signal Generation: Detects trading opportunities based on BPS thresholds
- Market State: Maintains current bid/ask prices and spread analysis
3. Order Management (utils/clob_orders.py)
Handles order placement and hedging strategies.
- Position Limits: Maximum concurrent trades via
MAX_TRADESconfiguration - Profit Margins: Configurable minimum profit per trade
- Trade Tracking: Real-time position monitoring and P&L calculation
- Market Windows: Time-based trading restrictions
gc.disable() # Eliminates GC pauses during tradingWhy: Python's garbage collector can cause trading delays. We disable it during active trading and manually trigger cleanup during quiet periods.
def set_cpu_affinity():
affinity_cores = [cpu_count - 2, cpu_count - 1] # Use dedicated cores
process.cpu_affinity(affinity_cores)
process.nice(psutil.HIGH_PRIORITY_CLASS) # High priorityBenefits:
- Dedicated CPU cores for trading operations
- High process priority for consistent performance
- Reduced interference from other system processes
Advantages:
- Non-blocking network operations
- Concurrent order processing
- Thread-safe state management
The system employs a market making strategy that focuses on:
- Spread Analysis: Monitors bid-ask spreads and identifies profitable opportunities
- BPS Threshold Trading: Uses configurable basis point thresholds (default: 50 BPS) to trigger trades
- Automated Hedging: Places hedge orders automatically to manage risk exposure
- Position Limits: Enforces maximum trade limits (configurable via
MAX_TRADES)
# BPS-based signal detection
if current_spread_bps >= TRADING_BPS_THRESHOLD:
# Generate trading signal
place_anchor_and_hedge(market_data)Key Parameters:
TRADING_BPS_THRESHOLD: Minimum spread required to trigger a trade (50 BPS default)PROFIT_MARGIN: Minimum profit margin per trade (2% default)MAX_TRADES: Maximum concurrent positions (2 default)
- Position Sizing: Controlled position sizes based on available capital
- Automatic Hedging: Every market making trade is automatically hedged
- Market Time Windows: Trades only during active market sessions
- Stop-Loss Protection: Built-in safeguards against adverse moves
The system provides comprehensive logging and monitoring capabilities:
- logs/: Trading activity logs
- Real-time orderbook updates
- Trading signal generation logs
- P&L tracking and reporting
- System health monitoring
Enable detailed logging by modifying the logger configuration in utils/logger.py.
License: This project is for educational and research purposes only.
Important Disclaimers:
- This software is provided as-is for educational purposes
- Trading involves substantial risk of financial loss
- Users are responsible for compliance with applicable financial regulations
- Past performance does not guarantee future results
- The authors are not responsible for any financial losses incurred
- Use at your own risk and ensure proper testing before live trading
# System Requirements
Python 3.11+
4+ CPU cores recommended
2GB+ RAM for orderbook processing
Stable internet connection- Clone the repository:
git clone https://github.com/nawaz0x1/py_polymarket_hft_mm
cd py_polymarket_hft_mm- Setup:
# Automated setup (Linux/Unix)
./setup.sh
# Manual setup
pip install -r requirements.txt- Configuration:
Create your configuration by editing config.py:
# Trading Parameters
TRADING_BPS_THRESHOLD = 50 # BPS threshold for trade signals
PROFIT_MARGIN = 0.02 # Minimum profit margin (2%)
MAX_TRADES = 2 # Maximum concurrent trades
PLACE_OPPOSITE_ORDER = True # Enable automatic hedging
# Performance Settings
REQUEST_TIMEOUT = 5 # API request timeout
MARKET_SESSION_SECONDS = 900 # Market session duration- API Setup:
- Set up your Polymarket CLOB API credentials
- Ensure proper wallet configuration for order signing
- Test connection with small trades first
# Launch the trading system
./run.sh
# Or run directly with Python
sudo env "PATH=$PATH" python main.pyAuthor: Shah Nawaz Haider
GitHub: @nawaz0x1
X (Twitter): @nawaz0x1
LinkedIn: Shah Nawaz Haider
If you find this project useful, consider supporting further development:
Crypto Donations:
- Ethereum (ETH):
0x89ae2f064cf2cb06a5e66a8e9ea6b653dcb93cfa - Solana (SOL):
2V4g71bG6dJyqv4REZeZSCtiF4pQauDvRiLy8MDNjWNv
Your support helps maintain and improve the trading algorithms!
FOR EDUCATIONAL AND RESEARCH PURPOSES ONLY
This software is provided strictly for educational, research, and demonstration purposes. By using this code, you acknowledge and agree to the following:
- HIGH RISK: Trading and market making involve substantial risk of financial loss
- NO GUARANTEES: Past performance does not guarantee future results
- CAPITAL LOSS: You may lose some or all of your invested capital
- MARKET VOLATILITY: Prediction markets are highly volatile and unpredictable
- USER RESPONSIBILITY: You are solely responsible for compliance with all applicable laws and regulations in your jurisdiction
- NO LEGAL ADVICE: This software does not constitute financial, legal, or investment advice
- REGULATORY COMPLIANCE: Ensure compliance with securities laws, derivatives regulations, and financial services requirements
- JURISDICTION SPECIFIC: Trading regulations vary by country and may prohibit certain activities
- NO WARRANTY: This software is provided "AS IS" without any warranties, express or implied
- BUGS AND ERRORS: The software may contain bugs, errors, or security vulnerabilities
- NO SUPPORT: No guarantee of maintenance, updates, or technical support
- THIRD-PARTY DEPENDENCIES: Relies on external APIs and services that may change or become unavailable
- NO LIABILITY: The authors and contributors are not liable for any financial losses, damages, or consequences
- USER ASSUMES RISK: You use this software entirely at your own risk
- INDEMNIFICATION: You agree to indemnify and hold harmless the authors from any claims or damages
- TEST THOROUGHLY: Always test extensively with small amounts before any live trading
- MONITOR CONSTANTLY: Automated trading systems require constant monitoring
- TECHNICAL KNOWLEDGE: Requires significant technical knowledge to operate safely
- API CHANGES: External API changes may break functionality without notice
By using this software, you acknowledge that you have read, understood, and agree to these terms.