Skip to content

feat: portfolio daily margin aggregation - #19

Merged
XucroYuri merged 4 commits into
mainfrom
goratio-dev-portfolio-margin
Sep 3, 2026
Merged

feat: portfolio daily margin aggregation#19
XucroYuri merged 4 commits into
mainfrom
goratio-dev-portfolio-margin

Conversation

@XucroYuri

Copy link
Copy Markdown
Owner

新增 portfolio_daily_margin,合并多笔持仓逐日盯市与保证金占用。

Copilot AI lite review requested due to automatic review settings September 3, 2026 00:12
@XucroYuri
XucroYuri merged commit 9707cbf into main Sep 3, 2026
11 checks passed
@XucroYuri
XucroYuri deleted the goratio-dev-portfolio-margin branch September 3, 2026 00:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.md and .planning/ROADMAP.md to 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 thread src/goratio/margin.py
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 thread tests/test_margin.py
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)
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.

2 participants