2026-01-02 18:19:46 +06:00
# Polymarket HFT Market Making System
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
_Python High-Frequency Trading Infrastructure for Prediction Markets_
2026-01-02 18:09:14 +06:00
[]()
[]()
2026-01-03 14:21:49 +06:00
[]()
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
## Overview
2026-01-02 18:09:14 +06:00
2026-01-04 21:42:28 +06:00
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.
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
**Key Features:**
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
- **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
2026-01-04 21:42:28 +06:00
- **WebSocket Integration**: Real-time market data streaming with automatic reconnection
2026-01-02 18:09:14 +06:00
---
## System Architecture
### Core Components
```
2026-01-03 14:21:49 +06:00
┌─────────────────────────────────────────────────────────┐
│ 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 │
└─────────────────────────────────────────────────────────┘
```
2026-01-04 21:42:28 +06:00
## Key Components
2026-01-03 14:21:49 +06:00
2026-01-04 21:42:28 +06:00
### 1. Main Trading Engine ([main.py](main.py))
2026-01-03 14:21:49 +06:00
2026-01-04 21:42:28 +06:00
The core trading loop that orchestrates all system components:
2026-01-03 14:21:49 +06:00
2026-01-04 21:42:28 +06:00
### 2. OrderBook Management ([utils/orderbook.py](utils/orderbook.py))
2026-01-03 14:21:49 +06:00
2026-01-04 21:42:28 +06:00
Real-time market data processing with WebSocket connections:
2026-01-03 14:21:49 +06:00
2026-01-04 21:42:28 +06:00
- **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
2026-01-03 14:21:49 +06:00
2026-01-04 21:42:28 +06:00
### 3. Order Management ([utils/clob_orders.py](utils/clob_orders.py))
2026-01-03 14:21:49 +06:00
2026-01-04 21:42:28 +06:00
Handles order placement and hedging strategies.
2026-01-03 14:21:49 +06:00
2026-01-04 21:42:28 +06:00
### 4. Risk Management
2026-01-03 14:21:49 +06:00
2026-01-04 21:42:28 +06:00
- **Position Limits**: Maximum concurrent trades via `MAX_TRADES` configuration
- **Profit Margins**: Configurable minimum profit per trade
- **Trade Tracking**: Real-time position monitoring and P&L calculation
- **Market Windows**: Time-based trading restrictions
2026-01-02 18:09:14 +06:00
---
## Performance Engineering
### 1. Garbage Collection Management
```python
gc . disable () # Eliminates GC pauses during trading
```
**Why:** Python's garbage collector can cause trading delays. We disable it during active trading and manually trigger cleanup during quiet periods.
### 2. CPU Optimization
```python
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 priority
```
**Benefits:**
- Dedicated CPU cores for trading operations
- High process priority for consistent performance
- Reduced interference from other system processes
### 3. Async Architecture
**Advantages:**
- Non-blocking network operations
- Concurrent order processing
- Thread-safe state management
### 4. Efficient Data Processing
---
2026-01-03 14:21:49 +06:00
## Trading Strategy
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
### Market Making Approach
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
The system employs a market making strategy that focuses on:
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
1. **Spread Analysis** : Monitors bid-ask spreads and identifies profitable opportunities
2. **BPS Threshold Trading** : Uses configurable basis point thresholds (default: 50 BPS) to trigger trades
3. **Automated Hedging** : Places hedge orders automatically to manage risk exposure
4. **Position Limits** : Enforces maximum trade limits (configurable via `MAX_TRADES` )
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
### Signal Generation
2026-01-02 18:09:14 +06:00
```python
2026-01-03 14:21:49 +06:00
# BPS-based signal detection
if current_spread_bps >= TRADING_BPS_THRESHOLD :
# Generate trading signal
place_anchor_and_hedge ( market_data )
2026-01-02 18:09:14 +06:00
```
2026-01-03 14:21:49 +06:00
**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)
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
### Risk Management
- **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
2026-01-02 18:09:14 +06:00
---
2026-01-03 14:21:49 +06:00
## Monitoring & Logging
The system provides comprehensive logging and monitoring capabilities:
### Log Files
- **[logs/ ](logs/ )**: Trading activity logs
### Performance Monitoring
- Real-time orderbook updates
- Trading signal generation logs
- P&L tracking and reporting
- System health monitoring
### Debug Information
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
Enable detailed logging by modifying the logger configuration in [utils/logger.py ](utils/logger.py ).
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
---
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
## License & Disclaimer
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
**License** : This project is for educational and research purposes only.
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
**Important Disclaimers:**
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
- 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
2026-01-02 18:09:14 +06:00
---
2026-01-03 14:21:49 +06:00
## Installation & Setup
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
### Prerequisites
2026-01-02 18:09:14 +06:00
2026-01-01 22:25:40 +06:00
```bash
2026-01-03 14:21:49 +06:00
# System Requirements
Python 3.11+
4+ CPU cores recommended
2GB+ RAM for orderbook processing
Stable internet connection
2026-01-02 18:09:14 +06:00
```
### Installation
2026-01-03 14:21:49 +06:00
1. **Clone the repository:**
2026-01-02 18:09:14 +06:00
```bash
git clone https://github.com/nawaz0x1/py_polymarket_hft_mm
cd py_polymarket_hft_mm
2026-01-03 14:21:49 +06:00
```
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
2. **Setup:**
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
```bash
2026-01-04 21:42:28 +06:00
# Automated setup (Linux/Unix)
2026-01-03 14:21:49 +06:00
./setup.sh
2026-01-02 18:09:14 +06:00
2026-01-04 21:42:28 +06:00
# Manual setup
pip install -r requirements.txt
2026-01-03 14:21:49 +06:00
```
2026-01-02 18:09:14 +06:00
2026-01-04 21:42:28 +06:00
3. **Configuration:**
2026-01-02 18:09:14 +06:00
2026-01-04 21:42:28 +06:00
Create your configuration by editing [config.py ](config.py ):
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
```python
# 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
```
2026-01-02 18:09:14 +06:00
2026-01-04 21:42:28 +06:00
4. **API Setup:**
- Set up your Polymarket CLOB API credentials
- Ensure proper wallet configuration for order signing
- Test connection with small trades first
### Quick Start
```bash
# Launch the trading system
./run.sh
# Or run directly with Python
sudo env "PATH= $PATH " python main.py
```
2026-01-02 18:09:14 +06:00
---
2026-01-03 14:21:49 +06:00
## Contact & Support
2026-01-02 18:09:14 +06:00
2026-01-03 14:21:49 +06:00
**Author** : Shah Nawaz Haider
**GitHub** : [@nawaz0x1 ](https://github.com/nawaz0x1 )
**X (Twitter)** : [@nawaz0x1 ](https://x.com/nawaz0x1 )
**LinkedIn** : [Shah Nawaz Haider ](https://www.linkedin.com/in/nawazhaider/ )
2026-01-02 18:09:14 +06:00
---
2026-01-03 14:21:49 +06:00
### Support Development
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!
2026-01-02 18:09:14 +06:00
2026-01-03 14:26:35 +06:00
---
## ⚠️ DISCLAIMER
**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:
### Financial Risk Warning
2026-01-04 21:42:28 +06:00
2026-01-03 14:26:35 +06:00
- **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
### Legal and Regulatory Compliance
2026-01-04 21:42:28 +06:00
2026-01-03 14:26:35 +06:00
- **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
### Software Limitations
2026-01-04 21:42:28 +06:00
2026-01-03 14:26:35 +06:00
- **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
### Liability Disclaimer
2026-01-04 21:42:28 +06:00
2026-01-03 14:26:35 +06:00
- **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
### Additional Warnings
2026-01-04 21:42:28 +06:00
2026-01-03 14:26:35 +06:00
- **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.**
2026-01-02 18:09:14 +06:00
