Skip to content

fix(alerts): 组合资产归零时触发完整回撤提醒 - #151

Merged
helsome merged 1 commit into
helsome:mainfrom
wxrbyte:codex/fix-drawdown-zero
Sep 21, 2026
Merged

helsome merged 1 commit into
helsome:mainfrom
wxrbyte:codex/fix-drawdown-zero

Conversation

@wxrbyte

@wxrbyte wxrbyte commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

问题与复现

Closes #150

启用 portfolio_drawdown(阈值 10%),先观测 totalAssets = 1000 建立峰值,再明确返回 totalAssets = 0。预期产生 100% 回撤提醒,原实现返回 null,既不通知也不保存事件;1000 → 100 的 90% 回撤则正常。

根因:evaluateDrawdown 把 totalAssets <= 0 全部过滤掉,但回撤计算的分母是历史正数峰值,当前资产为 0 仍是有效终点。

修改范围

  • 允许有限、非负的资产值参与回撤计算;明确归零且已有正数峰值时正常触发。
  • 初始零值不建立零峰值;未知、非有限值和负资产继续跳过。无效峰值遇到正数观测时重新初始化,避免无效分母。
  • 新增 9 项回归测试,覆盖 90% / 100% 回撤、缺失及非有限值、负数、初始零值和后续正数峰值;通过真实 JsonFileStore、规则仓库与 AlertEngine 重启验证归零事件落盘和通知。

仅修改回撤评估器和对应测试,无可见 UI 变化;不改出入金语义、冷却规则、其他提醒或持久化格式,不夹带基线修复。

验证

环境:Windows / Bun 1.4.2(744846f84),基于 main 1c1f6cd

先添加完整回归测试、保留旧实现:

  • bun test packages/shared/src/alerts/drawdown-zero.test.ts:5 pass / 4 fail,28 个断言。失败覆盖归零、初始零值后归零、NaN 和重启后的通知。

修复后:

  • bun test packages/shared/src/alerts:56 pass / 0 fail,144 个断言,5 个文件。
  • bun test packages/shared --isolate:1051 pass / 0 fail,4097 个断言,97 个文件。
  • bun run typecheck:core / i18n / shared / ui / electron 全部 exit 0。
  • git diff --check:通过。

上述集成使用模拟 portfolio.summary 能力和真实临时文件持久化,不调用券商或外部模型。未在本地执行全仓 bun test --isolate、桌面 E2E;全仓测试由远端 PR checks 提供。远端 PR checks:Typecheck / Focused tests / Secret scan / Full unit tests (advisory) 均为 SUCCESS;仅用于 main push 的 Full unit tests 按工作流条件跳过。

@wxrbyte
wxrbyte marked this pull request as ready for review September 20, 2026 14:14
@helsome
helsome merged commit 5584794 into helsome:main Sep 21, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Alerts] 组合资产明确归零时跳过 100% 回撤提醒

2 participants