Low-Latency Order Book Engine
Sep 2025 - Dec 2025
Minimal, high-performance order book engine in C++ designed to explore the foundations of low-latency trading systems. Includes nanosecond-level order matcher skeleton, lock-free queues, thread-local memory pools, and automated benchmarking on AWS EC2 spot instances.
Architecture
Order Matching Core:
- Minimal memory allocation design for sub-microsecond latency
- Inlined matching logic for CPU cache efficiency
- Lock-free queue skeleton for order ingestion
- Thread-local memory pools to reduce contention
Benchmarking Pipeline:
- Simulates 1M+ orders with alternating buy/sell sides
- Measures average microseconds per match
- Reproducible results on local machine or EC2
Separation of Concerns:
- Order ingestion (queue) → matching engine → latency measurement
- Decoupled architecture mirrors real exchange pipelines
Local Development
Build & Run:
cd orderbook-engine
chmod +x scripts/build_and_run_benchmark.sh
./scripts/build_and_run_benchmark.sh
This script:
- Cleans and creates fresh build directory
- Configures with CMake and builds with parallel jobs
- Runs validation executable
- Executes latency benchmark →
build/benchmark_results.txt
Unit Tests:
chmod +x scripts/run_tests.sh
./scripts/run_tests.sh
Test coverage:
- Order creation, type validation, comparison operations
- Order addition, matching, stress testing (1000 orders)
- Lock-free queue push/pop operations
- Thread-local memory pool allocation (2000 stress allocations)
Cloud Benchmarking
Automated EC2 Spot Execution:
chmod +x scripts/run_benchmark.sh
./scripts/run_benchmark.sh
Process:
- Launches EC2 spot instance with specified AMI/instance type
- Cloud-init installs dependencies (build-essential, cmake, git, perf)
- Clones repository and executes benchmark
- Logs results to
/home/ubuntu/benchmark_results.txt - Automatically shuts down instance after completion
Benefits: Repeatable benchmarking at low cost with cloud infrastructure consistency.
Design Insights
- Sub-microsecond latency goal: Minimal allocations, cache-aware layouts, inlined hot paths
- Lock-free architecture: Eliminates mutex contention in order ingestion pipeline
- EC2 spot automation: Reduces operational overhead while maintaining reproducibility
- Optimization roadmap:
- SIMD vectorization for price matching
- NUMA-aware memory allocation for multi-threaded workloads
- Order cancellations and partial fill simulation
- Latency spike detection and metrics logging
Intelligent Stock Screener
Oct 2025 - Dec 2025
Fully automated stock screening system scanning 3,800+ US stocks daily using Mark Minervini’s Trend Template methodology. Identifies high-conviction buy/sell signals via phase-based trend classification, relative strength momentum, and fundamental quality filters.
Core Features
Phase-Based Trend Classification:
- Phase 1 (Base): Consolidation after decline
- Phase 2 (Uptrend): Confirmed uptrend with 50>150>200 SMA alignment — BUY ZONE
- Phase 3 (Distribution): Topping pattern with weakening momentum
- Phase 4 (Downtrend): Declining trend — AVOID
Volatility Contraction Pattern (VCP):
- Identifies price consolidation before explosive breakout
- Formula: Range_i = ((High_i - Low_i) / Close_i) × 100
- VCP_Score = (∑ Contractions / Total_Periods) × Trend_Quality
- Example: 13 contractions (6.5% → 5.8% → 7.6% → 6.0%) with 54/100 quality score indicates imminent breakout
Intelligent Market Regime Filtering:
- Only generates buy signals when SPY in Phase 1/2 with ≥15% of stocks in Phase 2
- Avoids low-probability trades in declining markets
- Sell signals always generated (can exit any market)
Smart Caching Strategy:
- Fundamentals stored in Git repository as JSON with metadata
- Earnings season (6 weeks): refresh if >7 days old
- Normal periods: refresh if >90 days old
- Result: 74% fewer API calls, 15-20 min faster scans
Sample Output
📊 View Sample Scan Output (Jan 9, 2026)
OPTIMIZED FULL MARKET SCAN - ALL US STOCKS
Scan Date: 2026-01-09
Generated: 2026-01-09 14:04:49
SCANNING STATISTICS
Total Universe: 3,819 stocks
Analyzed: 1,188 stocks
Buy Signals: 370 | Sell Signals: 111
Error Rate: 0.26%
SPY Trend: Phase 2 - Uptrend (Bullish)
• 50 SMA: $678.39 (slope: 0.0563)
• 200 SMA: $626.72 (slope: 0.0982)
• Confidence: 85%
Market Breadth (n=1,188):
• Phase 1 (Base): 560 (47.1%)
• Phase 2 (Uptrend): 504 (42.4%)
• Phase 3 (Distribution): 1 (0.1%)
• Phase 4 (Downtrend): 123 (10.4%)
Market Regime: RISK-ON (Strong)
⭐ BUY #1: WWD | Score: 104.5/110
Phase: 2 | Entry Quality: Good | R:R: 3.2:1
Stop Loss: $299.25
• 11.1% above 50 SMA (strong breakout)
• Revenue: +16.5% YoY | EPS: +64% YoY
• Volume confirmation (1.17 ratio)
Covariance-Based Macro Trading System
Sep 2023 - Present
Systematic S&P 500 futures trading system powered by eigendecomposition of economic indicator covariance matrices. Decomposes 8×8 macro factor covariance to identify uncertainty regimes and size positions via macro surprise exposure.
Core Hypothesis
Markets respond to unexpected economic information, not levels. Raw covariance is misleading—it captures expected relationships.
Standard assumption: E[r_t | X_t] = α + βX_t
Reality: Markets react to surprises: ΔX_t - E_t[ΔX_t]
Solution: Decompose surprise covariance via PCA to identify 3-4 interpretable macro factors (growth, inflation, policy, volatility) and measure ES exposure across regimes.
5-Stage Architecture
- Data Acquisition: FRED API (CPI, GDP, Unemployment, Sentiment, Fed Funds, Treasury yields) + VIX
- Temporal Alignment: Daily → month-end LOCF, Quarterly → Hermite interpolation to monthly
- Covariance Estimation: Σ_ij = (1/(n-1)) ∑_t (X_it - X̄_i)(X_jt - X̄_j)
- Spectral Decomposition: Σ = UΛUᵀ → First 3-4 eigenvalues capture 85-92% variance
- Regime Quantification: Frobenius norm ‖Σ‖_F = √(∑_i,j σ²_ij) maps to regime state
Regime Classification:
- Stable (‖Σ‖_F < μ - σ): Low uncertainty, tight co-movement
- Neutral (μ - σ ≤ ‖Σ‖_F ≤ μ + σ): Moderate uncertainty
- Elevated (‖Σ‖_F > μ + σ): High uncertainty, factor decoupling
Infrastructure
AWS Stack:
- EventBridge (cron: daily 12:00 UTC) → Lambda (512MB, 600s) → S3 (versioned, encrypted)
- Lambda execution: ~4.2s average (p95: 8.7s)
- Native C++ binary (662 KB) with Eigen, AWS SDK, curl
- IaC: AWS CDK 2.x (TypeScript, dev/staging/prod)
Validation:
- 180+ unit tests: covariance symmetry, eigenvalue ordering, numerical stability
- 5+ year backtests: 73% alignment with NBER recession dates
Data Inputs
8 economic indicators at mixed frequencies:
- CPI (monthly) - Inflation anchor
- Real GDP (quarterly) - Earnings driver
- Unemployment (monthly) - Labor tightness
- Consumer Sentiment (monthly) - Forward demand
- Fed Funds (monthly) - Discount rate
- 10Y/2Y Treasury (daily) - Yield curve
- VIX (daily) - Realized volatility