A-Share Multi-Factor Quant Platform

A 股多因子量化研究 + 交易平台。从数据到回测到实盘的完整流水线。 面向量化开发面试,展示机构级架构设计能力。

691
Unit Tests
84
Python Modules
27K+
Lines of Python
91
REST API Endpoints
15
Alpha Factors

System Architecture

Event-driven architecture — all components communicate through EventBus, all state persisted in SQLite.

                         ┌─────────────────────────────────────────────────────────────┐
                         │              Core Architecture (core/)                      │
                         │   AsyncEventBus · Store · StateMachine · AuditLog           │
                         │   Scheduler · MessageBus(Local/Redis/Kafka) · Services      │
                         └────────────────────────────┬────────────────────────────────┘
                                                      │ All components communicate via EventBusAll state persisted via Store (SQLite WAL)
     ┌────────────────────────────────────────────────┼────────────────────────────────────────────┐
     │                                                │                                            │
     ▼                                                ▼                                            ▼
Data Layer           Factor Engine             Alpha Model              Portfolio Optimizer
 Synthetic               10 Technical                IC/ICIR Weighted           Equal Weight
 Tushare                 5 Fundamental               ML Signal (XGBoost/LGBM)   Mean Variance
 Baostock                Cython Hot Paths            LLM Sentiment              Risk Parity
 PostgreSQL              IC Monitor + Decay
 WebSocket L2            Graph Network Factor
 Realtime Fundamentals
     │                        │                           │                            │
     └────────────────────────┴───────────────────────────┴────────────────────────────┘
                                                          │
                                                          ▼
     ┌────────────────────────────────────────────────────────────────────────────────┐
     │                              Execution & Risk Layer                             │
     │                                                                                │
     │  Order Book          Backtest Engine         Risk Engine          Execution   │
     │  Red-Black Tree       Tick-Level Event        Per-tick Greeks          TWAP      │
     │  Price-Time FIFO      Market Impact           Pre-Trade Check          VWAP      │
     │  IOC/FOK/Partial      TWAP/VWAP Algo          Auto Delta-Hedge        Iceberg   │
     │  VPIN Metrics         3 Impact Models          Kill Switch             SmartRouter│
     │                                        12 Stress Scenarios                        │
     └────────────────────────────────────────────────────────────────────────────────┘
                                                          │
                                                          ▼
     ┌────────────────────────────────────────────────────────────────────────────────┐
     │                            Live Trading & Monitoring                             │
     │                                                                                │
     │   Trading Engine          Web Dashboard           Monitoring                    │
     │   AKShare Realtime        FastAPI (91 endpoints)  Prometheus + Grafana          │
     │   Paper Trading           Vue 3 + ECharts         16 Dashboard Panels           │
     │   QMT/xtquant Live        WebSocket Push          Structured Logging            │
     └────────────────────────────────────────────────────────────────────────────────┘
    

Core Modules

17 new source files (~5,000 lines) + 132 new tests — all integrated into the existing system.

AsyncEventBus v2
core/event_bus_v2.py
High-performance async event bus with per-handler queues, backpressure, and WAL event sourcing.
  • asyncio.Queue per handler — no cross-blocking
  • Backpressure: queue full → publisher waits, never drops
  • P50/P99/P999 latency histogram per handler
  • Dead letter queue with exponential backoff retry
  • Write-ahead log (WAL) for crash recovery
  • Auto-detect sync/async handlers
📊
Real Order Book (LOB)
execution/order_book.py
Realistic limit order book with price-time priority matching, replacing naive simulated exchange.
  • Sorted bid/ask price levels (red-black tree pattern)
  • FIFO queue at each level (price-time priority)
  • IOC / FOK / partial fill support
  • L1/L2/L3 market data snapshots
  • VPIN microstructure metrics
  • Spread, depth imbalance analytics
💥
Market Impact Models
execution/market_impact.py
Three classic market impact models with weighted ensemble for execution cost estimation.
  • Almgren-Chriss: temporary + permanent impact
  • Square-Root: industry standard (impact ∝ √(Q/V))
  • Kyle's Lambda: informed trading model
  • CompositeImpactModel: weighted ensemble
  • ExecutionCostCalculator: impact + commission + tax
⏱️
Tick-Level Backtester
backtest/tick_engine.py
Event-driven tick replay with real LOB matching and market impact simulation.
  • Event-driven tick replay (not vectorized)
  • Real order book matching per tick
  • Market impact simulation (3 models)
  • TWAP / VWAP execution algorithms
  • Per-tick risk checks
📐
Options Greeks
risk/greeks.py
Black-Scholes full Greeks calculator with portfolio aggregation and auto delta-hedge.
  • Black-Scholes: Delta/Gamma/Vega/Theta/Rho
  • math.erf for N() — no scipy dependency
  • Portfolio Greeks aggregation
  • Dollar Greeks (delta/gamma in $)
  • Auto delta-hedge order generation
