Skip to content

Commit a58be23

Browse files
committed
fix: 修正盈亏百分比计算错误(未考虑杠杆)
问题: - 旧算法只计算价格变化百分比,未考虑杠杆倍数 - 例:10倍杠杆,价格涨1% → 显示+1%(错误),实际应该是+10% - 导致 AI 收到错误的盈亏数据,影响决策 修复: - 使用实际盈亏除以保证金计算百分比 - 公式:pnlPct = (unrealizedPnl / marginUsed) * 100 - 优点: ✓ 考虑杠杆倍数 ✓ 使用 API 提供的实际盈亏(含手续费、资金费率) ✓ 更准确反映真实盈亏百分比 示例对比: - 入场:100,000,当前:101,000(+1%),10倍杠杆 - 旧算法:显示 +1%(错误) - 新算法:显示 +10%(正确) 感谢用户反馈 @1711z 🤖 Generated with Claude Code
1 parent f02c5df commit a58be23

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

trader/auto_trader.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -484,20 +484,18 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) {
484484
unrealizedPnl := pos["unRealizedProfit"].(float64)
485485
liquidationPrice := pos["liquidationPrice"].(float64)
486486

487-
// 计算盈亏百分比
488-
pnlPct := 0.0
489-
if side == "long" {
490-
pnlPct = ((markPrice - entryPrice) / entryPrice) * 100
491-
} else {
492-
pnlPct = ((entryPrice - markPrice) / entryPrice) * 100
493-
}
494-
495487
// 计算占用保证金(估算)
496488
leverage := 10 // 默认值,实际应该从持仓信息获取
497489
if lev, ok := pos["leverage"].(float64); ok {
498490
leverage = int(lev)
499491
}
500492
marginUsed := (quantity * markPrice) / float64(leverage)
493+
494+
// 计算盈亏百分比(基于实际盈亏和保证金)
495+
pnlPct := 0.0
496+
if marginUsed > 0 {
497+
pnlPct = (unrealizedPnl / marginUsed) * 100
498+
}
501499
totalMarginUsed += marginUsed
502500

503501
// 跟踪持仓首次出现时间

0 commit comments

Comments
 (0)