Fixed a UI bug calculating equity percentage incorrectly.

This commit is contained in:
2026-08-11 21:22:21 +03:00
parent 1faca2cdda
commit 03a724f7ea
2 changed files with 21 additions and 6 deletions
+2 -6
View File
@@ -533,17 +533,13 @@ def render_dashboard(snapshot: DashboardSnapshot) -> str:
if snapshot.warning
else ""
)
latest_account_value = (
snapshot.performance[-1].account_value
if snapshot.performance
else sum(row.market_value for row in snapshot.portfolio)
)
current_portfolio_value = sum(row.market_value for row in snapshot.portfolio)
portfolio_rows = "\n".join(
"<tr>"
f"<td>{escape(row.symbol)}</td>"
f"<td>{_format_number(row.quantity)}</td>"
f"<td>{_format_currency(row.market_value)}</td>"
f"<td>{_format_percent(row.market_value / latest_account_value if latest_account_value else None)}</td>"
f"<td>{_format_percent(row.market_value / current_portfolio_value if current_portfolio_value else None)}</td>"
f"<td>{_format_currency(row.average_entry_price)}</td>"
f"<td>{_format_currency(row.current_price)}</td>"
f"<td>{_format_currency(row.unrealized_pl)}</td>"
+19
View File
@@ -139,3 +139,22 @@ def test_render_dashboard_contains_requested_sections() -> None:
assert "FILLED</span>" in html
assert "orderside.sell" not in html
assert "orderstatus.filled" not in html
def test_portfolio_percentage_uses_current_portfolio_total() -> None:
html = render_dashboard(
DashboardSnapshot(
refreshed_at=datetime(2026, 8, 11, 10, 0, tzinfo=UTC),
performance=[
PerformancePoint(date(2026, 8, 11), 10_000.0, 10_000.0),
],
portfolio=[
PortfolioRow("CASH", "cash", None, 100.0, None, None, None),
PortfolioRow("SPY", "us_equity", 10.0, 10_000.0, 900.0, 1_000.0, 1_000.0),
],
trades=[],
)
)
assert "99.01%" in html
assert "100.00%" not in html