🛡️
Real-Time Risk Engine
risk/realtime_engine.py
Per-tick risk monitoring with pre-trade checks, auto-hedge, and kill switch.
  • Pre-trade check: simulate fill before sending
  • Multi-dimensional limits (7 types)
  • 5-level risk escalation: GREEN → KILL
  • Auto delta-hedge when threshold exceeded
  • 12 stress test scenarios
  • Kill switch: emergency flatten all
⚙️
Cython Hot Paths
utils/cyext/
4 compute-intensive functions accelerated with Cython, with pure Python fallback.
  • rolling_momentum: log returns with typed memoryviews
  • rolling_volatility: Welford single-pass algorithm
  • rank_ic: Spearman Rank IC with C-level sort
  • zscore_cross_section: online normalization
  • Auto-detect: Cython → Python fallback
🔗
Distributed Message Bus
core/message_bus.py
Pluggable message bus abstraction with Local, Redis, and Kafka backends.
  • MessageBus ABC with health_check()
  • LocalBus: asyncio.Queue (dev/test)
  • RedisBus: Pub/Sub with pattern matching
  • KafkaBus: aiokafka with consumer groups
  • ServiceRegistry: discover + heartbeat

Data Flow

One complete pipeline execution: python main.py run

1

Data Loading

DataProviderDataPipeline (clean/align/filter ST/suspended) → prices, returns, benchmark, metadata, financials. Supports Synthetic / Tushare / Baostock / PostgreSQL.

2

Factor Computation

FactorEngine computes 15 factors → cross-sectional processing (winsorize → zscore → neutralize) → IC evaluation. Cython hot paths for performance.

3

Alpha Signal

AlphaPipeline combines factors (ICIR-weighted) → cross-sectional rank normalization → signal. Optional: ML signal (XGBoost/LGBM) or LLM sentiment.

4

Portfolio Optimization

Covariance estimation (Ledoit-Wolf/EWMA) → PortfolioOptimizer (EW/MVO/RiskParity) → target weights with constraints (long-only, max weight, sector, turnover, lot size).

5

Backtest

BacktestEngine multi-period simulation → daily weight drift → rebalance with costs (commission 0.03% + stamp tax 0.1% + slippage). Optional: tick-level engine with LOB matching + market impact.

6

Risk Analysis & Report

VaR/CVaR → Stress tests (2008/2015/2020) → Barra decomposition → Regime detection → HTML report with ECharts + KPI + factor analysis + risk metrics.

vs Jane Street

Architecture patterns are aligned. Remaining gaps are physical limits, not code.

Dimension This Project Jane Street Gap
Event Bus AsyncEventBus (backpressure + DLQ + WAL) Similar architecture Matched
Order Book Red-black tree + FIFO + IOC/FOK + VPIN Similar architecture Matched
Backtest Tick-level event-driven + market impact Tick-level event-driven Matched
Risk Engine Per-tick Greeks + pre-trade + Kill Switch Per-tick pre-trade + auto-hedge Matched
Factor Compute Cython hot paths C++/Rust Close
Latency ~10ms (Python) ~10μs (OCaml + FPGA + DPDK) 1000x
Type Safety Runtime (Python) Compile-time (OCaml) Fundamental
Data Level 2 (10-level book) Level 3 (all orders/cancels) Resource
Strategy Monthly multi-factor stock selection Microsecond market-making + arb Category

Quick Start

Get up and running in 3 commands.

Terminal
# 1. Clone the repo $ git clone https://github.com/sijie-Z/quant-platform.git $ cd quant-platform # 2. Install dependencies $ pip install -r requirements.txt # 3. Run full pipeline (synthetic data, ~3 minutes) $ python main.py run --force INFO | [1/6] Loading data... (500 stocks, 5 years) INFO | [2/6] Computing factors... (15 factors) INFO | [3/6] Generating alpha signal... INFO | [4/6] Portfolio optimization... INFO | [5/6] Running backtest... INFO | [6/6] Generating report... INFO | Results saved to: ./results/ # 4. Run tests (610 tests) $ pytest tests/ -v 610 passed in 217s # 5. Start web dashboard $ python main.py web FastAPI + Vue 3 → http://localhost:8000

More Commands

CLI Reference
# Baostock real data (free, no API key)
python main.py run --use-baostock

# Strategy comparison
python main.py compare --optimizers equal_weight,mean_variance,risk_parity

# Parameter grid search
python main.py sweep --optimizers equal_weight,mean_variance --frequencies monthly,weekly

# ML Alpha signal
python main.py ml train --model lightgbm
python main.py ml signal --model xgboost

# LLM research agent
python main.py research report

# Performance profiling
python main.py profile

Tech Stack

Production-grade technologies across the full stack.

