fix: dashboard metrics — real drawdown baseline, realized/unrealized split, risk radar fields

- Max drawdown was double-broken: the backend built the equity curve on a
  hardcoded $10k baseline (understating a small account's drawdown ~20x) and
  the frontend multiplied the already-percent value by 100 again (0.87% shown
  as -86.9%). The curve now starts from the trader's real initial balance
  (10k fallback when unknown) and the UI renders the percent once; the demo
  engine and type docs are aligned to percent semantics.
- Header now separates equity-based 'Total P/L (incl. unrealized)' from
  'Realized P/L (closed trades)' so it no longer contradicts profit factor /
  win rate, and the stats strip shows the fee-drag chain
  (gross - fees = net) plus a per-trade-labelled sharpe.
- Risk radar read a non-existent account field (total_unrealized_profit) so
  unrealized PnL always showed $0; small PnLs also render with cents now.
- Nav 'Connect Hyperliquid' turns into a green connected chip when the server
  already holds a fully-authorized exchange, instead of nagging forever from
  a browser without the localStorage flow state.
This commit is contained in:
tinkle-community
2026-07-06 00:01:44 +09:00
parent ee5917adc6
commit 026c5d3089
12 changed files with 116 additions and 28 deletions

View File

@@ -126,6 +126,7 @@ func TestGetClosedPositionsByTraderFiltersIncludesLegacyAutopilotIDs(t *testing.
stats, err := positions.GetFullStatsByTraderFilters(
[]string{"current-trader"},
[]string{"%_user-123_claw402_%"},
0,
)
if err != nil {
t.Fatalf("get stats: %v", err)
@@ -134,3 +135,23 @@ func TestGetClosedPositionsByTraderFiltersIncludesLegacyAutopilotIDs(t *testing.
t.Fatalf("unexpected stats: trades=%d pnl=%.2f", stats.TotalTrades, stats.TotalPnL)
}
}
func TestCalculateMaxDrawdownUsesRealBaseline(t *testing.T) {
// +50 then -100: peak 550, trough 450 on a 500 account → 100/550 ≈ 18.18%.
pnls := []float64{50, -100}
got := calculateMaxDrawdownFromPnls(pnls, 500)
if got < 18.1 || got > 18.3 {
t.Fatalf("expected ~18.18%% drawdown on a 500 baseline, got %.2f", got)
}
// Unknown baseline falls back to the neutral 10k curve: 100/10050 ≈ 1%.
fallback := calculateMaxDrawdownFromPnls(pnls, 0)
if fallback < 0.9 || fallback > 1.1 {
t.Fatalf("expected ~1%% drawdown on the 10k fallback, got %.2f", fallback)
}
if calculateMaxDrawdownFromPnls(nil, 500) != 0 {
t.Fatalf("no trades must mean zero drawdown")
}
}