Skip to content

High-Frequency Trading Simulator - A comprehensive Python framework for simulating HFT strategies, backtesting, and market analysis

License

Notifications You must be signed in to change notification settings

Xyerophyte/hft-simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

πŸš€ HFT Simulator

Python 3.9+ License: MIT Build Status Code Style: Black PRs Welcome

A production-grade High-Frequency Trading simulator with machine learning integration, realistic market microstructure modeling, and comprehensive performance analytics. Built for quantitative researchers, algorithmic traders, and fintech developers.


πŸ“‹ Overview

This project implements a complete trading system infrastructure suitable for quantitative research and algorithmic trading strategy development. It combines tick-by-tick market simulation with nanosecond precision, deep learning models for price prediction, and realistic execution simulation including latency, slippage, and order book dynamics.

The HFT Simulator bridges the gap between academic research and production trading systems. Unlike simple backtesting frameworks that use candlestick data, this simulator operates at the tick level with full L2/L3 order book depth, FIFO price-time priority matching, and microsecond latency modelingβ€”the same components used by real HFT firms.

Whether you're developing market-making algorithms, testing statistical arbitrage strategies, or researching ML-driven trading signals, this platform provides the infrastructure needed to simulate realistic market conditions and measure true strategy performance.


πŸ“‘ Table of Contents


✨ Features

Core Trading Infrastructure

  • Tick-by-Tick Processing β€” Nanosecond-precision timestamps for authentic HFT simulation
  • L2/L3 Order Book β€” Full depth order book with bid/ask levels and queue position tracking
  • FIFO Matching Engine β€” Price-time priority order matching with partial fills
  • Latency Modeling β€” Network, exchange, and queue delays (configurable from Β΅s to ms)

Trading Strategies

  • Market Making β€” Bid/ask quoting with inventory management and adverse selection detection
  • Statistical Arbitrage β€” Pair trading with z-score signals and mean reversion
  • Latency Arbitrage β€” Stale quote detection and speed-based trading
  • Momentum Strategy β€” ML-enhanced trend following with risk controls

Machine Learning

  • LSTM Networks β€” PyTorch-based sequential models for price prediction
  • Transformer Models β€” Attention-based architecture for time series
  • Ensemble Methods β€” Combine multiple models for robust signals
  • 90+ Engineered Features β€” Technical indicators, microstructure features, and more

Analytics & Backtesting

  • Event-Driven Backtester β€” Realistic execution with fees and slippage
  • 40+ Performance Metrics β€” Sharpe, Sortino, max drawdown, win rate, etc.
  • Visualization Suite β€” Equity curves, drawdown charts, trade analysis

πŸ“‹ Prerequisites

Requirement Minimum Version Recommended
Python 3.9+ 3.11+
pip 21.0+ Latest
OS Windows 10 / Linux / macOS Any
RAM 4 GB 8+ GB
Storage 500 MB 2+ GB (for data)

Required Python Packages

numpy>=1.21.0
pandas>=1.3.0
torch>=1.9.0
scikit-learn>=0.24.0
PyYAML>=5.4
matplotlib>=3.4.0
requests>=2.25.0

πŸ”§ Installation

Option 1: Standard Installation

# Clone the repository
git clone https://github.com/Xyerophyte/hft-simulator.git
cd hft-simulator

# Create virtual environment (recommended)
python -m venv venv
source venv/bin/activate  # Linux/macOS
# or
.\venv\Scripts\activate   # Windows

# Install dependencies
pip install -r requirements.txt

Option 2: Development Installation

# Clone with full history
git clone https://github.com/Xyerophyte/hft-simulator.git
cd hft-simulator

# Install in editable mode with dev dependencies
pip install -e ".[dev]"

# Install pre-commit hooks
pre-commit install

Verify Installation

python -c "from src.hft import OrderBook, MatchingEngine; print('βœ… Installation successful!')"

⚑ Quick Start

Run HFT Simulation (30 seconds)

# Run market making simulation with 5000 ticks
python run_hft.py --ticks 5000 --strategy market_maker

Run ML Backtest

# Run full backtest with ML model
python main.py

Minimal Python Example

