Skip to content

Conversation

@wz14
Copy link

@wz14 wz14 commented Dec 28, 2025

📝 Description | 描述

English: Fixed inaccurate position holding duration calculation in backtest mode. Previously, the system was using current system time instead of backtest simulation time, causing incorrect duration displays.

中文: 修复回测模式下持仓时长计算不准确的问题。之前系统使用的是当前系统时间而非回测模拟时间,导致持仓时长显示错误。


🎯 Type of Change | 变更类型

  • 🐛 Bug fix | 修复 Bug
  • ✨ New feature | 新功能
  • 💥 Breaking change | 破坏性变更
  • ♻️ Refactoring | 重构
  • ⚡ Performance improvement | 性能优化
  • 🔒 Security fix | 安全修复
  • 🔧 Build/config change | 构建/配置变更

🔗 Related Issues | 相关 Issue

  • Fixes backtest position holding duration calculation | 修复回测持仓时长计算问题

📋 Changes Made | 具体变更

English:

  • Added CurrentTimestampMs field to decision.Context struct to store current backtest timestamp
  • Updated buildDecisionContext() in backtest/runner.go to set the current timestamp for accurate holding duration calculation
  • Modified convertPositions() to use position open time (pos.OpenTime) instead of system time (time.Now()) for UpdateTime field
  • Enhanced formatPositionInfo() in decision/engine.go to use context timestamp for backtest mode, falling back to system time for live trading

中文:

  • decision.Context 结构体中添加 CurrentTimestampMs 字段,用于存储当前回测时间戳
  • 更新 backtest/runner.go 中的 buildDecisionContext() 函数,设置当前时间戳以准确计算持仓时长
  • 修改 convertPositions() 函数,使用持仓开仓时间(pos.OpenTime)而非系统时间(time.Now())作为 UpdateTime 字段值
  • 优化 decision/engine.go 中的 formatPositionInfo() 函数,回测模式使用上下文时间戳,实盘交易模式回退到系统时间

🧪 Testing | 测试

Test Environment | 测试环境

  • OS | 操作系统: macOS
  • Go Version | Go 版本: (请填写)
  • Exchange | 交易所: N/A (backtest mode | 回测模式)

Manual Testing | 手动测试

  • Tested locally | 本地测试通过
  • Tested on testnet | 测试网测试通过(交易所集成相关)
  • Unit tests pass | 单元测试通过
  • Verified no existing functionality broke | 确认没有破坏现有功能

Test Results | 测试结果

Build successful: go build -o nofx main.go
Position holding duration now correctly calculated using backtest simulation time
Live trading mode remains unaffected (uses system time as fallback)

🔒 Security Considerations | 安全考虑

  • No API keys or secrets hardcoded | 没有硬编码 API 密钥
  • User inputs properly validated | 用户输入已正确验证
  • No SQL injection vulnerabilities | 无 SQL 注入漏洞
  • Authentication/authorization properly handled | 认证/授权正确处理
  • Sensitive data is encrypted | 敏感数据已加密
  • N/A (not security-related) | 不适用

⚡ Performance Impact | 性能影响

  • No significant performance impact | 无显著性能影响
  • Performance improved | 性能提升
  • Performance may be impacted (explain below) | 性能可能受影响

If impacted, explain | 如果受影响,请说明:
No performance impact. Only changed the timestamp source for duration calculations.
无性能影响。仅改变了时长计算的时间戳来源。


✅ Checklist | 检查清单

Code Quality | 代码质量

  • Code follows project style | 代码遵循项目风格
  • Self-review completed | 已完成代码自查
  • Comments added for complex logic | 已添加必要注释
  • Code compiles successfully | 代码编译成功 (go build)
  • Ran go fmt | 已运行 go fmt

Documentation | 文档

  • Updated relevant documentation | 已更新相关文档
  • Added inline comments where necessary | 已添加必要的代码注释
  • Updated API documentation (if applicable) | 已更新 API 文档

Git

  • Commits follow conventional format | 提交遵循 Conventional Commits 格式
  • Rebased on latest dev branch | 已 rebase 到最新 dev 分支
  • No merge conflicts | 无合并冲突

📚 Additional Notes | 补充说明

English: This fix ensures that during backtesting, the position holding duration is calculated based on the simulated time progression rather than real system time. This provides more accurate metrics for strategy evaluation. Live trading functionality is preserved through a fallback mechanism to system time when backtest timestamp is not available.

中文: 此修复确保在回测期间,持仓时长基于模拟时间进度而非真实系统时间进行计算,为策略评估提供更准确的指标。通过在回测时间戳不可用时回退到系统时间的机制,保留了实盘交易功能。


By submitting this PR, I confirm | 提交此 PR,我确认:

  • I have read the Contributing Guidelines | 已阅读贡献指南
  • I agree to the Code of Conduct | 同意行为准则
  • My contribution is licensed under AGPL-3.0 | 贡献遵循 AGPL-3.0 许可证

🌟 Thank you for your contribution! | 感谢你的贡献!

@cla-assistant
Copy link

cla-assistant bot commented Dec 28, 2025

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant