跳至正文
返回文库全部文档

在真实策略工作负载上比较 VectorBT 与 ML4T

代码 《交易机器学习》

总结

本笔记在受支持的案例研究策略上,将 VectorBT Pro 和 VectorBT OSS 与 ML4T 进行正确性与运行时间对比。两个版本均涵盖 ETF 配置、以 USD 报价的外汇以及 US 股票面板;VectorBT Pro 还参与了 CME 期货比较。审计会检查成交、估值、时间戳和账户价值差异,并对货币金额、数量及其他数值字段采用不同容差。笔记还报告仅引擎运行时间,以及合成规模压力比较的结果。

资产模型支持情况决定了哪些比较有意义:OSS 缺少 CME 工作负载所需的原生期货乘数和保证金模型;加密货币永续合约资金费与保证金核算也未使用任何 VectorBT 版本。测得的运行时间不包括数据加载、目标构建、适配器准备和结果提取。因此,计时结果仅反映经过测试的引擎调用、数据集、软件版本和机器,并非通用速度排名,也不能证明支持未纳入的资产类型。

核心观点

  • 一致性检查依据明确的精度规则比较成交、估值、时间戳和货币结果。
  • VectorBT OSS 未纳入 CME 期货比较,因为它缺少所需的乘数和保证金模型。
  • 两个 VectorBT 版本都未用于加密货币永续合约资金费和保证金核算评估。
  • 仅引擎计时不包括准备和输出工作,因此运行时间比较的结论范围有限。
  • 通过合成规模比较并不意味着支持此前不受支持的资产核算模型。

标签

全文
# 18_vectorbt_engine_parity.py


```py
# ---
# jupyter:
#   jupytext:
#     cell_metadata_filter: tags,-all
#     text_representation:
#       extension: .py
#       format_name: percent
#       format_version: '1.3'
#       jupytext_version: 1.19.3
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# %% [markdown]
# # VectorBT Pro and OSS on Current Case-Study Strategies
#
# This notebook reports the VectorBT Pro and VectorBT OSS rows from the current real-strategy audit.
# Required comparisons use ETF, CME futures, USD-quoted foreign-exchange, and US equity-panel target
# streams where the pinned VectorBT edition supports the asset and accounting contract.
#
# **Learning objectives**
#
# - Compare VectorBT Pro and OSS with ML4T on supported real-data workloads
# - Distinguish fill precision from the monetary unit used for account values
# - Understand why VectorBT OSS is not used for the CME futures contract
# - Read engine-only runtime evidence without treating it as a universal ranking
#
# **Book reference**: Chapter 16, Section 16.3

# %% [markdown]
# ## Setup

# %%
"""Current VectorBT parity evidence."""

import json

import polars as pl
from IPython.display import display

from utils.paths import get_chapter_dir

# %% tags=["parameters"]
# Production defaults - Papermill injects overrides after this cell
ROUND_SECONDS = 3

# %%
AUDIT_PATH = get_chapter_dir(16) / "resources" / "framework_parity_audit.json"
audit = json.loads(AUDIT_PATH.read_text(encoding="utf-8"))
FRAMEWORKS = ["vectorbt_pro", "vectorbt_oss"]
FRAMEWORK_NAMES = {
    key: f"{audit['frameworks'][key]['display_name']} {audit['frameworks'][key]['version']}"
    for key in FRAMEWORKS
}
CASE_NAMES = {
    "etfs": "ETF allocation",
    "cme_futures": "CME futures",
    "crypto_perps_funding": "Crypto perpetual funding",
    "fx_pairs": "FX allocation (USD-quoted pairs)",
    "us_equities_panel": "US equity panel",
}

# %% [markdown]
# ## 1. Required comparisons

# %% tags=["results"]
results = (
    pl.DataFrame(audit["real_strategy_records"])
    .filter(pl.col("framework").is_in(FRAMEWORKS))
    .with_columns(
        pl.col("case_study").replace_strict(CASE_NAMES).alias("strategy"),
        pl.col("framework").replace_strict(FRAMEWORK_NAMES).alias("engine"),
    )
    .select(
        "strategy",
        "engine",
        "status",
        "fills",
        "valuations",
        "valuation_timestamps_match",
        "equity_gap",
        "equity_raw_gap",
        "terminal_gap",
        "terminal_raw_gap",
    )
    .sort("strategy", "engine")
)

assert results.height == 7
assert results.filter(pl.col("status") == "pass").height == 7
assert results["valuation_timestamps_match"].all()

display(results)

# %% [markdown]
# Both VectorBT editions participate in the ETF, USD-quoted foreign-exchange, and US equity-panel
# comparisons. VectorBT Pro also supplies contract multipliers and futures-style leverage for the
# CME comparison. Fill prices retain eight-decimal precision, quantities retain five-decimal
# precision, and account monetary values must round to the same cent.

# %% [markdown]
# ## 2. Unsupported asset models

# %% tags=["results"]
unsupported = (
    pl.DataFrame(audit["unsupported_records"])
    .filter(pl.col("framework").is_in(FRAMEWORKS))
    .with_columns(
        pl.col("case_study").replace_strict(CASE_NAMES).alias("strategy"),
        pl.col("framework").replace_strict(FRAMEWORK_NAMES).alias("engine"),
    )
    .select("strategy", "engine", "reason")
    .sort("strategy", "engine")
)
display(unsupported)

# %% [markdown]
# VectorBT OSS does not provide the native multiplier and margin-account model required by the CME
# bundle. Neither edition is used to emulate crypto-perpetual funding and margin accounting.

# %% [markdown]
# ## 3. Engine-only timing
#
# Each correctness-passing VectorBT row has an engine-only timing record.

# %% tags=["results"]
timing = (
    pl.DataFrame(audit["performance_records"])
    .filter(pl.col("framework").is_in(FRAMEWORKS))
    .with_columns(
        pl.col("case_study").replace_strict(CASE_NAMES).alias("strategy"),
        pl.col("framework").replace_strict(FRAMEWORK_NAMES).alias("engine"),
        pl.col("framework_median_seconds").round(ROUND_SECONDS).alias("vectorbt_seconds"),
        pl.col("ml4t_median_seconds").round(ROUND_SECONDS).alias("ml4t_seconds"),
        pl.col("framework_to_ml4t_ratio").round(2).alias("vectorbt_div_ml4t"),
    )
    .select("strategy", "engine", "vectorbt_seconds", "ml4t_seconds", "vectorbt_div_ml4t")
)

assert timing.height == 7
display(timing)

# %% [markdown]
# The measured region excludes data loading, target construction, adapter preparation, and output
# extraction. The observed ratios do not establish the same relationship for other datasets,
# strategy mechanics, or machines.

# %% [markdown]
# ## 4. Synthetic stress evidence

# %% tags=["results"]
stress = (
    pl.DataFrame(audit["synthetic_stress"]["records"])
    .filter(pl.col("framework").is_in(FRAMEWORKS))
    .with_columns(pl.col("framework").replace_strict(FRAMEWORK_NAMES).alias("engine"))
    .select("engine", "intents", "fills", "trades", "terminal_value", "status")
)
display(stress)

# %% [markdown]
# VectorBT Pro and OSS both pass the generated stress comparison against their matching ML4T
# profiles. This establishes scale conformance for the fixed target-order recipe. It does not create
# support for the excluded asset models.

```

在遵守原作品许可的前提下,附作者信息全文展示。 许可协议: MIT

此摘要由 Stratmill 研究智能体根据原文撰写,并非原文副本。