from src.hft import OrderBook, Side, MatchingEngine
from src.hft.strategies import SimpleMarketMaker

# Create order book and matching engine
book = OrderBook(symbol="BTC")
engine = MatchingEngine(book)

# Initialize market maker
mm = SimpleMarketMaker(spread_bps=2.0, max_inventory=10.0)

# Make market at current price
result = mm.make_market(mid_price=50000.0)
print(f"Bid: ${result['bid']:.2f}, Ask: ${result['ask']:.2f}")

πŸ“– Usage

Command Line Interface

# HFT Simulation
python run_hft.py [OPTIONS]

Options:
  --ticks INT       Number of ticks to simulate (default: 1000)
  --strategy STR    Strategy: market_maker, stat_arb, latency_arb (default: market_maker)
  --price FLOAT     Initial price (default: 50000)
  --spread FLOAT    Initial spread in bps (default: 2.0)
  --volatility FLOAT Price volatility (default: 0.001)
  --seed INT        Random seed (default: 42)

Python API Usage

Market Making

from src.hft.strategies.market_maker import MarketMaker, MarketMakerConfig

config = MarketMakerConfig(
    default_spread_bps=3.0,
    max_position=5.0,
    inventory_skew_factor=0.15
)

mm = MarketMaker(config=config)
quote = mm.calculate_quote(mid_price=50000, volatility=0.001)
print(f"Quote: {quote.bid_price:.2f} / {quote.ask_price:.2f}")

Statistical Arbitrage

from src.hft.strategies.stat_arb import StatisticalArbitrage

arb = StatisticalArbitrage()
arb.add_pair("BTC_ETH", "BTC", "ETH")

# Update with prices
signal = arb.update_prices("BTC_ETH", price_1=50000, price_2=3000)
if signal and signal.z_score > 2.0:
    print(f"Arbitrage signal: {signal.signal}")

Order Book Operations

from src.hft.order_book import OrderBook, Order, Side

book = OrderBook(symbol="BTC")

# Add orders
book.add_order(Order(order_id=1, side=Side.BID, price=49990, size=10, timestamp_ns=0))
book.add_order(Order(order_id=2, side=Side.ASK, price=50010, size=10, timestamp_ns=0))

# Get market state
print(f"Mid: ${book.mid_price:.2f}")
print(f"Spread: {book.spread_bps:.1f} bps")
print(f"Depth: {book.get_depth(5)}")

βš™οΈ Configuration

Configuration Files

File Purpose
config/default.yaml Main configuration
config/hft_config.yaml HFT-specific parameters

Environment Variables

# Data Settings
export HFT_SYMBOL="BTCUSDT"
export HFT_INTERVAL="1m"
export HFT_HISTORY_DAYS=30

# Trading Settings
export HFT_INITIAL_CAPITAL=10000
export HFT_FEE_RATE=0.001
export HFT_STRATEGY="market_maker"

# ML Settings
export HFT_MODEL_TYPE="lstm"
export HFT_EPOCHS=50

# Output
export HFT_RESULTS_DIR="./results"
export HFT_LOG_LEVEL="INFO"

Example Configuration (YAML)

# config/hft_config.yaml
market_maker:
  spread_bps: 3.0
  base_size: 0.5
  max_position: 5.0
  inventory_skew_factor: 0.15

stat_arb:
  entry_z_threshold: 2.5
  exit_z_threshold: 0.3
  lookback_periods: 50

risk:
  max_loss_per_trade: 20.0
  daily_loss_limit: 500.0
  position_limit: 5.0

πŸ“š API Documentation

Core Modules

Module Description
src.hft.tick_data Tick processing with nanosecond timestamps
src.hft.order_book L2/L3 order book implementation
src.hft.matching_engine FIFO price-time priority matching
src.hft.latency Network and exchange latency modeling
src.hft.execution Fill simulation with slippage

Strategy Interface

from abc import ABC, abstractmethod

class BaseStrategy(ABC):
    @abstractmethod
    def generate_signal(self, data) -> TradeSignal:
        """Generate trading signal from market data."""
        pass
    
    @abstractmethod
    def get_name(self) -> str:
        """Return strategy name."""
        pass

