# HFTBacktest **Repository Path**: chen_cong/hftbacktest ## Basic Information - **Project Name**: HFTBacktest - **Description**: No description available - **Primary Language**: C++ - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-08-05 - **Last Updated**: 2026-09-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # HFTBacktest > A high-fidelity HFT (high-frequency trading) backtest engine, written in modern C++20. > Layered architecture, multi-provider data ingestion, pluggable matching algorithms, three-tier risk management, and a 36-indicator analytics suite. --- ## ✨ Features | Module | Highlights | | ------------------- | -------------------------------------------------------------------------------------------- | | **Data Provider** | Local CSV/Parquet + Online HTTP (cpp-httplib) + Redis cache (in-memory or RESP-TCP backend) | | **Matching Engine** | Price-time priority, VWAP, Iceberg; real order book & queue; maker/taker cost model | | **Strategy SDK** | Dynamic `.so`/`.dll` loader; sandbox + resource limits; JWT-secured auth | | **Risk** | Pre-trading / Real-time / Post-trading 3-tier + pluggable rules | | **Analytics** | 36 standard metrics: Sharpe/Sortino/Calmar/Alpha/Beta/Brinson/IC/IR | | **Task Scheduler** | Local / Distributed (Ray/Celery/Argo ready); Grid/Random/Bayesian optimization | | **Web** | Vue 3 frontend + FastAPI proxy + `/health` endpoint | | **Deployment** | Docker / docker-compose / Kubernetes manifest + Prometheus + Grafana | ## 🏗️ Architecture ```mermaid flowchart TB Web["Web UI
(Vue 3 + FastAPI + /health)"] Task["Task Layer
(Local / Ray / Argo)"] Strat["Strategy
(.so/.dll loader)"] Risk["Risk
(3-tier rules)"] Engine["Engine Core
(matching)
━━━━━━━━━━
price_time / VWAP / Iceberg
real cost model (maker/taker)"] Data["Data Provider
(local/HTTP/enterprise)"] Storage["Storage
(Redis / ClickHouse / MinIO)"] Web --> Task Task --> Strat Strat --> Engine Risk --> Engine Engine --> Data Data --> Storage classDef ext fill:#fef3c7,stroke:#b45309,color:#78350f; classDef core fill:#dbeafe,stroke:#1e40af,color:#1e3a8a; classDef backend fill:#dcfce7,stroke:#166534,color:#14532d; class Web,Strat ext; class Engine,Data,Risk core; class Task,Storage backend; ``` > The legacy ASCII diagram is still in `git log`; GitHub/Gitee renders > this mermaid version directly. Generate the API reference with > `doxygen Doxyfile` (output: `docs/api/html/index.html`). ## 🚀 Quick Start ### Prerequisites - CMake ≥ 3.20 - C++20 compiler (MSVC 2022 / GCC 11+ / Clang 14+) - vcpkg or system-installed: spdlog, fmt, tomlplusplus, cpp-httplib (header-only) ### Build ```bash # Linux / macOS cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release cmake --build build -j$(nproc) # Windows (VS generator) cmake -B build -G "Visual Studio 17 2022" -A x64 cmake --build build --config Release -j # Windows (Ninja + vcvars64 — recommended for CI parity) # Run from a "x64 Native Tools Command Prompt for VS 2022" shell: cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release cmake --build build -j # Run unit tests ctest --test-dir build --output-on-failure ./build/tests/hbt_tests # Linux/macOS ./build/tests/Release/hbt_tests.exe # Windows (VS generator) ``` > ⚠ **SDK users on Windows:** The bundled `out/_rebuild.bat` already runs vcvars64 + Ninja and is the fastest path. Use it as the canonical command: > ```cmd > out\_rebuild.bat > ``` ### Run a Backtest ```bash # Pre-recorded tick replay (PnL ≈ ¥1,000,300 on demo data) ./build/src/hbt_replay --config config/dev.toml # Linux/macOS .\build\src\hbt_replay.exe --config config\dev.toml # Windows # Or use the bundled demo driver for a single-symbol quick check: ./build/src/hbt_replay 600519 ./data ``` ### Compile Your Strategy The strategy SDK is platform-portable — `dlopen` on POSIX, `LoadLibraryA` on Windows. ```bash # Linux g++ -std=c++20 -shared -fPIC -O2 \ -I src/strategy/include -I src/common/include \ examples/my_ma20.cpp -o lib/libmy_ma20.so # macOS (Apple Silicon) clang++ -std=c++20 -shared -fPIC -O2 \ -I src/strategy/include -I src/common/include \ examples/my_ma20.cpp -o lib/libmy_ma20.dylib # Windows (MSVC, from x64 Native Tools shell) cl /nologo /std:c++20 /LD /EHsc /O2 \ /I src\strategy\include /I src\common\include \ examples\my_ma20.cpp /Fe:bin\my_ma20.dll ``` Then set in `dev.toml`: ```toml [strategy] so_path = "lib/libmy_ma20.so" # or libmy_ma20.dylib / my_ma20.dll params = { fast = "5", slow = "20" } ``` The runtime loader resolves `.so`/`.dylib`/`.dll` automatically. An empty `so_path` disables the user strategy (builtin strategies run instead). --- ## ✅ Test Coverage 77 unit tests across 16 files. Run from the build directory: ```bash ctest --output-on-failure # quick pass/fail ./tests/hbt_tests # per-test breakdown ``` | Module | Tests | Highlights | | --------------------- | ----- | --------------------------------------------------------------------------- | | common | 10 | ringbuffer, MPMC queue, types round-trip, time util, logger | | crypto primitives | 8 | SHA-256 (FIPS 180-4), HMAC-SHA256 (RFC 4231), base64url, constant-time | | auth_sso (JWT) | 8 | HS256 sign/verify, alg=none reject, expired/tampered, no-exp claim | | http_client | 7 | GET/POST/JSON, 404, invalid URL, mock httplib::Server | | online_data_provider | 8 | URL templates, CSV/JSON parse, mock HTTP endpoints, timeout | | redis_cache | 6 | memory backend + RESP-TCP backend (set/get/del/ttl/reconnect) | | engine_orderbook | 2 | add/remove, best levels | | engine_matcher | 7 | price-time/VWAP/Iceberg + market walking + cancel + part-fill M:N | | engine_core | 2 | push/pop MPMC, stats | | engine_main | 1 | end-to-end replay with trades | | cost | 10 | A-share/ETF/Future/Crypto fee schedules, maker/taker split, slippage | | risk | 10 | position limit, order rate, p4 rule factory (limit/drawdown/rate) | | analytics | 2 | metrics basics + benchmark attribution | | task | 1 | grid parameter sweep | | strategy_sdk | 3 | my_ma20 dynamic-load E2E (uptrend/downtrend, missing DLL) | | **Total** | **77** | **all passing** | Tests are auto-registered by `#include "test_*.cpp.inc"` from `test_main.cpp`. New tests should follow the `TEST(name) { ... }` macro pattern and be added to `tests/test_main.cpp`. Test files must use the `.cpp.inc` suffix (not `.cpp`): a `.cpp` file would also be compiled as a separate binary, which double-runs the whole suite. See `CONTRIBUTING.md` for the full workflow. --- ## 📂 Project Layout ``` HFTBacktest/ ├── CMakeLists.txt # 根 CMake ├── cmake/ # 依赖配置 ├── src/ │ ├── common/ # POD 类型、ringbuffer、MPMC、配置、logger │ ├── data_provider/ # 行情源:local/online/enterprise │ ├── engine/ # 撮合引擎:order_book/matcher/cost/scheduler │ ├── strategy/ # 策略 SDK:builtin/grid/sandbox/api/sdk │ ├── risk/ # 三层风控 │ ├── analytics/ # 36 指标 + 报告输出 │ ├── task/ # 任务调度 + 网格寻优 │ └── enterprise/ # ClickHouse/MinIO/TDengine backend + JWT auth ├── web/ # Vue 3 + FastAPI ├── deploy/ # Docker/k8s/prometheus ├── tests/ # 单元测试 (test_*.cpp.inc) ├── examples/ # 用户策略示例 ├── docs/ # 设计文档(18 篇) + 本 README 链接 └── config/ # TOML 配置 ``` ## 📚 Documentation ### 🚀 新人起步(强烈推荐先读) | 文档 | 时间 | 适合 | |------|------|------| | **[docs/00-quickstart.md](docs/00-quickstart.md)** | 5 分钟 | 小白 / 第一次接触 | | **[docs/README-dev.md](docs/README-dev.md)** | 3 分钟 | 文档地图(按角色推荐) | | **[docs/17-maintenance.md](docs/17-maintenance.md)** | 15 分钟 | 二次开发 / 维护 | ### 📖 设计文档(15 章) 1. [Tech Stack](docs/01-tech-stack.md) 2. [Architecture](docs/02-architecture.md) 3. [Directory](docs/03-directory.md) 4. [LOC Budget](docs/04-loc-budget.md) 5. [Data Provider](docs/05-data-provider.md) 6. [Matching Engine](docs/06-matching-engine.md) 7. [Strategy API](docs/07-strategy-api.md) 8. [Risk](docs/08-risk.md) 9. [Analytics](docs/09-analytics.md) 10. [Task Scheduler](docs/10-task-scheduler.md) 11. [Web API](docs/11-web-api.md) 12. [Deployment](docs/12-deployment.md) 13. [Enterprise Migration](docs/13-enterprise-migrate.md) 14. [Dev Stages](docs/14-dev-stages.md) 15. [Boundary](docs/15-boundary.md) ### 🛠️ 实战经验 | 文档 | 内容 | |------|------| | [phase3_replay.md](docs/phase3_replay.md) | 端到端回测 + 行情回放实现经验 | | [phase4_risk.md](docs/phase4_risk.md) | 风控模块接入 + 链接器 bug 排查 | | [16-stage2-plan.md](docs/16-stage2-plan.md) | Stage 2 计划 | | [M1 报告系列](../qclaw_workspace/hftbacktest_*.md) | M1 路线图 + P0-W8 完成记录 | ## 📄 License MIT