CarrotQuant
CarrotQuant 是基于 Python/Numba 的 1m+ 高性能通用全市场 (A股/美股/期货) 事件驱动与向量化量化回测引擎。
🌟 核心亮点
- 极致吞吐量:基于 Numba JIT 打平内联与连续 2D C-Contiguous 内存布局,零堆分配开销 (3000万+ Ticks/s)。
- 通用多空机制:
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为动态懒求值复权视角。无复权需求或已有复权时零开销。 - 多表与自定义列按需/懒加载 (
LazyCustomFields):自动支持多表字段与自定义特征列(如因子factor_b、pe_ttm、vwap等)。支持custom_columns显式筛选与字段级懒透视,访问时才生成 2D 矩阵,未访问零开销。 - 物理严格防未来切片:策略通过
ctx.get('factor_b')(当前 $t$ 步切片)与ctx.get_history('factor_b')(物理边界[:t+1, :])访问数据,绝无未来函数污染。 - 盘口流动性限制:支持设置
max_volume_ratio(例如0.1表示单笔交易上限为当前 Bar 10% 成交量)。 - 限价单与撤单机制:支持
buy_limit/sell_limit限价单与cancel_order撤单,支持跨 Bar 订单保存与价格触达自动撮合。 - 做多/做空保证金率与融资融券扣费:支持设置
long_margin_ratio/short_margin_ratio(保证金率校验),以及margin_interest_rate/borrow_interest_rate(日频融资与融券利息扣除)。 - 统一极简单入口 API (
engine.run):无论内存单 Container、磁盘分块 Stream,还是 JIT 信号矩阵,统一通过engine.run(...)单一方法启动。
🚀 快速开始
from carrotquant import strategy, BarContext, Engine, ColumnDataLoader
# 1. 定义策略 (使用 @strategy 装饰器,支持自定义列 factor_b)
@strategy
def dual_ma_strategy(ctx: BarContext):
# 读取自定义因子列 factor_b 当前快照 (N,) 与 历史切片 [:t+1, :]
factor_b = ctx.get("factor_b")
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 factor_b[i] > 0.5 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. 磁盘级惰性分块扫描 (指定按需加载特征列 custom_columns)
data_stream = ColumnDataLoader.scan_parquet_chunks(
path="data/parquet/ashare.kline.1m",
custom_columns=["factor_b"],
partition_by="year"
)
# 3. 初始化并运行统一引擎 (设置印花税、佣金、万一滑点、盘口 10% 成交量比例)
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, # 盘口最多吃 10% 流动性
matching_mode="close" # 字符串指定按收盘价撮合
)
results = engine.run(strategy=dual_ma_strategy, data=data_stream)
# 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.0.4.tar.gz.
File metadata
- Download URL: carrotquant_engine-1.0.4.tar.gz
- Upload date:
- Size: 22.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
663fc71fb46021845cb5755321378d5395fb4b81caa39cb1ad3a20c867c03e14
|
|
| MD5 |
7543d4a6b5eb90a3aaaea930802e0722
|
|
| BLAKE2b-256 |
6b65004e4c140a21d57b36a00df740e7750cc0e34c6b8cadb3a03dac8d9eb454
|
Provenance
The following attestation bundles were made for carrotquant_engine-1.0.4.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.0.4.tar.gz -
Subject digest:
663fc71fb46021845cb5755321378d5395fb4b81caa39cb1ad3a20c867c03e14 - Sigstore transparency entry: 2476599993
- Sigstore integration time:
-
Permalink:
CRThu/carrotquant-engine@a969973d3dccaf7c79fe0bd45043b5aba1cf8cf4 -
Branch / Tag:
refs/tags/v1.0.4 - Owner: https://github.com/CRThu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@a969973d3dccaf7c79fe0bd45043b5aba1cf8cf4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file carrotquant_engine-1.0.4-py3-none-any.whl.
File metadata
- Download URL: carrotquant_engine-1.0.4-py3-none-any.whl
- Upload date:
- Size: 24.1 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 |
82b4a2d3f6c90226f96a779165ebb7a235c0e2d7802614f83aa87be2c9680e2a
|
|
| MD5 |
ac960ef9c79945c719ca0d8c0e70b70f
|
|
| BLAKE2b-256 |
c3079e6a0274c2404c5a590df27be9caad0ddf42f68cea5fe33e3ba31e113bbc
|
Provenance
The following attestation bundles were made for carrotquant_engine-1.0.4-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.0.4-py3-none-any.whl -
Subject digest:
82b4a2d3f6c90226f96a779165ebb7a235c0e2d7802614f83aa87be2c9680e2a - Sigstore transparency entry: 2476600004
- Sigstore integration time:
-
Permalink:
CRThu/carrotquant-engine@a969973d3dccaf7c79fe0bd45043b5aba1cf8cf4 -
Branch / Tag:
refs/tags/v1.0.4 - Owner: https://github.com/CRThu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@a969973d3dccaf7c79fe0bd45043b5aba1cf8cf4 -
Trigger Event:
push
-
Statement type: