程序员量化交易实战 41:把每日运行计划压成摘要

📅 2026/7/9 12:43:33
程序员量化交易实战 41:把每日运行计划压成摘要
第 40 篇已经有了DailyRunPlan它包含请求、结果、失败动作和动作汇总。但计划对象不适合直接打到日志里。生产环境里看日志时我们通常只需要先知道一句话今天能不能跑跑的是哪天有多少股票失败了几项。摘要对象第 41 章新增app/daily_run_summary.py。dataclass(frozenTrue) class DailyRunSummary: trade_date: str status: str dry_run: bool symbol_count: int failed_check_count: int action_summary: str executable: bool它不是替代DailyRunPlan而是从计划对象里提取一层“人第一眼要看的信息”。字段用途trade_date确认这次运行对应哪个交易日status快速判断 ready、dry-run-ready 或 blockedsymbol_count观察本次覆盖的股票数量是否异常failed_check_count不展开细节也能知道失败规模action_summary判断是 warning、blocker 还是无需处理executable是否允许真实执行从计划构造摘要摘要函数只读DailyRunPlan不重新计算业务规则。def build_daily_run_summary(plan: DailyRunPlan) - DailyRunSummary: return DailyRunSummary( trade_dateplan.result.trade_date, statusplan.result.status, dry_runplan.result.dry_run, symbol_countlen(plan.request.required_symbols), failed_check_countlen(plan.result.failed_checks), action_summaryplan.action_summary, executableplan_can_execute(plan), )这里继续复用plan_can_execute()。如果后面执行标准变化摘要层不用跟着复制判断逻辑。一行日志日志里最怕结构太散。第 41 章补了一个稳定格式def format_daily_run_summary(summary: DailyRunSummary) - str: return ( f{summary.trade_date} fstatus{summary.status} fsymbols{summary.symbol_count} ffailed_checks{summary.failed_check_count} factions{summary.action_summary} fexecutable{str(summary.executable).lower()} )blocked 示例输出2026-02-11 statusblocked symbols1 failed_checks1 actionsblocker executablefalse这行文字可以直接放进 CLI 输出、定时任务日志或告警消息。它不负责解释全部细节只负责让人知道接下来要不要点开 artifact。接到可运行示例本章的摘要对象已经接入专栏代码仓库里的章节示例命令。为了让第 41-45 篇共用同一个真实场景示例故意构造了一次data_gaps未通过的每日运行运行窗口正常、历史归档正常、运行健康正常只有行情数据检查失败。运行命令uv run python -m scripts.chapter_examples paper-command本章对应的输出如下这里最重要的是两段信息。第一段是一行摘要statusblocked、failed_checks1、actionsblocker、executablefalse。它适合放进日志和告警标题里让人不用展开 JSON 就知道今天不能真实执行。第二段是结构化字段dry_runFalse、symbol_count2、failed_check_count1。这类字段适合后续接入 CLI、任务平台或监控系统时继续拆分而不是靠字符串解析。摘要不是审计凭证。它只是入口层的“状态标题”。真正要排查为什么 blocked需要看下一篇落盘的 artifact。测试本章测试覆盖两件事ready 计划能正确统计去重后的股票数量。blocked 计划能输出稳定的一行摘要。运行命令uv run pytest tests/test_daily_run_summary.py tests/test_daily_run_plan.py本批次补充paper-command后全量测试通过276 passed, 2 warnings本章更新与代码仓库本章更新内容新增app/daily_run_summary.py。新增DailyRunSummary。实现build_daily_run_summary()。实现format_daily_run_summary()。新增tests/test_daily_run_summary.py覆盖 ready 与 blocked 摘要。在scripts/chapter_examples.py中接入paper-command可直接复现第 41-45 篇的运行链路。代码仓库https://github.com/ax2/zi-quant-platform本章代码git clone https://github.com/ax2/zi-quant-platform.git cd zi-quant-platform git checkout chapter-41-45-paper-command uv sync --extra dev uv run python -m scripts.chapter_examples paper-command uv run pytest tests/test_daily_run_summary.py tests/test_daily_run_plan.py tests/test_chapter_examples.py第 41-45 篇共用 tagchapter-41-45-paper-command。当前全量测试通过276 passed只有既有 FastAPI deprecation warning。本篇小结生产化不是只把核心逻辑写出来还要让运行状态能被快速看懂。第 41 篇把每日运行计划压缩成摘要。下一步要解决的是摘要之外的完整上下文怎么落盘出了问题以后能不能回放当时的请求、失败检查和处理动作。