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

@@ -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