From 03a724f7eae497cd6f4f9e921d74b74490fb40a1 Mon Sep 17 00:00:00 2001 From: Jarno Date: Tue, 11 Aug 2026 21:22:21 +0300 Subject: [PATCH] Fixed a UI bug calculating equity percentage incorrectly. --- src/trading_bot/ui/dashboard.py | 8 ++------ tests/test_dashboard.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/trading_bot/ui/dashboard.py b/src/trading_bot/ui/dashboard.py index bfeddba..9cd985f 100644 --- a/src/trading_bot/ui/dashboard.py +++ b/src/trading_bot/ui/dashboard.py @@ -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( "" f"{escape(row.symbol)}" f"{_format_number(row.quantity)}" f"{_format_currency(row.market_value)}" - f"{_format_percent(row.market_value / latest_account_value if latest_account_value else None)}" + f"{_format_percent(row.market_value / current_portfolio_value if current_portfolio_value else None)}" f"{_format_currency(row.average_entry_price)}" f"{_format_currency(row.current_price)}" f"{_format_currency(row.unrealized_pl)}" diff --git a/tests/test_dashboard.py b/tests/test_dashboard.py index 571e96a..f349d58 100644 --- a/tests/test_dashboard.py +++ b/tests/test_dashboard.py @@ -139,3 +139,22 @@ def test_render_dashboard_contains_requested_sections() -> None: assert "FILLED" 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