diff --git a/.gitignore b/.gitignore index a62aaf0..5553f6b 100644 --- a/.gitignore +++ b/.gitignore @@ -64,4 +64,7 @@ CLAUDE.local.md .claude/docs/ # Local work-in-progress notes -TODO.md \ No newline at end of file +TODO.md + +# Results +results \ No newline at end of file diff --git a/README.md b/README.md index ed3dd76..99f3a17 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # hqbacktest - A股量化策略回测与交易模拟引擎

- + +

@@ -113,6 +114,17 @@ result.save("results/moving-average") 命令行(推荐用于 CI 与可复现实验): +`hqbacktest` 不下载数据,命令行示例需要你本地已有一份 `hqdata` CSV 快照。先用 `hqdata` CLI 抓一段真实日线(换成你自己的 tushare/ricequant token 和日期区间): + +```bash +hqdata --source tushare calendar --start 20260123 --end 20260212 +hqdata --source tushare stock-list --start 20260123 --end 20260212 +hqdata --source tushare stock-daily --start 20260123 --end 20260212 +hqdata --source tushare stock-factor --start 20260123 --end 20260212 +``` + +再跑仓库自带的 [`configs/moving_average.toml`](configs/moving_average.toml)(日期区间需要和你抓取的快照区间对上): + ```bash hqbacktest run --config configs/moving_average.toml --output results/moving-average ``` diff --git a/configs/moving_average.toml b/configs/moving_average.toml new file mode 100644 index 0000000..7bc4080 --- /dev/null +++ b/configs/moving_average.toml @@ -0,0 +1,26 @@ +# Example config for `hqbacktest run` using the `examples.moving_average` +# strategy against a real hqdata CSV snapshot. +# +# The date range below assumes you already have a `tushare` snapshot under +# `~/.hqdata/tushare` covering 20260123-20260212 (fetch it first with the +# `hqdata` CLI, see README "配置数据源"). If your snapshot covers a +# different range, change `start_date` / `end_date` accordingly -- they +# must fall within calendar.csv's trading days AND have stock_daily / +# stock_list / stock_factor CSVs on disk for every day in range. + +[start] +start_date = "20260123" # first trading day with data in your snapshot +end_date = "20260212" # last trading day with data in your snapshot + +[capital] +initial_cash = "100000" + +[data] +source = "tushare" # or "ricequant"; must match a snapshot under data_root +data_root = "~/.hqdata" + +[strategy] +module = "examples.moving_average" + +[output] +directory = "results/moving-average" diff --git a/docs/cli.md b/docs/cli.md index a953b7d..e78d353 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -12,6 +12,8 @@ hqbacktest run --config configs/moving_average.toml --output results/moving-aver python -m hqbacktest run --config configs/moving_average.toml --output results/moving-average ``` +`configs/moving_average.toml` 是仓库自带的示例配置,但 `hqbacktest` 不下载数据——运行前需要本地已有一份覆盖该日期区间的 `hqdata` CSV 快照(`hqdata` CLI 抓取步骤见 README「配置数据源」一节),否则会以退出码 2 报 `SnapshotFileMissingError`。 + ### 1.1 策略模块解析 `hqbacktest run` 把 config 文件所在目录和当前工作目录加入 `sys.path`,让 `[strategy].module = "my_strategy"` 这类**不带点号的写法**可以直接 `import` 成功,行为与 `python -m hqbacktest run` 一致。 diff --git a/src/hqbacktest/__init__.py b/src/hqbacktest/__init__.py index 7885c70..9e6f0cc 100644 --- a/src/hqbacktest/__init__.py +++ b/src/hqbacktest/__init__.py @@ -1,5 +1,7 @@ """hqbacktest - A-share quantitative strategy backtest and trading simulation engine.""" +from importlib.metadata import version + from hqbacktest.data import ( CacheKey, DataCache, @@ -51,7 +53,7 @@ TradingRuleSet, ) -__version__ = "0.1.4" +__version__ = version("hqbacktest") __all__ = [ "__version__",