Python 3.10+
Core language
asyncio
Async event bus
Cython
Hot path acceleration
Numba
6 JIT kernels
Pandas / NumPy
Data processing
cvxpy
Portfolio optimization
XGBoost / LightGBM
ML alpha signals
FastAPI
REST API (91 endpoints)
Vue 3 + ECharts
Bloomberg-style dashboard
SQLite WAL
State persistence
PostgreSQL
Production storage
Redis / Kafka
Message bus backends
Prometheus + Grafana
Monitoring (16 panels)
Docker
Container deployment
GitHub Actions
CI/CD (Py 3.10/3.11/3.12)
AKShare
Real-time A-share data

A-Share Pitfalls Handled

10 real-world A-share traps that every quant must handle. We handle all of them.

1. Forward Adjustment
Tushare qfq; synthetic data generates adj_factor for price continuity across dividends.
2. Suspension
Short suspension (≤30d) forward-fill; long suspension removed from universe.
3. Survivorship Bias
Track listing/delisting dates; point-in-time universe construction.
4. Price Limits
Daily returns capped at ±10%; limit-up/down flags prevent impossible fills.
5. ST Stocks
is_st flag; excluded by default (±5% limits, high delisting risk).
6. T+1 Rule
Monthly rebalance naturally avoids; daily uses shift(-1) for next-day execution.
7. Transaction Costs
Commission 0.03% bilateral + stamp tax 0.1% sell-only + slippage model.
8. Lot Size
Optimizer rounds down to 100-share multiples (1 lot = 100 shares).
9. Ex-Dividend
Forward adjustment embeds dividend adjustments into historical prices.
10. Sector Drift
Latest sector classification; dynamic neutralization handles reclassifications.

Project Structure

84 Python modules organized by domain.

File Tree
quant_platform/
├── main.py                          # CLI entry point
├── app.py                           # FastAPI application
├── CLAUDE.md                        # Full architecture docs
├── INTERVIEW_CHEATSHEET.md          # Interview prep (12 topics)
├── JANE_STREET_GAP_ANALYSIS.md      # Gap analysis vs Jane Street
│
├── core/                            # ★ Core Architecture
│   ├── event_bus_v2.py              # AsyncEventBus (backpressure+DLQ+WAL)
│   ├── events.py                    # Event bus bridge (backward compat)
│   ├── message_bus.py               # Distributed bus (Local/Redis/Kafka)
│   ├── store.py                     # SQLite persistence (WAL, 8 tables)
│   ├── state_machine.py             # 8-state lifecycle
│   ├── audit.py                     # Compliance audit trail
│   └── scheduler.py                 # A-share market hours
│
├── data/                            # Data Layer
│   └── providers/                   # Synthetic/Tushare/Baostock/PG/WS/L2
│
├── factors/                         # Factor Engine (15 factors)
│   ├── technical.py                 # 10 technical factors
│   ├── fundamental.py               # 5 fundamental factors
│   ├── network.py                   # Graph network factor
│   └── ic_monitor.py                # IC decay monitoring
│
├── alpha/                           # Alpha Model (IC/ICIR/ML/LLM)
├── portfolio/                       # Portfolio Optimization (EW/MVO/RP)
│
├── backtest/                        # Backtest Engine
│   ├── engine.py                    # Vectorized monthly
│   └── tick_engine.py               # ★ Tick-level event-driven
│
├── execution/                       # Execution Layer
│   ├── order_book.py                # ★ Real LOB (FIFO+IOC/FOK+VPIN)
│   └── market_impact.py             # ★ Impact models (AC/SR/Kyle)
│
├── risk/                            # Risk Management
│   ├── realtime_engine.py           # ★ Real-time risk (Greeks+pre-trade)
│   ├── greeks.py                    # ★ Black-Scholes Greeks
│   └── barra.py                     # Barra 10-factor model
│
├── trading/                         # Live Trading
│   ├── broker.py                    # SimulatedBroker (LOB) + QMTBroker
│   └── engine.py                    # Trading engine (EventBus+Risk+LOB)
│
├── services/                        # Microservice skeletons
├── agent/                           # LLM modules
├── utils/                           # Utilities
│   └── cyext/                       # ★ Cython hot paths (3 .pyx)
│
├── api/                             # FastAPI (91 endpoints)
├── frontend/                        # Vue 3 + ECharts
├── monitoring/                      # Grafana dashboard (16 panels)
│
└── tests/                           # 610 unit tests
    ├── test_core/                   # EventBus(38) + Store(16) + SM(15) + ...
    ├── test_execution/              # OrderBook(22) + OMS(17) + Impact(10)
    ├── test_risk/                   # RealTimeRisk(12) + Greeks(8) + ...
    ├── test_backtest/               # TickEngine(15) + Cost(4) + ...
    └── test_utils/                  # Cython(18) + Numba + Cache + ...