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

@@ -38,7 +38,9 @@ type HistorySummary struct {
func (s *PositionStore) GetHistorySummary(traderID string) (*HistorySummary, error) {
summary := &HistorySummary{}
fullStats, err := s.GetFullStats(traderID)
// Baseline 0 is fine here: the summary only reads counts/win-rate/PnL, not
// the drawdown that depends on it.
fullStats, err := s.GetFullStats(traderID, 0)
if err != nil {
return nil, err
}

View File

@@ -54,14 +54,16 @@ func (s *PositionStore) GetPositionStats(traderID string) (map[string]interface{
return stats, nil
}
// GetFullStats gets complete trading statistics
func (s *PositionStore) GetFullStats(traderID string) (*TraderStats, error) {
return s.GetFullStatsByTraderFilters([]string{traderID}, nil)
// GetFullStats gets complete trading statistics. startingEquity is the real
// account baseline used for the drawdown equity curve; pass 0 when unknown.
func (s *PositionStore) GetFullStats(traderID string, startingEquity float64) (*TraderStats, error) {
return s.GetFullStatsByTraderFilters([]string{traderID}, nil, startingEquity)
}
// GetFullStatsByTraderFilters gets complete trading statistics for explicit
// trader IDs plus optional legacy trader ID patterns.
func (s *PositionStore) GetFullStatsByTraderFilters(traderIDs []string, traderIDPatterns []string) (*TraderStats, error) {
// trader IDs plus optional legacy trader ID patterns. startingEquity is the
// real account baseline for the drawdown calculation; pass 0 when unknown.
func (s *PositionStore) GetFullStatsByTraderFilters(traderIDs []string, traderIDPatterns []string, startingEquity float64) (*TraderStats, error) {
stats := &TraderStats{}
var positions []TraderPosition
@@ -106,7 +108,7 @@ func (s *PositionStore) GetFullStatsByTraderFilters(traderIDs []string, traderID
stats.SharpeRatio = calculateSharpeRatioFromPnls(pnls)
}
if len(pnls) > 0 {
stats.MaxDrawdownPct = calculateMaxDrawdownFromPnls(pnls)
stats.MaxDrawdownPct = calculateMaxDrawdownFromPnls(pnls, startingEquity)
}
return stats, nil
@@ -192,13 +194,21 @@ func calculateSharpeRatioFromPnls(pnls []float64) float64 {
return mean / stdDev
}
// calculateMaxDrawdownFromPnls calculates maximum drawdown
func calculateMaxDrawdownFromPnls(pnls []float64) float64 {
// calculateMaxDrawdownFromPnls reconstructs an equity curve from the realized
// PnL sequence on top of startingEquity and returns the max peak-to-trough
// drawdown as a PERCENT (e.g. 18.5 = -18.5%). The baseline matters: the same
// $87 dip is 0.9% of a $10k account but 18% of a $480 one, so callers should
// pass the trader's real initial balance. A non-positive baseline falls back
// to a neutral $10k so the metric stays defined (but understated) when the
// account baseline is unknown.
func calculateMaxDrawdownFromPnls(pnls []float64, startingEquity float64) float64 {
if len(pnls) == 0 {
return 0
}
const startingEquity = 10000.0
if startingEquity <= 0 {
startingEquity = 10000.0
}
equity := startingEquity
peak := startingEquity
var maxDD float64

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")
}
}