For detailed API documentation, see docs/HFT_MODULE.md.


πŸ› οΈ Development

Setup Development Environment

# Clone and install dev dependencies
git clone https://github.com/Xyerophyte/hft-simulator.git
cd hft-simulator
pip install -e ".[dev]"

# Install pre-commit hooks
pre-commit install

Code Style

This project uses:

  • Black for code formatting
  • isort for import sorting
  • flake8 for linting
  • mypy for type checking
# Format code
black src/ tests/

# Sort imports
isort src/ tests/

# Run linter
flake8 src/ tests/

# Type check
mypy src/

Project Structure

hft-simulator/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ hft/               # ⚑ HFT Module
β”‚   β”‚   β”œβ”€β”€ tick_data.py
β”‚   β”‚   β”œβ”€β”€ order_book.py
β”‚   β”‚   β”œβ”€β”€ matching_engine.py
β”‚   β”‚   β”œβ”€β”€ latency.py
β”‚   β”‚   β”œβ”€β”€ execution.py
β”‚   β”‚   β”œβ”€β”€ simulator.py
β”‚   β”‚   └── strategies/
β”‚   β”œβ”€β”€ ml/                # Machine Learning
β”‚   β”œβ”€β”€ strategies/        # Trading Strategies
β”‚   β”œβ”€β”€ backtest/          # Backtesting Framework
β”‚   └── analytics/         # Performance Analytics
β”œβ”€β”€ config/                # Configuration Files
β”œβ”€β”€ docs/                  # Documentation
β”œβ”€β”€ tests/                 # Test Suite
β”œβ”€β”€ run_hft.py            # HFT Entry Point
β”œβ”€β”€ main.py               # Main Entry Point
└── requirements.txt

πŸ§ͺ Testing

Run All Tests

# Run test suite
python -m pytest tests/ -v

# Run with coverage
python -m pytest tests/ --cov=src --cov-report=html

# View coverage report
open htmlcov/index.html  # macOS
start htmlcov/index.html # Windows

Run Specific Tests

# Test HFT module
python -m pytest tests/test_hft.py -v

# Test strategies
python -m pytest tests/test_strategies.py -v

# Test with markers
python -m pytest -m "not slow" tests/

πŸš€ Deployment

Production Considerations

  1. Data Source: Configure real-time data feeds (Binance, Polygon, etc.)
  2. Latency: Deploy close to exchange servers for minimum latency
  3. Monitoring: Set up alerts for PnL, position limits, and errors
  4. Logging: Use structured logging with appropriate levels

Docker Deployment

FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .
CMD ["python", "run_hft.py", "--ticks", "10000"]
docker build -t hft-simulator .
docker run -it hft-simulator

πŸ”§ Troubleshooting

Common Issues

Issue Solution
ModuleNotFoundError: No module named 'src' Run from project root or add to PYTHONPATH
torch not found Install PyTorch: pip install torch
sortedcontainers not found Install: pip install sortedcontainers
Slow simulation Reduce tick count or use simpler strategy
Memory issues Process data in chunks, reduce lookback periods

Debug Mode

# Run with verbose logging
export HFT_LOG_LEVEL=DEBUG
python run_hft.py --ticks 100

🀝 Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

How to Contribute

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Code of Conduct

Please read our Code of Conduct before contributing.


πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ‘₯ Authors and Acknowledgments

Author: Xyerophyte

Acknowledgments

  • Inspired by real-world HFT system architectures
  • Built with PyTorch, NumPy, and Pandas
  • Market data from Binance API

πŸ’¬ Support


πŸ“ Changelog

v1.0.0 (2026-01-16)

  • ✨ Added true HFT module with tick-by-tick processing
  • ✨ Implemented L2/L3 order book with FIFO matching
  • ✨ Added market making, stat arb, and latency arb strategies
  • ✨ Integrated latency modeling (Β΅s precision)
  • ✨ Added Transformer and Ensemble ML models
  • πŸ“š Comprehensive documentation

See CHANGELOG.md for full history.


Made with ❀️ for quantitative trading research

About

High-Frequency Trading Simulator - A comprehensive Python framework for simulating HFT strategies, backtesting, and market analysis

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages