Skip to content

JMasson0706/HFT-Simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

High Frequency Trading (HFT) Simulator

A C++ implementation of a high-frequency trading simulator that models order book mechanics, order matching, and trade execution with optional latency simulation.

Project Overview

This simulator provides a realistic environment for testing trading strategies and understanding market microstructure. It implements:

  • Order Book Management: Maintains separate bid and ask books with price-time priority
  • Order Matching Engine: Matches orders according to standard exchange rules
  • Trade Execution: Generates and logs completed trades
  • Latency Simulation: Optional delay simulation for realistic HFT scenarios
  • Market Data: Real-time best bid/ask prices and sizes

Order Matching Flow

  1. Order Submission: User/agent submits order to exchange
  2. Event Queue: Order enters event queue (if latency enabled)
  3. Exchange Processing: Exchange processes order from queue
  4. Order Book Matching: OrderBook attempts to match order against opposite side
  5. Trade Generation: Matched orders generate trades
  6. Book Update: Remaining order quantity added to book

Building and Running

Prerequisites

  • C++17 or later
  • CMake (optional, for build system)

Compilation

cd Code-Projects/HFT
g++ -std=c++17 -O2 -o hft_simulator src/*.cpp

Running

./hft_simulator

Usage Examples

Basic Usage

#include "ExchangeSimulator.hpp"

int main() {
    ExchangeSimulator exchange;
    
    // Submit limit orders to build the book
    exchange.submitOrder(100.50, 100, Side::BUY, OrderType::LIMIT, 1);
    exchange.submitOrder(101.00, 100, Side::SELL, OrderType::LIMIT, 2);
    
    // Submit a market order that will match
    exchange.submitOrder(0.0, 50, Side::BUY, OrderType::MARKET, 3);
    
    // Print results
    exchange.printOrderBook();
    exchange.printTradeLog();
    exchange.printStatistics();
    
    return 0;
}

Latency Simulation

ExchangeSimulator exchange(true); // Enable latency

// Set custom latency generator (1-10ms delay)
exchange.setLatencyGenerator([]() {
    return std::rand() % 9000 + 1000; // 1-10ms in microseconds
});

// Submit orders (they go to queue)
exchange.submitOrder(100.50, 100, Side::BUY, OrderType::LIMIT, 1);
exchange.submitOrder(101.00, 100, Side::SELL, OrderType::LIMIT, 2);

// Process the queue
exchange.processEventQueue();

Features

Order Types

  • Limit Orders: Orders with specific price limits
  • Market Orders: Orders that execute at best available price

Order Matching

  • Price-Time Priority: Orders matched by price first, then time
  • Partial Fills: Orders can be partially filled
  • Market Orders: Execute immediately against best available price

Market Data

  • Best Bid/Ask: Current best bid and ask prices
  • Market Depth: Size at best bid/ask levels
  • Order Book Display: Full order book visualization

Statistics and Logging

  • Trade Log: Complete history of all executed trades
  • Performance Metrics: Orders processed, trades executed, total volume
  • Real-time Statistics: Live market statistics

Testing

The simulator includes comprehensive tests:

  1. Basic Test: Simple order submission and matching
  2. Stress Test: 1000 random orders to test performance
  3. Latency Test: Order processing with simulated delays

Run tests with:

./hft_simulator

Performance

  • Order Processing: ~1-10 microseconds per order (depending on book depth)
  • Memory Usage: Efficient data structures with minimal overhead
  • Scalability: Designed to handle high-frequency order flows

Future Enhancements

Planned Features

  • Multiple instruments/symbols
  • Advanced order types (stop-loss, iceberg, etc.)
  • Market maker algorithms
  • Risk management systems
  • Network latency simulation
  • Historical data replay
  • WebSocket API for real-time data
  • Configuration file support

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Implement your changes
  4. Add tests for new functionality
  5. Submit a pull request

License

This project is open source and available under the MIT License.

About

A C++ implementation of a high-frequency trading simulator that models order book mechanics, order matching, and trade execution with optional latency simulation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors