OpenHFT-Lab is a comprehensive, high-performance trading system designed for educational and research purposes. It demonstrates advanced concepts in high-frequency trading, including sub-millisecond latency processing, lock-free data structures, and real-time market data handling.
- Sub-millisecond latency: Lock-free ring buffers with memory barriers
- High throughput: 3,500+ market data events per second
- Zero-copy processing: Unsafe memory operations for critical paths
- Real-time order books: Live bid/ask updates with microsecond timestamps
- Concurrent architecture: Producer-consumer patterns with SPSC/MPSC queues
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Market Data βββββΆβ Lock-Free Queue βββββΆβ Order Books β
β (Binance WS) β β (Ring Buffer) β β (L2/L3 Data) β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β
βΌ
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Risk Engine ββββββ HFT Engine βββββΆβ Strategies β
β (Controls) β β (Orchestrator) β β (Market Making) β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Order Gateway β
β (Execution) β
ββββββββββββββββββββ
- .NET 8.0 SDK
- Visual Studio 2022 / VS Code
- Docker (optional, for full stack deployment)
# Clone the repository
git clone https://github.com/your-org/OpenHFT-Lab.git
cd OpenHFT-Lab
# Build the solution
dotnet build --configuration Release
# Run the HFT engine with live market data
dotnet run --project src/OpenHFT.UI --configuration Release
# Run performance benchmarks
dotnet run --project bench/OpenHFT.Benchmarks --configuration Release
# Run unit tests
dotnet test --configuration Release# Build and start all services
docker-compose up -d
# Access services:
# - HFT Engine API: http://localhost:5000
# - Web Dashboard: http://localhost:3000
# - Grafana Monitoring: http://localhost:3001
# - Prometheus Metrics: http://localhost:9090Real-time results from production system:
| Metric | Value | Notes |
|---|---|---|
| Market Data Rate | 3,532 events/sec | Live Binance processing |
| End-to-End Latency | < 10ΞΌs | Market data β Order generation |
| Order Book Updates | < 1ΞΌs | Single price level update |
| Memory Allocation | ~0 | Zero-allocation hot paths |
| CPU Usage | < 5% | Single core, 3 symbols |
| Queue Depth | 0 | Perfect throughput match |
Live Order Books (Sample):
BTCUSDT: $120,517.70@2.837 BTC | $120,494.10@0.054 BTC (18,174 updates/10s)
ETHUSDT: $4,711.49@18.4 ETH | $4,711.50@5.37 ETH (16,633 updates/10s)
ADAUSDT: $0.8809@3,385.5K ADA | $0.8807@162.3K ADA (3,400 updates/10s)
- ποΈ High-Frequency Trading Fundamentals
- β‘ Lock-Free Programming
- π§ Memory Management & Performance
- β±οΈ Latency Optimization
- ποΈ System Architecture
- π Concurrency Model
- π Market Data Processing
- π Order Book Management
- β‘ OpenHFT.Core: Lock-Free Collections & Utilities
- π‘ OpenHFT.Feed: Market Data Adapters
- π OpenHFT.Book: Order Book Engine
- π― OpenHFT.Strategy: Trading Strategies
- ποΈ OpenHFT.UI: Main Engine & API
- Lock-Free Ring Buffers: SPSC/MPSC queues with memory barriers
- Unsafe Memory Operations: Zero-copy processing for critical paths
- Microsecond Timestamps: High-resolution timing throughout
- Branch Prediction: Optimized hot paths with likely/unlikely hints
- NUMA Awareness: Thread affinity and memory locality optimization
- Binance WebSocket Integration: Live futures market data
- Gap Detection: Sequence number validation and recovery
- Multiple Asset Support: Concurrent processing of multiple symbols
- Data Normalization: Unified market data events across exchanges
- L2/L3 Market Data: Price levels and individual orders
- Order Flow Imbalance: Real-time microstructure analytics
- Best Bid/Ask Tracking: Sub-microsecond updates
- Historical Snapshots: Point-in-time order book reconstruction
- Market Making: Adaptive spread and inventory management
- Statistical Arbitrage: Mean reversion and momentum strategies
- Risk Management: Real-time position and exposure monitoring
- Strategy Framework: Pluggable architecture for custom strategies
// Create high-performance ring buffer
var marketDataQueue = new LockFreeRingBuffer<MarketDataEvent>(65536);
// Connect to Binance WebSocket
var binanceAdapter = new BinanceAdapter(logger);
await binanceAdapter.ConnectAsync();
// Process events at microsecond precision
while (marketDataQueue.TryRead(out var marketEvent))
{
var orderBook = orderBooks[marketEvent.SymbolId];
orderBook.ApplyEvent(marketEvent);
// Generate trading signals
var orders = strategy.OnMarketData(marketEvent, orderBook);
foreach (var order in orders)
{
await gateway.SendOrder(order);
}
}// Lock-free SPSC ring buffer - single producer, single consumer
public sealed class LockFreeRingBuffer<T> where T : struct
{
private readonly T[] _buffer;
private readonly int _bufferMask;
private volatile long _writeSequence;
private volatile long _readSequence;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryWrite(T item)
{
var writeSeq = Volatile.Read(ref _writeSequence);
var wrapPoint = writeSeq - _buffer.Length;
var cachedGatingSequence = Volatile.Read(ref _readSequence);
if (wrapPoint > cachedGatingSequence)
return false; // Buffer full
_buffer[writeSeq & _bufferMask] = item;
Volatile.Write(ref _writeSequence, writeSeq + 1);
return true;
}
}public override IEnumerable<OrderIntent> OnMarketData(
MarketDataEvent marketData, OrderBook orderBook)
{
var (bidPrice, askPrice) = CalculateQuotes(orderBook);
var position = GetPosition(marketData.Symbol);
// Adaptive spread based on inventory and volatility
var spread = CalculateAdaptiveSpread(orderBook, position);
var inventorySkew = CalculateInventoryAdjustment(position);
return new[]
{
new OrderIntent(
clientOrderId: GenerateOrderId(),
side: Side.Buy,
priceTicks: bidPrice - inventorySkew,
quantity: CalculateOrderSize(Side.Buy, position),
timestampIn: TimestampUtils.GetTimestampMicros()
)
};
}OpenHFT-Lab/
βββ src/
β βββ OpenHFT.Core/ # Lock-free collections, utilities
β β βββ Collections/ # Ring buffers, concurrent data structures
β β βββ Models/ # Market data events, order models
β β βββ Utils/ # Timestamp, price, symbol utilities
β βββ OpenHFT.Feed/ # Market data adapters
β β βββ Adapters/ # Binance, simulation adapters
β β βββ Interfaces/ # Feed handler contracts
β βββ OpenHFT.Book/ # Order book engine
β β βββ Core/ # Order book implementation
β β βββ Models/ # Price levels, book sides
β βββ OpenHFT.Strategy/ # Trading strategies
β β βββ Strategies/ # Market making, arbitrage
β β βββ Interfaces/ # Strategy framework
β βββ OpenHFT.UI/ # Main engine and API
β βββ Services/ # HFT engine orchestrator
β βββ Controllers/ # REST API endpoints
βββ tests/
β βββ OpenHFT.Tests/ # Comprehensive unit tests
βββ bench/
β βββ OpenHFT.Benchmarks/ # Performance benchmarking
βββ docs/ # Documentation
βββ docker/ # Container deployment
β οΈ IMPORTANT DISCLAIMERThis software is designed for educational and research purposes only. It is not intended for production trading without proper:
- Risk management systems
- Regulatory compliance
- Professional oversight
- Comprehensive testing
Trading financial instruments involves substantial risk of loss. Use at your own risk.
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Run tests:
dotnet test - Run benchmarks:
dotnet run --project bench/OpenHFT.Benchmarks - Submit a pull request
- Follow C# coding conventions
- Write comprehensive unit tests
- Document performance-critical code
- Use XML documentation for public APIs
- Profile performance changes with BenchmarkDotNet
This project is licensed under the MIT License - see the LICENSE file for details.
- Binance for providing free WebSocket market data APIs
- .NET Team for the high-performance runtime
- LMAX Disruptor for lock-free programming inspiration
- Financial Markets community for HFT knowledge sharing
- π Documentation
- π Issue Tracker
- π¬ Discussions
- π§ Email: support@openhft-lab.com
Built with β€οΈ for the quantitative finance and high-performance computing communities