Skip to content

algonomad571/TradeCore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mini Backtester & Algorithmic Trading Engine

A high-performance C++ trading engine that simulates algorithmic trading strategies using historical market data. The engine processes orders efficiently, maintains realistic market behavior, and reports comprehensive performance metrics.

Features

Core Components

  • High-Performance Order Book: Cache-friendly limit order book with time and price priority
  • Strategy Framework: Pluggable architecture for multiple trading strategies
  • Market Data Replay: Historical tick and bar data processing
  • Performance Metrics: Comprehensive PnL, drawdown, and risk analysis
  • Low-Latency Design: Optimized for speed with minimal memory allocations

Implemented Strategies

  1. SMA Crossover Strategy: Simple Moving Average crossover signals
  2. VWAP Execution Strategy: Volume-Weighted Average Price execution

Performance Features

  • Microsecond-level timing and benchmarking
  • Cache-friendly data structures (std::vector preferred over std::list)
  • Efficient order matching algorithm
  • Real-time performance monitoring (ticks/sec, orders/sec)

Getting Started

Prerequisites

  • Modern C++ compiler (C++17 or newer)
  • CMake 3.16 or higher

Building the Project

# Clone or download the project
mkdir build && cd build
cmake ..
make

# Run the backtester
./backtester

# Run tests
./tests

Project Structure

MiniBacktester/
├── src/                    # Source files
│   ├── main.cpp           # Main application
│   ├── Engine.cpp         # Trading engine implementation
│   ├── OrderBook.cpp      # Limit order book
│   ├── Strategy.cpp       # Base strategy class
│   ├── SMA.cpp           # SMA crossover strategy
│   ├── VWAP.cpp          # VWAP execution strategy
│   ├── CSVReader.cpp     # Market data reader
│   └── PerformanceMetrics.cpp
├── include/               # Header files
├── data/                 # Historical market data
│   └── sample_data.csv   # Sample tick data
├── tests/                # Unit tests
└── CMakeLists.txt        # Build configuration

Usage

Basic Usage

# Run with default sample data
./backtester

# Run with custom data file
./backtester path/to/your/data.csv

CSV Data Format

The engine expects CSV files with the following format:

timestamp,symbol,price,volume
1640995200000000,AAPL,150.00,500
1640995200001000,AAPL,150.05,300

Where:

  • timestamp: Microseconds since epoch
  • symbol: Trading symbol
  • price: Price per share
  • volume: Number of shares

Example Output

Starting Mini Backtester & Trading Engine
========================================

Loading historical data from: data/sample_data.csv
Loaded 10000 ticks

Processing market data...
[SMA_AAPL_10_30] Bullish crossover detected. Buying 100 shares at 150.25
[VWAP_AAPL_500] Starting VWAP execution at 150.30

Backtest completed in 45 ms
Processing rate: 222,222 ticks/second

=== Trading Engine Summary ===
Total Trades: 15
Total PnL: $245.50
Active Positions: 1
Active Strategies: 2
Processing Rate: 222,222 ticks/second

=== Performance Metrics ===
Total PnL: $245.50
Max Drawdown: $12.30
Win Rate: 73.33%
Sharpe Ratio: 1.45

Architecture

Order Book Design

  • Price Levels: Sorted vectors for cache efficiency
  • Time Priority: FIFO within price levels
  • Fast Matching: O(1) best bid/ask access
  • Memory Efficient: Minimal dynamic allocations

Strategy Framework

class Strategy {
public:
    virtual void onTick(const Tick& tick) = 0;
    virtual void onTrade(const Trade& trade) = 0;
    virtual void onOrderUpdate(const Order& order) = 0;
};

Performance Optimizations

  • Cache-friendly data structures
  • Pre-allocated vectors where possible
  • Minimal string operations in hot paths
  • Efficient order lookup with hash maps
  • RAII for automatic resource management

Performance Benchmarks

Typical performance on modern hardware:

  • Order Processing: 500,000+ orders/second
  • Tick Processing: 200,000+ ticks/second
  • Memory Usage: <100MB for 1M orders
  • Latency: <1μs per order match

Advanced Features

Risk Management

  • Position tracking per symbol
  • Realized and unrealized PnL calculation
  • Maximum drawdown monitoring
  • Configurable position limits

Reporting

  • Trade export to CSV
  • Equity curve generation
  • Performance metrics calculation
  • Real-time statistics

Extensibility

  • Plugin architecture for new strategies
  • Configurable order types
  • Multi-symbol support
  • Custom performance metrics

Testing

Run the comprehensive test suite:

./tests

Tests cover:

  • Order book functionality
  • Strategy execution
  • Performance metrics
  • Edge cases and error handling
  • Performance benchmarks

Future Enhancements

  • Multi-threading support
  • Real-time market data feeds
  • Advanced order types (stop-loss, iceberg)
  • Machine learning strategy integration
  • Web-based dashboard
  • Database persistence
  • Risk management modules

Contributing

This project demonstrates core concepts for algorithmic trading systems and is designed for educational and portfolio purposes. It showcases:

  • Modern C++ best practices
  • High-performance computing techniques
  • Financial market microstructure
  • Quantitative trading concepts
  • Software architecture patterns

Perfect for demonstrating skills to quantitative trading firms, prop trading companies, and high-frequency trading organizations.

License

This project is provided as-is for educational and demonstration purposes.

About

Mini Backtester / Trading Engine in C++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published