From ed9417195c4a950b85e5edfc0a80da46156ccebd Mon Sep 17 00:00:00 2001 From: henrylab Date: Thu, 30 Oct 2025 22:23:05 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BF=AE=E5=A4=8Dhyperliquid=20=E6=80=BB?= =?UTF-8?q?=E7=9B=88=E4=BA=8F=EF=BC=8C=E6=80=BB=E5=87=80=E5=80=BC=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98=202.=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E6=8C=81=E4=BB=93=E7=9B=88=E4=BA=8F=E7=99=BE=E5=88=86?= =?UTF-8?q?=E6=AF=94=E9=94=99=E8=AF=AF=EF=BC=8C=E8=AE=A1=E7=AE=97=E5=85=AC?= =?UTF-8?q?=E5=BC=8F=E5=BA=94=E8=AF=A5=E5=8A=A0=E5=85=A5=E6=9D=A0=E6=9D=86?= =?UTF-8?q?=E5=80=8D=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trader/auto_trader.go | 20 ++++++++++---------- trader/hyperliquid_trader.go | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/trader/auto_trader.go b/trader/auto_trader.go index 42bc2e69..c181dc25 100644 --- a/trader/auto_trader.go +++ b/trader/auto_trader.go @@ -427,14 +427,6 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) { unrealizedPnl := pos["unRealizedProfit"].(float64) liquidationPrice := pos["liquidationPrice"].(float64) - // 计算盈亏百分比 - pnlPct := 0.0 - if side == "long" { - pnlPct = ((markPrice - entryPrice) / entryPrice) * 100 - } else { - pnlPct = ((entryPrice - markPrice) / entryPrice) * 100 - } - // 计算占用保证金(估算) leverage := 10 // 默认值,实际应该从持仓信息获取 if lev, ok := pos["leverage"].(float64); ok { @@ -443,6 +435,14 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) { marginUsed := (quantity * markPrice) / float64(leverage) totalMarginUsed += marginUsed + // 计算盈亏百分比 + pnlPct := 0.0 + if side == "long" { + pnlPct = ((markPrice - entryPrice) / entryPrice) * float64(leverage) * 100 + } else { + pnlPct = ((entryPrice - markPrice) / entryPrice) * float64(leverage) * 100 + } + // 跟踪持仓首次出现时间 posKey := symbol + "_" + side currentPositionKeys[posKey] = true @@ -873,9 +873,9 @@ func (at *AutoTrader) GetPositions() ([]map[string]interface{}, error) { pnlPct := 0.0 if side == "long" { - pnlPct = ((markPrice - entryPrice) / entryPrice) * 100 + pnlPct = ((markPrice - entryPrice) / entryPrice) * float64(leverage) * 100 } else { - pnlPct = ((entryPrice - markPrice) / entryPrice) * 100 + pnlPct = ((entryPrice - markPrice) / entryPrice) * float64(leverage) * 100 } marginUsed := (quantity * markPrice) / float64(leverage) diff --git a/trader/hyperliquid_trader.go b/trader/hyperliquid_trader.go index b8c8754f..fdd646e0 100644 --- a/trader/hyperliquid_trader.go +++ b/trader/hyperliquid_trader.go @@ -85,12 +85,12 @@ func (t *HyperliquidTrader) GetBalance() (map[string]interface{}, error) { result := make(map[string]interface{}) // 🔍 调试:打印API返回的完整CrossMarginSummary结构 - summaryJSON, _ := json.MarshalIndent(accountState.CrossMarginSummary, " ", " ") + summaryJSON, _ := json.MarshalIndent(accountState.MarginSummary, " ", " ") log.Printf("🔍 [DEBUG] Hyperliquid API CrossMarginSummary完整数据:") log.Printf("%s", string(summaryJSON)) - accountValue, _ := strconv.ParseFloat(accountState.CrossMarginSummary.AccountValue, 64) - totalMarginUsed, _ := strconv.ParseFloat(accountState.CrossMarginSummary.TotalMarginUsed, 64) + accountValue, _ := strconv.ParseFloat(accountState.MarginSummary.AccountValue, 64) + totalMarginUsed, _ := strconv.ParseFloat(accountState.MarginSummary.TotalMarginUsed, 64) // ⚠️ 关键修复:从所有持仓中累加真正的未实现盈亏 totalUnrealizedPnl := 0.0