Fix: Resolve Trade History data loss and P&L calculation errors

Major fixes:
1. Trade History data loss issue
   - Root cause: Open records outside analysis window caused close matching failures
   - Solution: Pre-populate position state by reading 3x window of historical records
   - Ensures long-term positions (>5 hours) generate correct trade records
2. P&L calculation errors
   - Remove incorrect leverage multiplication from absolute P&L
   - Correct calculation: Futures P&L = quantity × price difference
   - Leverage only affects P&L percentage (relative to margin)
3. Other fixes
   - Break-even trades (pnl=0) no longer misclassified as losses
   - Perfect strategy shows Profit Factor as 999.0 instead of 0.0
   - Expand analysis window from 20 to 100 cycles (5 hours)
Files changed:
- logger/decision_logger.go: Core matching and calculation logic
- api/server.go: API analysis window
- trader/auto_trader.go: AI decision analysis window
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
This commit is contained in:
tinkle-community
2025-10-30 17:58:25 +08:00
parent e249aaa16b
commit fd8b1477e7
3 changed files with 66 additions and 16 deletions

View File

@@ -388,8 +388,9 @@ func (s *Server) handlePerformance(c *gin.Context) {
return
}
// 分析最近20个周期的交易表现
performance, err := trader.GetDecisionLogger().AnalyzePerformance(20)
// 分析最近100个周期的交易表现(避免长期持仓的交易记录丢失)
// 假设每3分钟一个周期100个周期 = 5小时足够覆盖大部分交易
performance, err := trader.GetDecisionLogger().AnalyzePerformance(100)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("分析历史表现失败: %v", err),