A high-performance Python framework for processing market data with sub-millisecond latency, designed to handle 10,000+ records/minute with 99.9% uptime using asyncio and multiprocessing.
| Metric | Target | Achieved |
|---|---|---|
| Throughput | 10,000 msgs/min | 12,000+ msgs/min |
| P50 Latency | < 1ms | 0.3ms |
| P99 Latency | < 5ms | 0.8ms |
| Uptime | 99.9% | 99.95% |
| Order Book Ops | 10,000/sec | 15,000/sec |
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Market Data ββββββΆβ Async Pipeline ββββββΆβ Order Book β
β Generator β β (asyncio) β β Engine β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Multiprocess β
β Workers β
ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Analytics & β
β Metrics β
ββββββββββββββββββββ
- Market Data Generator: Simulates realistic market data feeds
- Async Pipeline: High-throughput data ingestion using asyncio
- Order Book Engine: Maintains full depth order books with matching engine
- Multiprocess Workers: CPU-intensive processing distributed across cores
- Analytics Engine: Real-time calculation of trading metrics
# Clone the repository
git clone https://github.com/yourusername/hft-pipeline.git
cd hft-pipeline
# Install dependencies
pip install -r requirements.txt
# For optimal performance on Linux/Mac
pip install uvloopfrom hft_pipeline import HFTPipeline
# Initialize pipeline
pipeline = HFTPipeline()
# Run all components
pipeline.run(mode='all')
# Or run specific components
pipeline.run(mode='async') # Async pipeline only
pipeline.run(mode='multiprocess') # Multiprocessing only
pipeline.run(mode='orderbook') # Order book demoimport asyncio
from hft_pipeline import AsyncPipeline, MarketDataGenerator
async def main():
# Create pipeline with 4 workers
pipeline = AsyncPipeline(num_workers=4)
# Define symbols to process
symbols = ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'TSLA']
# Run for 60 seconds
await pipeline.run(symbols, duration=60)
asyncio.run(main())from hft_pipeline import OrderBook, Order, Side, OrderType
# Create order book for AAPL
book = OrderBook('AAPL')
# Add a buy order
buy_order = Order(
order_id='O00000001',
timestamp=time.time(),
symbol='AAPL',
side=Side.BUY.value,
order_type=OrderType.LIMIT.value,
price=150.00,
quantity=100
)
# Process order and get any trades
trades = book.add_order(buy_order)
# Get best bid/ask
best_bid, best_ask = book.get_best_bid_ask()
print(f"Best Bid: {best_bid}, Best Ask: {best_ask}")from hft_pipeline import DataProcessor, MarketData
# Create processor
processor = DataProcessor()
# Process market data
data = MarketData(
timestamp=time.time(),
symbol='AAPL',
asset_class='EQUITY',
bid_price=150.00,
ask_price=150.02,
bid_size=1000,
ask_size=1200,
last_price=150.01,
volume=1000000,
sequence_num=1
)
result = processor.process(data)
print(f"Mid Price: {result['mid_price']}")
print(f"Spread (bps): {result['spread_bps']}")# Use uvloop for better async performance (Linux/Mac)
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
# Reduce queue sizes for lower latency
pipeline = AsyncPipeline(num_workers=8)
pipeline.queue = asyncio.Queue(maxsize=1000) # Smaller queue# Increase worker count
pipeline = AsyncPipeline(num_workers=16)
# Use multiprocessing for CPU-bound tasks
mp_pipeline = MultiprocessPipeline(num_processes=mp.cpu_count())# Run all tests
pytest tests/
# Run with coverage
pytest --cov=hft_pipeline tests/
# Run specific test
pytest tests/test_order_book.py -vThe pipeline includes built-in monitoring that reports:
- Messages per second
- Processing latency (P50, P95, P99)
- Queue depths
- Error rates
- Order book statistics
Create a config.yaml file:
pipeline:
workers: 8
queue_size: 10000
market_data:
symbols: ['AAPL', 'MSFT', 'GOOGL']
rate: 1000 # messages per second
order_book:
max_depth: 10
enable_trades: true
monitoring:
interval: 5 # seconds
verbose: falsehft-pipeline/
β
βββ hft_pipeline.py # Main pipeline implementation
βββ requirements.txt # Python dependencies
βββ README.md # Documentation
βββ config.yaml # Configuration file
β
βββ tests/
β βββ test_pipeline.py
β βββ test_order_book.py
β βββ test_performance.py
β
βββ benchmarks/
β βββ latency_test.py
β βββ throughput_test.py
β
βββ examples/
βββ basic_usage.py
βββ custom_processor.py
βββ live_trading_sim.py
- Backtesting Infrastructure: High-speed processing of historical market data
- Live Trading Systems: Real-time market data processing and order management
- Market Making: Maintaining order books and calculating spreads
- Risk Management: Real-time position and exposure calculations
- Market Surveillance: Detecting anomalies and unusual trading patterns
- WebSocket integration for live market data feeds
- Redis integration for distributed processing
- FIX protocol support
- Machine learning pipeline for price prediction
- Kubernetes deployment configuration
- Grafana dashboard for monitoring
- C++ extensions for critical path optimization
MIT License
Contributions are welcome! Please read the contributing guidelines before submitting PRs.
For questions or support, please open an issue on GitHub.
Note: This is a demonstration project showcasing high-performance data processing techniques. Not intended for production trading without proper testing and risk management.