AI 原生量化交易框架 — Rust 实现高性能内核,Python 提供 RL 训练接口,一套代码贯穿回测、训练、优化、验证、生产的完整链路。
Project description
English | 中文
AXON
面向量化交易与强化学习的事件驱动交易引擎。它从设计之初就以 AI 为核心,而非在传统量化系统上"嫁接"机器学习模块。
AI 原生量化交易框架 — Rust 实现高性能内核,Python 提供 RL 训练接口,一套代码贯穿回测、训练、优化、验证、生产的完整链路。
设计哲学
- AI First:强化学习(RL)环境与回测引擎共享同一套数据结构,训练与生产零差异
- Rust Core:纳秒级时间戳、确定性撮合、零成本抽象,回测吞吐 > 1M events/sec
- Python Front:通过 PyO3 暴露 Gymnasium 兼容接口,可直接挂 Stable-Baselines3 / Ray RLlib
- Full Pipeline:回测 → 训练 → HPO → Walk-forward → 追踪 → 注册 → 部署,全链路内置
- 100% 开源:Apache-2.0 许可,无企业版、无功能阉割
特性
回测引擎
- 多级撮合:L1 基础撮合 → L2 复杂订单簿 → L3 多资产交叉
- 冲击模型:Almgren-Chriss 永久/临时冲击 + 概率延迟 + 分层费用
- 确定性回放:
SimulatedClock+ crossbeam-channel bounded 100K 事件队列 - 列式存储:Arrow/Parquet,1M tick 读写 < 15ms
RL 环境
- Gymnasium API:离散 / 连续 / 混合动作空间
- 奖励函数:PnL / Sharpe / Sortino,基于统一
ReturnHistory - 向量化:
VecEnv支持多环境并行 rollout - PyO3 绑定:maturin 打包,6 个子模块
训练管线
- 超参优化:Optuna 集成 + NSGA-II 多目标 + Pareto 前沿 + 早停剪枝
- 滚动前向验证:Purged + Embargo + 泄漏检测 + Deflated Sharpe Ratio
- 实验追踪:MLflow / WandB / Local / Memory 四后端
- 模型注册:SemVer + 阶段生命周期 + 自动归档 + 回滚
- 分布式训练:Ray Actor + Parameter Server + Checkpoint 容错
AI 增强
- LLM 智能体:ReAct + Tool Calling,内置
PlaceOrder/QueryPortfolio交易工具,带 SafetyMode 风控 - 模型集成:Voting / Stacking / 动态加权,在线监控夏普比率自动调权
- 可解释性:SHAP 特征归因 + 反事实解释 +
Explainertrait 内建 - 合规审计:不可篡改的交易日志 + 决策报告归档
生产部署
- 交易所适配:Binance / OKX REST + WebSocket(自动重连)
- 风控引擎:预交易检查(12ns)、实时熔断、仓位限制
- 推理引擎:ONNX / Candle 双后端 + CPU/GPU 亲和性绑核 + 批推理
快速开始
环境要求
- Rust >= 1.96.0(rustup)
- Python >= 3.12(可选,用于 RL 训练)
编译与测试
git clone https://github.com/pengwow/axon_quant.git
cd axon_quant
# 编译
cargo build
# 测试(1200+ 用例)
cargo test --workspace
# 静态检查
cargo clippy --workspace -- -D warnings
Python Wheel
# 构建 wheel
maturin build --release
# 安装
pip install target/wheels/axon_quant-*.whl
# 验证
python -c "import axon_quant; print(axon_quant.__version__)"
训练示例
# 随机基线
python examples/01_random_agent.py
# PPO 训练
python examples/02_train_ppo.py --timesteps 50000
# HPO 优化
python examples/03_hpo/hpo_single_objective.py
# 滚动前向验证
python examples/08_walk_forward/walk_forward_basic.py
架构
AXON 采用 Cargo Workspace 管理 21 个 crate,按依赖层级自下而上分为 9 层:
┌─────────────────────────────────────────────────────────────┐
│ Layer 9: 应用入口 │
│ ├─ axon-cli CLI 工具 │
│ └─ axon-python PyO3 统一入口(axon_quant 包) │
├─────────────────────────────────────────────────────────────┤
│ Layer 8: AI 智能体 │
│ ├─ axon-llm ReAct 智能体 + Tool Calling │
│ └─ axon-explain SHAP / 反事实 / 决策报告 │
├─────────────────────────────────────────────────────────────┤
│ Layer 7: 模型服务 │
│ ├─ axon-inference ONNX / Candle / tch 推理引擎 │
│ └─ axon-ensemble 模型集成(投票 / Stacking / 动态加权) │
├─────────────────────────────────────────────────────────────┤
│ Layer 6: 训练管线 │
│ ├─ axon-rl Gymnasium 环境 + VecEnv + 奖励函数 │
│ ├─ axon-hpo Optuna 超参优化(单目标 / 多目标) │
│ ├─ axon-distributed Ray Actor 分布式训练 │
│ └─ axon-walk-forward 滚动前向验证(Purged + Embargo) │
├─────────────────────────────────────────────────────────────┤
│ Layer 5: 实验治理 │
│ ├─ axon-tracker MLflow / WandB / Local / Memory 追踪 │
│ └─ axon-registry 模型注册表(SemVer + 生命周期 + 回滚) │
├─────────────────────────────────────────────────────────────┤
│ Layer 4: 生产执行 │
│ ├─ axon-exchange Binance / OKX 适配器(REST + WebSocket) │
│ ├─ axon-risk 风控引擎(仓位 / 回撤 / VaR / 熔断) │
│ ├─ axon-oms 订单管理系统 │
│ └─ axon-monitor 监控告警 + 健康检查 │
├─────────────────────────────────────────────────────────────┤
│ Layer 3: 回测引擎 │
│ ├─ axon-backtest L1/L2/L3 撮合 + Almgren-Chriss 冲击模型 │
│ └─ axon-compliance 合规审计 + 日报 / 月报 / 年报 │
├─────────────────────────────────────────────────────────────┤
│ Layer 2: 数据服务 │
│ └─ axon-data Arrow 列式存储 + CSV/Parquet 源 + 特征管道 │
├─────────────────────────────────────────────────────────────┤
│ Layer 1: 核心类型 │
│ └─ axon-core 时间戳 / 价格 / 数量 / 订单 / 事件 / 队列 │
│ / 组合 / 波动率 / 延迟 / 费用 / SIMD │
└─────────────────────────────────────────────────────────────┘
线程模型
- 核心匹配引擎:单线程,避免锁竞争,保证确定性
- I/O 线程池:tokio runtime,处理 WebSocket / REST / 文件 I/O
- 计算线程池:rayon,因子计算 / 数据转换 / 并行回测
- 事件队列:crossbeam-channel bounded 100K,零锁设计
数据管道
AXON 的所有模块共享同一套 Arrow RecordBatch,零拷贝透传,无格式转换断层:
数据源 (CSV/Parquet/WebSocket/Mock/交易所 API)
│
▼
axon-data (schema 校验 / 时间对齐 / 去重 / mmap 缓存)
│
▼
Arrow RecordBatch (内存) ──→ TradingEnv / FeaturePipeline / BacktestEngine
│
▼
InferenceEngine (ONNX/Candle 批推理 < 1ms)
│
▼
ExchangeAdapter (Binance/OKX 实盘下单)
层级说明
- axon-core:整个系统的基石。提供
Timestamp(纳秒精度)、Price/Quantity(基于rust_decimal)、Order、Event、Queue、Portfolio等核心类型,以及 SIMD 加速的归一化与订单簿操作。 - axon-data:统一数据接入层。基于 Apache Arrow 的
RecordBatch列式存储,支持 CSV / Parquet / Mock 数据源,内置FeaturePipeline(Z-Score 归一化 + 滑动窗口)。 - axon-backtest:事件驱动回测引擎。支持 L1(价格优先)、L2(订单簿)、L3(暗池 / 拍卖)三级撮合,集成 Almgren-Chriss 市场冲击模型与概率延迟模拟。
- axon-exchange:生产级交易所适配器。统一
ExchangeAdaptertrait,已实现对 Binance / OKX 的 REST + WebSocket 对接,内置指数退避重连与令牌桶限流。 - axon-rl:强化学习环境。
TradingEnv实现 Gymnasium 标准接口(reset/step/render),支持连续动作(目标仓位比例[-1, 1])、离散动作(分仓档位)、多目标奖励与向量化并行环境VecEnv。 - axon-inference:模型推理引擎。支持 ONNX Runtime、Candle(纯 Rust)、tch-rs(PyTorch C++)三后端,具备异步批推理管线、CPU/GPU 亲和性绑定与模型热更新能力。
- axon-llm:大语言模型智能体。基于 ReAct 推理循环,内置"市场分析"、"查询持仓"、"提交订单"三个工具,支持 OpenAI 兼容后端与流式响应。
- axon-explain:可解释性引擎。集成 SHAP 特征归因、反事实解释("如果当时不买入,收益会如何变化")与结构化决策报告,满足监管合规与策略迭代需求。
- axon-ensemble:模型集成。提供 HardVote、SoftVote、WeightedVote、Stacking、DynamicWeighted 五种策略,支持在线性能监控与自动权重调整。
仓库结构
axon_quant/
├── crates/ # 21 个 Rust crate
│ ├── axon-core/ # 核心类型(time/types/market/order/event/queue/portfolio)
│ ├── axon-backtest/ # 回测引擎(L1/L2/L3 撮合 + 冲击模型)
│ ├── axon-rl/ # RL 环境(Gymnasium + VecEnv)
│ ├── axon-hpo/ # 超参数优化(Optuna + NSGA-II)
│ ├── axon-walk-forward/ # 滚动前向验证(Purged + Embargo)
│ ├── axon-distributed/ # 分布式训练(Ray)
│ ├── axon-tracker/ # 实验追踪(MLflow/WandB/Local/Memory)
│ ├── axon-registry/ # 模型注册表(SemVer + 生命周期)
│ ├── axon-exchange/ # 交易所适配器(Binance/OKX)
│ ├── axon-inference/ # 推理引擎(ONNX/Candle)
│ ├── axon-risk/ # 风控引擎
│ ├── axon-oms/ # 订单管理系统
│ ├── axon-monitor/ # 监控告警
│ ├── axon-llm/ # LLM 智能体
│ ├── axon-python/ # Python 绑定入口
│ └── axon-cli/ # CLI 工具
├── python/ # Python 包(axon_quant)
├── examples/ # 训练示例脚本
├── tests/ # 测试(Rust + Python)
├── docs/ # 设计文档 + ADR
├── scripts/ # 构建与测试脚本
├── pyproject.toml # Python 打包配置
├── Makefile # 开发命令
└── Dockerfile # 多阶段构建
Crate 矩阵
| Crate | 功能 |
|---|---|
| axon-core | 核心类型(11 模块) |
| axon-backtest | 回测引擎(L1/L2/L3) |
| axon-rl | RL 环境(Gymnasium + VecEnv) |
| axon-hpo | 超参数优化(Optuna) |
| axon-walk-forward | 滚动前向验证 |
| axon-distributed | 分布式训练(Ray) |
| axon-tracker | 实验追踪 |
| axon-registry | 模型注册表 |
| axon-exchange | 交易所适配器(Binance/OKX) |
| axon-inference | 推理引擎(ONNX/Candle) |
| axon-python | Python 绑定(PyO3) |
| axon-cli | CLI 工具 |
| axon-risk | 风控引擎 |
| axon-oms | 订单管理 |
| axon-monitor | 监控告警 |
| axon-llm | LLM 智能体 |
| axon-explain | SHAP 可解释性 |
| axon-ensemble | 模型集成 |
| axon-compliance | 合规审计 |
| axon-data | 数据服务 |
| axon-integration-tests | 集成测试 |
性能
| 指标 | 数值 |
|---|---|
| 回测吞吐 | > 1M events/sec |
| 撮合延迟 | < 1us (P99) |
| 风控检查 | 12ns (AtomicBool 熔断 + HashMap 仓位) |
| 订单提交 | 1.2µs (幂等 + UUID v7 + 状态机) |
| RL 训练 | > 10k steps/sec (8 env VecEnv) |
| 分布式加速 | > 5x (8 workers) |
| 测试用例 | 1200+ Rust + 24 Python |
基准测试
workspace 已建立 50+ Criterion bench,跨 5 个 crate:
| Crate | Bench 入口 | 覆盖 |
|---|---|---|
axon-core |
benches/core_bench.rs |
28 个:冲击模型/波动率/延迟/订单簿/订单/事件/费用 |
axon-backtest |
benches/impact_bench.rs |
8 个:撮合延迟/不同冲击模型/订单簿深度/永久衰减/多笔/TOML 配置 |
axon-data |
benches/axon_data_bench.rs |
7 个 group(8+ bench):LRU/Dataset lazy/CSV/Parquet 流式/Bar 聚合/Mock/Mmap |
axon-rl |
benches/rl_bench.rs |
11 个:观测/奖励/TradingEnv 端到端/Action 转换 |
| Phase 4 crates | benches/phase4_bench.rs |
15 个:风控/OMS/监控延迟 |
make bench # 全 workspace,本地 5-10 分钟
make bench-cmp # 存 main baseline,PR 对比
make bench-one CRATE=axon-core BENCH=event_builder_tick # 单个 bench
cargo bench -p axon-core -- impact_linear # 直接 cargo 跑
CI 不跑 bench(避免 main runner 性能噪声)。报告: target/criterion/<group>/report/index.html。
CPU/GPU 亲和性
axon-inference 提供 affinity 模块,跨平台绑核降低跨核 cache miss:
use axon_inference::affinity::{AffinityPlan, pin_to};
let plan = AffinityPlan::new().with_cpus(vec![0, 1]).with_cuda(0);
pin_to(&plan)?;
或通过 BatchConfig 配置(BatchInferencePipeline::new 启动时自动调):
[batch]
collect_cpu_cores = [0, 1, 2, 3]
collect_gpu_device_id = 0
平台支持: Linux / macOS 完整支持, Windows 运行时返回 Err(AffinityError::NotAvailable)(用 WSL2 / numactl 替代)。
工程实践
- TDD 驱动 — 先测试后实现,CI 强制
-D warnings - 1200+ 测试 — 单元测试 + 集成测试 + Python 场景测试
- cargo clippy — 零警告策略
- cargo-mutants — 变异测试覆盖
- cargo-fuzz — 模糊测试(撮合引擎/订单簿/风控)
- Miri — 数据竞争检测
- Loom — 确定性并发测试
文档
许可
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file axon_quant-0.1.3.tar.gz.
File metadata
- Download URL: axon_quant-0.1.3.tar.gz
- Upload date:
- Size: 879.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f0c1b1dbf5ac9f02751c7e78554391c61c15958f1e44327b084ed638ef52a28c
|
|
| MD5 |
d9ee521990c6c8930cd00b723477cc76
|
|
| BLAKE2b-256 |
c64fb4dfe73251868be0258c815dfb5991ce2d7b83c0af3e76457e026078002f
|
Provenance
The following attestation bundles were made for axon_quant-0.1.3.tar.gz:
Publisher:
publish.yml on pengwow/axon_quant
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
axon_quant-0.1.3.tar.gz -
Subject digest:
f0c1b1dbf5ac9f02751c7e78554391c61c15958f1e44327b084ed638ef52a28c - Sigstore transparency entry: 1893060373
- Sigstore integration time:
-
Permalink:
pengwow/axon_quant@83545dce2bf8f2b464f724e138b1803df556a807 -
Branch / Tag:
refs/heads/0.1.3 - Owner: https://github.com/pengwow
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@83545dce2bf8f2b464f724e138b1803df556a807 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file axon_quant-0.1.3-cp314-cp314-win_amd64.whl.
File metadata
- Download URL: axon_quant-0.1.3-cp314-cp314-win_amd64.whl
- Upload date:
- Size: 13.0 MB
- Tags: CPython 3.14, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82e06c437f181a8429b0c5b37dc69e36b71c4ab838fed521762135a5bd51bd71
|
|
| MD5 |
8533d7f6e5e9391c300566346feafb85
|
|
| BLAKE2b-256 |
2ffb5091d786ea2247f861b24cf71dc2cf93f8f3c913d7c91c886532938c27aa
|
Provenance
The following attestation bundles were made for axon_quant-0.1.3-cp314-cp314-win_amd64.whl:
Publisher:
publish.yml on pengwow/axon_quant
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
axon_quant-0.1.3-cp314-cp314-win_amd64.whl -
Subject digest:
82e06c437f181a8429b0c5b37dc69e36b71c4ab838fed521762135a5bd51bd71 - Sigstore transparency entry: 1893060816
- Sigstore integration time:
-
Permalink:
pengwow/axon_quant@83545dce2bf8f2b464f724e138b1803df556a807 -
Branch / Tag:
refs/heads/0.1.3 - Owner: https://github.com/pengwow
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@83545dce2bf8f2b464f724e138b1803df556a807 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file axon_quant-0.1.3-cp314-cp314-manylinux_2_38_x86_64.whl.
File metadata
- Download URL: axon_quant-0.1.3-cp314-cp314-manylinux_2_38_x86_64.whl
- Upload date:
- Size: 14.8 MB
- Tags: CPython 3.14, manylinux: glibc 2.38+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b239776a17e5a5389dff528b041dcd83175866e742cf7a997b4bbfa4bea78b64
|
|
| MD5 |
d6182b851b1951951f0ae3e2e5267d7a
|
|
| BLAKE2b-256 |
584b3a6a73b7c14f9f272263b7f961d47d634dbd4793afc703fddd81f4857263
|
Provenance
The following attestation bundles were made for axon_quant-0.1.3-cp314-cp314-manylinux_2_38_x86_64.whl:
Publisher:
publish.yml on pengwow/axon_quant
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
axon_quant-0.1.3-cp314-cp314-manylinux_2_38_x86_64.whl -
Subject digest:
b239776a17e5a5389dff528b041dcd83175866e742cf7a997b4bbfa4bea78b64 - Sigstore transparency entry: 1893060910
- Sigstore integration time:
-
Permalink:
pengwow/axon_quant@83545dce2bf8f2b464f724e138b1803df556a807 -
Branch / Tag:
refs/heads/0.1.3 - Owner: https://github.com/pengwow
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@83545dce2bf8f2b464f724e138b1803df556a807 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file axon_quant-0.1.3-cp314-cp314-macosx_11_0_arm64.whl.
File metadata
- Download URL: axon_quant-0.1.3-cp314-cp314-macosx_11_0_arm64.whl
- Upload date:
- Size: 13.5 MB
- Tags: CPython 3.14, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
958d7c9c3fc77fde12aa3ab85b25b407cd1de1f0b9bca501fe938f2ab51c0712
|
|
| MD5 |
1b5efa3ead75187dbe9c1f44f8dd06a7
|
|
| BLAKE2b-256 |
0b35f41c1458383cfef273ad797ca0f288c8f64e20cfcc8462b34dd29f15a18e
|
Provenance
The following attestation bundles were made for axon_quant-0.1.3-cp314-cp314-macosx_11_0_arm64.whl:
Publisher:
publish.yml on pengwow/axon_quant
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
axon_quant-0.1.3-cp314-cp314-macosx_11_0_arm64.whl -
Subject digest:
958d7c9c3fc77fde12aa3ab85b25b407cd1de1f0b9bca501fe938f2ab51c0712 - Sigstore transparency entry: 1893060653
- Sigstore integration time:
-
Permalink:
pengwow/axon_quant@83545dce2bf8f2b464f724e138b1803df556a807 -
Branch / Tag:
refs/heads/0.1.3 - Owner: https://github.com/pengwow
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@83545dce2bf8f2b464f724e138b1803df556a807 -
Trigger Event:
workflow_dispatch
-
Statement type: