diff --git a/README.md b/README.md index 72147d6..9b89afb 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,8 @@ skfolio、OptimalPortfolios和qis已通过真实库调用测试。AKShare适配 下一步开发顺序与验收条件见[开发路线](docs/ROADMAP.md),平台对接见[接口契约](docs/INTEGRATION.md)。 +完整研究导出同时保留`platform-snapshot.json`。在干净Git检出下,还通过quant-lab发布`standard/v2`的`research`适配视图,可供统一校验、索引和Report Hub只读展示;其中`investable=false`、`rankable=false`,不等同于完整执行账本认证。适配器不替代原始申赎、应收款和每日资金对账,也不让合成样例变成真实FOF业绩。 + ## 验证 ```powershell diff --git a/docs/INTEGRATION.md b/docs/INTEGRATION.md index 2bd1c7e..e514be2 100644 --- a/docs/INTEGRATION.md +++ b/docs/INTEGRATION.md @@ -39,6 +39,10 @@ payload = platform_snapshot(result, data) 对接验收:平台收到的资产分项之和应等于total_value;`mode`不能被忽略;每条目标保留时点;不能把预计流动性金额展示为已可用现金。接入后再考虑封装REST API和调度,不在首版引入第二个运行服务。 -## Exploratory standard/v2 export +## 探索性standard/v2导出 -A clean Git checkout additionally publishes a `standard/v2` research profile through the pinned quant-lab adapter. It is always `investable=false` and `rankable=false`. A dirty or unavailable checkout retains the original report without claiming a clean code revision. Dataset identities use content hashes, and date-only NAV observations are stamped at the end of their UTC day. This profile does not certify a historical universe or replace the original accounting evidence. +干净Git检出通过固定的quant-lab适配器额外发布`standard/v2`的`research`视图,始终标记`investable=false`和`rankable=false`。检出不干净或Git身份不可用时保留原报告,不声称干净提交身份。数据身份使用内容哈希,只有日期的净值观测标记为对应UTC日末;该视图不认证历史股票池或替代原始会计证据。 + +使用`quant-lab validate --run-dir <研究目录>`核验后,可扫描到独立实验索引,由Report Hub按索引位置重新验证产物并展示。索引是可重建缓存;来源损坏时应显示不可用,不能使用旧缓存指标。`research`视图中的汇总NAV不能承担完整QExec现金分录、订单和成交链路的精确归因。 + +v2的`metrics.json`保留原报告的全部指标,并添加数据分类、估值口径和`backtest_stats`展示行。完整估值区间使用首末已知估值及完整路径回撤,年化与Sharpe留空;完整月末收益样本另行展示收益、月末回撤和按12期年化的指标,两行不能混合使用。陈旧估值可能低估风险;没有新增基金基准或将低频净值插值成日收益。`config.json`的`study_config`保留原回测配置。 diff --git a/src/quant_fund/report.py b/src/quant_fund/report.py index 85f66b3..f36a2a3 100644 --- a/src/quant_fund/report.py +++ b/src/quant_fund/report.py @@ -111,7 +111,48 @@ def export_run(result, dataset, source_directory, output): dataset_snapshots={"dataset": dataset.fingerprint}, nav=levels[["date", "nav"]], comparability="not_historical_universe", - config={"currency": "CNY", "calendar": "synthetic_or_supplied"}, + metrics={ + **metrics, + "evidence_kind": dataset.metadata["classification"], + "measurement_basis": { + "period_start": str(nav.index[0].date()), + "period_end": str(nav.index[-1].date()), + "currency": "CNY", + "monthly_annualization_periods": 12, + "sharpe_risk_free_rate": 0, + "valuation": manifest["valuation"], + "monthly_sample": "相邻完整月末已知估值的收益;不含首个非完整月区间", + "first_return_month_end": str(monthly.index[0].date()) + if len(monthly) + else None, + "last_return_month_end": str(monthly.index[-1].date()) + if len(monthly) + else None, + "benchmark": None, + }, + "backtest_stats": [ + { + "portfolio": "完整估值区间(已知估值)", + "total_return": metrics["full_period_return"], + "ann_return": None, + "sharpe": None, + "max_drawdown": metrics["full_period_max_drawdown"], + }, + { + "portfolio": "完整月末收益样本(年化12期)", + "total_return": metrics["total_return"], + "ann_return": metrics["annual_return"], + "sharpe": metrics["sharpe"], + "max_drawdown": metrics["max_drawdown"], + "observations": metrics["observations"], + }, + ], + }, + config={ + "currency": "CNY", + "calendar": "synthetic_or_supplied", + "study_config": result["config"], + }, ) manifest["files"] = { str(p.relative_to(root)).replace("\\", "/"): hashlib.sha256(p.read_bytes()).hexdigest() diff --git a/tests/test_research.py b/tests/test_research.py index 5bed461..d704cc9 100644 --- a/tests/test_research.py +++ b/tests/test_research.py @@ -88,6 +88,20 @@ def test_end_to_end_report_reconciles_and_detects_tampering(demo_directory, tmp_ published = load_and_validate_run_v2(root) assert published.tags["rankable"] == "false" assert published.dataset_snapshots["dataset"] == data.fingerprint + native_metrics = json.loads((root / "metrics.json").read_text()) + exported_metrics = json.loads((root / "standard/v2/metrics.json").read_text(encoding="utf-8")) + assert all(exported_metrics[key] == value for key, value in native_metrics.items()) + assert exported_metrics["evidence_kind"] == "synthetic" + full_period, month_ends = exported_metrics["backtest_stats"] + assert full_period["total_return"] == native_metrics["full_period_return"] + assert full_period["max_drawdown"] == native_metrics["full_period_max_drawdown"] + assert full_period["ann_return"] is None and full_period["sharpe"] is None + assert month_ends["total_return"] == native_metrics["total_return"] + assert month_ends["ann_return"] == native_metrics["annual_return"] + assert month_ends["sharpe"] == native_metrics["sharpe"] + assert month_ends["max_drawdown"] == native_metrics["max_drawdown"] + assert month_ends["observations"] == native_metrics["observations"] + assert exported_metrics["measurement_basis"]["monthly_annualization_periods"] == 12 exported_nav = pd.read_parquet(root / "standard/v2/returns.parquet") assert exported_nav.nav_units.iloc[0] / 10000 == pytest.approx(nav.total_value.iloc[0]) assert verify_run(root)["max_reconciliation_error"] < 1e-6 @@ -100,6 +114,24 @@ def test_end_to_end_report_reconciles_and_detects_tampering(demo_directory, tmp_ verify_run(root) +@pytest.mark.parametrize("end,observations", [("2024-01-10", 0), ("2024-02-29", 1)]) +def test_short_reports_preserve_missing_monthly_metrics( + demo_directory, tmp_path, monkeypatch, end, observations +): + monkeypatch.setattr("quant_fund.report.clean_git_commit", lambda root: "a" * 40) + data = Dataset.load(demo_directory) + result = run_backtest(data, BacktestConfig(start="2024-01-02", end=end, strategy="equal")) + root = export_run(result, data, demo_directory, tmp_path / "short-report") + published = json.loads((root / "standard/v2/metrics.json").read_text(encoding="utf-8")) + full, monthly = published["backtest_stats"] + assert isinstance(full["total_return"], float) + assert monthly["observations"] == observations + assert all( + monthly[key] is None for key in ("total_return", "ann_return", "sharpe", "max_drawdown") + ) + assert verify_run(root)["max_reconciliation_error"] < 1e-6 + + def test_empty_portfolio_still_reports_confirmed_receivables(make_dataset): from quant_fund.engine import Ledger