Skip to content

amank-23/cpp-trading-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Real-Time Trading System

A complete, multi-threaded real-time trading system built in C++

License: MIT C++ CMake Build Status Platform

View Demo · Documentation · Report Bug · Request Feature


Table of Contents


Overview

A high-performance, enterprise-grade trading system implementing core financial market infrastructure components. This system demonstrates advanced C++ programming techniques, real-time data processing, and financial software architecture patterns used in production trading environments.

Key Highlights

  • Multi-threaded Architecture: Concurrent order processing and GUI updates
  • Real-time GUI: Live ImGui dashboard with order book visualization
  • Risk Management: Pre-trade validation and position tracking
  • WebSocket Integration: Real-time market data handling
  • Cross-platform: Linux, Windows, and macOS support

Features

Core Trading Engine

  • Order Book: Price-time priority matching
  • Order Types: Limit orders with market order support
  • Trade Execution: Real-time matching engine
  • Order Management: Add, modify, cancel operations

Market Data

  • WebSocket Client: Real-time data ingestion
  • JSON Processing: High-performance parsing
  • Message Queue: Thread-safe producer-consumer
  • Data Simulation: Built-in market data simulator

Risk Management

  • Position Tracking: Real-time portfolio monitoring
  • Pre-trade Checks: Risk validation before execution
  • Exposure Limits: Configurable position limits
  • P&L Calculation: Real-time profit/loss tracking

Live Dashboard

  • Order Book Display: Real-time bid/ask visualization
  • Portfolio Panel: Position and P&L monitoring
  • Trade History: Execution log with full details
  • Risk Metrics: Live risk monitoring display

Demo

Live Trading System in Action

View Demo Video

30-second demo showing real-time order matching, GUI updates, and risk management

Screenshots

Real-Time Dashboard

Dashboard Overview Complete trading dashboard with order book, portfolio, and trade history

Order Book
Order Book
Live bid/ask display with price-time priority
Portfolio
Portfolio
Real-time position and P&L tracking
Trade History
Trade History
Execution log with full trade details

Architecture

graph TB
    A[WebSocket Market Data] --> B[Order Processing Thread]
    B --> C[Risk Engine]
    C --> D[Order Book Engine]
    D --> E[Trade Execution]
    E --> F[Position Update]
    F --> G[GUI Dashboard]
    
    D --> H[Order Book Display]
    F --> I[Portfolio Panel]  
    E --> J[Trade History]
    
    H --> G
    I --> G
    J --> G
    
    style A fill:#e1f5fe
    style D fill:#f3e5f5
    style G fill:#e8f5e8
Loading

Component Overview

Component Description Key Features
Order Book Core matching engine Price-time priority, O(log n) operations
WebSocket Client Market data handler Async I/O, message queuing
Risk Engine Risk management Pre-trade validation, position tracking
Dashboard GUI visualization Real-time updates, ImGui framework

Installation

Prerequisites

System Requirements

  • OS: Linux (Ubuntu 20.04+), Windows 10+, macOS 10.15+
  • Compiler: GCC 9+, Clang 10+, MSVC 2019+
  • CMake: 3.16 or higher
  • Memory: 4GB RAM minimum
  • Storage: 1GB free space

Dependencies

  • Boost: System utilities and threading
  • websocketpp: WebSocket client library
  • nlohmann/json: JSON parsing
  • Google Test: Unit testing framework
  • ImGui: GUI framework
  • GLFW + OpenGL: Graphics rendering

Quick Start

# 1. Clone the repository
git clone https://github.com/amank-23/cpp-trading-engine.git
cd cpp-trading-engine

# 2. Initialize submodules
git submodule update --init --recursive

# 3. Install dependencies (Ubuntu/Debian)
sudo apt update
sudo apt install -y build-essential cmake pkg-config
sudo apt install -y libboost-system-dev nlohmann-json3-dev libwebsocketpp-dev
sudo apt install -y libgtest-dev libglfw3-dev libgl1-mesa-dev
sudo apt install -y libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev

# 4. Build the project
mkdir build && cd build
cmake ..
make -j$(nproc)

# 5. Run the system
./TradingSystem

Alternative Installation Methods

🐳 Docker Installation
# Build Docker image
docker build -t trading-system .

# Run with GUI support (Linux)
docker run -it --rm \
  -e DISPLAY=$DISPLAY \
  -v /tmp/.X11-unix:/tmp/.X11-unix \
  trading-system
📦 Package Manager Installation
# Using vcpkg
vcpkg install boost nlohmann-json websocketpp gtest glfw3

# Using Conan
conan install . --install-folder=build --build=missing

Usage

Running Different Components

# Full system with GUI
./TradingSystem

# Backend-only test (no GUI required)
./BackendTest

# Unit tests
./RunTests

# Interactive launcher
../run.sh

Configuration

⚙️ System Configuration
// Risk management settings
RiskEngine risk_engine(80.0);  // Max position size

// WebSocket connection
ws_client->connect("ws://your-market-data-feed.com");

// GUI settings
Dashboard dashboard(*order_book, *risk_engine);

API Usage Example

#include "order_book/OrderBook.h"
#include "risk/RiskEngine.h"

// Create components
auto order_book = std::make_shared<OrderBook>();
auto risk_engine = std::make_shared<RiskEngine>(100.0);

// Set up trade callback
order_book->on_trade([](const Trade& trade) {
    std::cout << "Trade executed: " << trade.price 
              << " x " << trade.quantity << std::endl;
});

// Create and add order
auto order = std::make_shared<Order>(
    1, "BTC-USD", OrderType::LIMIT, 
    OrderSide::BUY, 50000.0, 1
);

if (risk_engine->check_pre_trade_risk(*order)) {
    order_book->add_order(order);
}

Testing

Running Tests

# Run unit tests
make test

# Or run directly
./RunTests

The project includes unit tests for the OrderBook component using Google Test framework.


License

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

MIT License

Copyright (c) 2025 amank-23

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

About

Real-time C++ trading system with ImGui dashboard

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published