Skip to main content

CarrotQuant

CarrotQuant 是基于 Python/Numba 的 1m+ 高性能通用全市场 (A股/美股/期货) 事件驱动与向量化量化回测引擎。

🌟 核心亮点

  • 极致吞吐量:基于 Numba JIT 打平内联与连续 2D C-Contiguous 内存布局,零堆分配开销 (3000万+ Ticks/s)。
  • 通用多空机制buy / sell 天然支持做多与做空 (pos += amountpos -= amount),统一浮动资产计算 $PV = \text{Cash} + \sum \text{pos}_i \times \text{close}_i$。
  • 轻量动态复权架构data.close 为原始真实交割价,data.adj.close / ctx.adj.close 为动态懒求值复权视角。无复权需求或已有复权时零开销。
  • 多表与自定义列按需/懒加载 (LazyCustomFields):自动支持多表字段与自定义特征列(如因子 factor_bpe_ttmvwap 等)。支持 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

carrotquant_engine-1.0.4.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

carrotquant_engine-1.0.4-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

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

Hashes for carrotquant_engine-1.0.4.tar.gz
Algorithm Hash digest
SHA256 663fc71fb46021845cb5755321378d5395fb4b81caa39cb1ad3a20c867c03e14
MD5 7543d4a6b5eb90a3aaaea930802e0722
BLAKE2b-256 6b65004e4c140a21d57b36a00df740e7750cc0e34c6b8cadb3a03dac8d9eb454

See more details on using hashes here.

Provenance

The following attestation bundles were made for carrotquant_engine-1.0.4.tar.gz:

Publisher: workflow.yml on CRThu/carrotquant-engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file carrotquant_engine-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for carrotquant_engine-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 82b4a2d3f6c90226f96a779165ebb7a235c0e2d7802614f83aa87be2c9680e2a
MD5 ac960ef9c79945c719ca0d8c0e70b70f
BLAKE2b-256 c3079e6a0274c2404c5a590df27be9caad0ddf42f68cea5fe33e3ba31e113bbc

See more details on using hashes here.

Provenance

The following attestation bundles were made for carrotquant_engine-1.0.4-py3-none-any.whl:

Publisher: workflow.yml on CRThu/carrotquant-engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

1.3.0

2 files

1.2.1

2 files

1.2.0

2 files

1.1.1

2 files

1.1.0

2 files

1.0.5

2 files

This release

1.0.4 This release

2 files

1.0.3

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page