feat: portfolio risk constraint gates - #21
Merged
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
check_portfolio_constraints() 对回撤值的度量单位与同模块现有摘要输出不一致且缺少 initial_capital 校验,可能导致门控结果错误或触发除零异常。
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
此 PR 在 goratio.margin 中新增组合级风控门控函数 check_portfolio_constraints(),用于输出保证金占用率与回撤相关的风控 gate 结果,并补齐对应单元测试与文档记录,方便在组合研究/回测摘要上做约束检查。
Changes:
- 新增
check_portfolio_constraints():计算保证金占用率与回撤门控结果并返回结构化 gate 信息 - 新增对应单测
test_check_portfolio_constraints - 更新
CHANGELOG.md与.planning/ROADMAP.md记录新增能力与测试数
File summaries
| File | Description |
|---|---|
src/goratio/margin.py |
新增组合级约束检查函数,输出保证金占用与回撤门控结果 |
tests/test_margin.py |
增加 check_portfolio_constraints() 的单元测试覆盖 |
CHANGELOG.md |
在变更日志中补充新增 API 记录 |
.planning/ROADMAP.md |
Roadmap 勾选新增功能并更新测试通过数量 |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+369
to
+391
| """组合级风控门控:保证金占用率与回撤上限。""" | ||
| daily_rows = portfolio_summary.get("daily_rows", []) | ||
| if daily_rows: | ||
| max_margin = max( | ||
| row["total_margin"] / portfolio_summary.get("initial_capital", 1) | ||
| for row in daily_rows | ||
| ) | ||
| else: | ||
| max_margin = None | ||
| drawdown = portfolio_summary.get("max_drawdown") | ||
| return { | ||
| "max_margin_utilization": max_margin, | ||
| "max_margin_utilization_threshold": max_margin_utilization, | ||
| "margin_utilization_passed": bool( | ||
| max_margin is not None and max_margin <= max_margin_utilization | ||
| ), | ||
| "max_drawdown": drawdown, | ||
| "max_drawdown_threshold": max_drawdown, | ||
| "drawdown_passed": bool( | ||
| drawdown is not None and drawdown <= max_drawdown | ||
| ), | ||
| "note": "组合风控门控研究,不构成投资建议", | ||
| } |
Comment on lines
+249
to
+252
| gates = check_portfolio_constraints(summary) | ||
|
|
||
| self.assertTrue(gates["margin_utilization_passed"]) | ||
| self.assertTrue(gates["drawdown_passed"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
新增 check_portfolio_constraints,输出保证金占用与回撤风控门控。