feat: portfolio daily margin aggregation - #19
Merged
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The current portfolio aggregation sums per-position cumulative P&L per day, which can drop closed-position P&L and produce incorrect final_equity/drawdown results.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR introduces a new portfolio_daily_margin() helper to aggregate multiple positions’ daily mark-to-market P&L and margin usage into a portfolio-level daily series, and documents the feature in project notes/changelog.
Changes:
- Add
portfolio_daily_margin()to aggregate per-position daily mark rows into a portfolio summary. - Add a unit test covering the new API.
- Update
CHANGELOG.mdand.planning/ROADMAP.mdto reflect the new feature and test count.
File summaries
| File | Description |
|---|---|
src/goratio/margin.py |
Adds portfolio-level daily aggregation wrapper on top of run_daily_position_mark(). |
tests/test_margin.py |
Adds a unit test for portfolio_daily_margin(). |
CHANGELOG.md |
Documents the new portfolio_daily_margin() API in the changelog list. |
.planning/ROADMAP.md |
Marks the roadmap item as done and updates the stated passing test count. |
Review details
Suppressed comments (1)
src/goratio/margin.py:351
- After changing aggregation to daily P&L deltas, the equity curve should be computed as a running sum of daily P&L (so closed positions remain reflected in later equity). Right now
equity = initial_capital + record["pnl"]assumes the per-day bucket already contains portfolio cumulative P&L, which isn't true once you switch to per-day deltas and is also what causes the current implementation to forget exited positions.
daily_rows = []
peak = initial_capital
max_drawdown = 0.0
for d in sorted(daily_map):
record = daily_map[d]
equity = initial_capital + record["pnl"]
peak = max(peak, equity)
max_drawdown = max(max_drawdown, peak - equity)
daily_rows.append(
{
"date": d,
"total_margin": record["margin"],
"total_pnl": record["pnl"],
"equity": equity,
"position_count": record["count"],
}
)
- 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
+315
to
+334
| from collections import defaultdict | ||
| from datetime import date | ||
|
|
||
| daily_map = defaultdict(lambda: {"margin": 0.0, "pnl": 0.0, "count": 0}) | ||
| rows_by_position = [] | ||
| for position in positions: | ||
| result = run_daily_position_mark( | ||
| records, | ||
| instrument=position["instrument"], | ||
| entry_date=position["entry_date"], | ||
| exit_date=position["exit_date"], | ||
| direction=position["direction"], | ||
| lots=position["lots"], | ||
| ) | ||
| rows_by_position.append(result) | ||
| for row in result["rows"]: | ||
| d = row["date"] | ||
| daily_map[d]["margin"] += row["margin_estimate"] | ||
| daily_map[d]["pnl"] += row["cumulative_pnl"] | ||
| daily_map[d]["count"] += 1 |
Comment on lines
+221
to
+235
| positions = [ | ||
| { | ||
| "instrument": "gold", | ||
| "entry_date": date(2024, 1, 2), | ||
| "exit_date": date(2024, 1, 4), | ||
| "direction": 1, | ||
| "lots": 1, | ||
| } | ||
| ] | ||
|
|
||
| summary = portfolio_daily_margin(records, positions) | ||
|
|
||
| self.assertEqual(summary["day_count"], 3) | ||
| self.assertGreater(summary["final_equity"], 100000.0) | ||
| self.assertGreater(summary["daily_rows"][0]["total_margin"], 0) |
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.
新增 portfolio_daily_margin,合并多笔持仓逐日盯市与保证金占用。