fix(backtest): correct position holding duration calculation in backtest mode #1291
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📝 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 | 变更类型
🔗 Related Issues | 相关 Issue
📋 Changes Made | 具体变更
English:
CurrentTimestampMsfield todecision.Contextstruct to store current backtest timestampbuildDecisionContext()inbacktest/runner.goto set the current timestamp for accurate holding duration calculationconvertPositions()to use position open time (pos.OpenTime) instead of system time (time.Now()) forUpdateTimefieldformatPositionInfo()indecision/engine.goto 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 | 测试环境
Manual Testing | 手动测试
Test Results | 测试结果
🔒 Security Considerations | 安全考虑
⚡ Performance Impact | 性能影响
If impacted, explain | 如果受影响,请说明:
No performance impact. Only changed the timestamp source for duration calculations.
无性能影响。仅改变了时长计算的时间戳来源。
✅ Checklist | 检查清单
Code Quality | 代码质量
go build)go fmt| 已运行go fmtDocumentation | 文档
Git
devbranch | 已 rebase 到最新dev分支📚 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,我确认:
🌟 Thank you for your contribution! | 感谢你的贡献!