CarrotQuant Engine (carrotquant-engine)
CarrotQuant Engine 是基于 Python 与 Numba 的高性能事件驱动与向量化量化回测引擎,支持全市场多品种多表的数据供给、撮合执行与绩效分析。
📦 安装指南 (Installation)
环境要求:Python >= 3.12(支持 Python 3.12 / 3.13 / 3.14+)。
# 使用 uv 安装
uv add carrotquant-engine
🛠️ 特性 (Features)
- 高性能计算内核:基于 Numba JIT 与连续 2D C-Contiguous 内存布局,降低循环执行与内存分配开销。
- Duck Typing 数据协议与多表供给:
engine.run(data=...)原生支持标准dict传参与 Duck Typing(具备.to_df()/.read()/.collect()的数据源、LazyFrame、DataFrame或 Stream 生成器),支持主行情表、副行情表(如指数index)、特征列(如pe_ttm)、稀疏离散事件(如龙虎榜)与静态属性(如板块)。 - 单趟极速矩阵构建 (MatrixBuilder):单趟完成全局坐标映射与内存填充,严格校验 OHLC 全量价格列,未提供
volume/amount时保持纯净None。 - Master Clock 时空对齐:副 TS 表按主时钟自动 Left Join 内存对齐,超出时间步截断,缺失时间步填充
NaN。 - 多空双向撮合:
buy/sell支持做多与做空 (pos += amount与pos -= amount),统一浮动资产计算 $PV = \text{Cash} + \sum \text{pos}_i \times \text{close}_i$。 - 轻量动态复权:
data.close为原始成交价(用于资金交割),data.adj.close/ctx.adj.close提供动态后复权视图。 - 防未来函数切片:策略通过
ctx.get('factor')(当前 $t$ 步快照)与ctx.get_history('factor')(物理边界[:t+1, :])访问数据,避免未来数据泄露。 - 分块流式预热 (
warmup_steps):支持分块流式回测并在预热期只更新指标状态而不触发资金扣除。 - 流动性与撮合限制:支持
max_volume_ratio(盘口成交量比例限制)、限价单buy_limit/sell_limit与cancel_order撤单机制。 - 保证金与融资融券费率:支持设置
long_margin_ratio/short_margin_ratio(保证金率校验),以及margin_interest_rate/borrow_interest_rate(日频利息计提)。
🚀 快速开始
from cq.engine import strategy, BarContext, Engine, ts_table, event_table, static_table
import polars as pl
# 1. 定义策略 (支持访问副行情表 index 与离散事件表)
@strategy
def multi_asset_strategy(ctx: BarContext):
# 读取副表(指数)当前价格与历史收盘价切片 [:t+1, :]
index_table = ctx.get("index")
index_close_hist = index_table.close_history
# 读取离散事件表 (龙虎榜)
dt_events = ctx.get("dragon_tiger")
for i in range(ctx.n_symbols):
if not ctx.is_tradable[i]:
continue
# 使用 ctx.adj.close_history 读取后复权历史收盘价
c_hist = ctx.adj.close_history[-20:, i]
ma5 = c_hist[-5:].mean()
ma20 = c_hist[-20:].mean()
# 结合指数趋势与均线信号买卖
if ma5 > ma20 and ctx.positions[i] == 0:
ctx.buy(symbol_idx=i, amount=100)
elif ma5 < ma20 and ctx.positions[i] > 0:
ctx.sell(symbol_idx=i, amount=ctx.positions[i])
# 2. 初始化回测引擎
engine = Engine(
initial_cash=1_000_000.0,
fee_rate=0.0003,
min_fee=5.0,
stamp_duty=0.0005,
slippage=0.0001,
max_volume_ratio=0.1,
matching_mode="close"
)
# 3. 运行多表回测 (主表自动推断,副表显式声明物理语义)
results = engine.run(
strategy=multi_asset_strategy,
data={
"stock": pl.read_parquet("data/parquet/ashare.kline.1d/**/*.parquet"),
"index": ts_table(pl.read_parquet("data/parquet/aindex.kline.1d/**/*.parquet")),
},
)
# 4. 输出回测绩效与 Polars 交易日志
print(results.summary())
print(results.trade_logs) # Polars DataFrame
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
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 carrotquant_engine-1.3.0.tar.gz.
File metadata
- Download URL: carrotquant_engine-1.3.0.tar.gz
- Upload date:
- Size: 40.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0330f433368893106d67bb20d11d42d77806f22c50e9b779368b0edec32c3f34
|
|
| MD5 |
a83dca411be10eb03df2857b2201edab
|
|
| BLAKE2b-256 |
0a2f757bb6f1d47f439e1ad1b7be197802de0c44a6f0843b7730e087d96108c9
|
Provenance
The following attestation bundles were made for carrotquant_engine-1.3.0.tar.gz:
Publisher:
workflow.yml on CRThu/carrotquant-engine
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
carrotquant_engine-1.3.0.tar.gz -
Subject digest:
0330f433368893106d67bb20d11d42d77806f22c50e9b779368b0edec32c3f34 - Sigstore transparency entry: 2678186496
- Sigstore integration time:
-
Permalink:
CRThu/carrotquant-engine@c7efceebdcc661ace3eb1c51c5f9e98ea4793b22 -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/CRThu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@c7efceebdcc661ace3eb1c51c5f9e98ea4793b22 -
Trigger Event:
push
-
Statement type:
File details
Details for the file carrotquant_engine-1.3.0-py3-none-any.whl.
File metadata
- Download URL: carrotquant_engine-1.3.0-py3-none-any.whl
- Upload date:
- Size: 47.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fb82513b04a2bac4858c1e7c74b7ae7bdc3cb9a22206cc31d498d5372b87189
|
|
| MD5 |
bcccf64e0e8cf0256c53c874a5194980
|
|
| BLAKE2b-256 |
61b5e81f896acfa22596e70b711a8f0f812bd3cf02aa680b15728616fab47d87
|
Provenance
The following attestation bundles were made for carrotquant_engine-1.3.0-py3-none-any.whl:
Publisher:
workflow.yml on CRThu/carrotquant-engine
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
carrotquant_engine-1.3.0-py3-none-any.whl -
Subject digest:
8fb82513b04a2bac4858c1e7c74b7ae7bdc3cb9a22206cc31d498d5372b87189 - Sigstore transparency entry: 2678186563
- Sigstore integration time:
-
Permalink:
CRThu/carrotquant-engine@c7efceebdcc661ace3eb1c51c5f9e98ea4793b22 -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/CRThu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@c7efceebdcc661ace3eb1c51c5f9e98ea4793b22 -
Trigger Event:
push
-
Statement type: