mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-12 07:16:56 +08:00
Feature: Add position holding duration to AI decision context
Track and display how long each position has been held to help AI make better timing decisions. **Implementation**: - Added UpdateTime field to PositionInfo struct (decision/engine.go:26) - Added positionFirstSeenTime map to AutoTrader for tracking (trader/auto_trader.go:60) - Record opening time when position is created successfully: - executeOpenLongWithRecord: Records timestamp for long positions (trader/auto_trader.go:540-541) - executeOpenShortWithRecord: Records timestamp for short positions (trader/auto_trader.go:593-594) - Fallback tracking in buildTradingContext for program restart scenarios (trader/auto_trader.go:386-392) - Auto-cleanup closed positions from tracking map (trader/auto_trader.go:409-414) - Display duration in user prompt with smart formatting: - Under 60 min: "持仓时长25分钟" - Over 60 min: "持仓时长2小时15分钟" **Example Output**: ``` 1. TAOUSDT LONG | 入场价435.5300 当前价433.1900 | 盈亏-0.54% | 杠杆20x | 保证金25 | 强平价418.1528 | 持仓时长2小时15分钟 ``` **Benefits**: - AI can see how long positions have been held - Helps enforce minimum holding period (30-60 min) from system prompt - Simple implementation with minimal overhead - Auto-cleanup prevents memory leaks 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -23,6 +23,7 @@ type PositionInfo struct {
|
||||
UnrealizedPnLPct float64 `json:"unrealized_pnl_pct"`
|
||||
LiquidationPrice float64 `json:"liquidation_price"`
|
||||
MarginUsed float64 `json:"margin_used"`
|
||||
UpdateTime int64 `json:"update_time"` // 持仓更新时间戳(毫秒)
|
||||
}
|
||||
|
||||
// AccountInfo 账户信息
|
||||
@@ -335,10 +336,24 @@ func buildUserPrompt(ctx *Context) string {
|
||||
if len(ctx.Positions) > 0 {
|
||||
sb.WriteString("## 当前持仓\n")
|
||||
for i, pos := range ctx.Positions {
|
||||
sb.WriteString(fmt.Sprintf("%d. %s %s | 入场价%.4f 当前价%.4f | 盈亏%+.2f%% | 杠杆%dx | 保证金%.0f | 强平价%.4f\n\n",
|
||||
// 计算持仓时长
|
||||
holdingDuration := ""
|
||||
if pos.UpdateTime > 0 {
|
||||
durationMs := time.Now().UnixMilli() - pos.UpdateTime
|
||||
durationMin := durationMs / (1000 * 60) // 转换为分钟
|
||||
if durationMin < 60 {
|
||||
holdingDuration = fmt.Sprintf(" | 持仓时长%d分钟", durationMin)
|
||||
} else {
|
||||
durationHour := durationMin / 60
|
||||
durationMinRemainder := durationMin % 60
|
||||
holdingDuration = fmt.Sprintf(" | 持仓时长%d小时%d分钟", durationHour, durationMinRemainder)
|
||||
}
|
||||
}
|
||||
|
||||
sb.WriteString(fmt.Sprintf("%d. %s %s | 入场价%.4f 当前价%.4f | 盈亏%+.2f%% | 杠杆%dx | 保证金%.0f | 强平价%.4f%s\n\n",
|
||||
i+1, pos.Symbol, strings.ToUpper(pos.Side),
|
||||
pos.EntryPrice, pos.MarkPrice, pos.UnrealizedPnLPct,
|
||||
pos.Leverage, pos.MarginUsed, pos.LiquidationPrice))
|
||||
pos.Leverage, pos.MarginUsed, pos.LiquidationPrice, holdingDuration))
|
||||
|
||||
// 使用FormatMarketData输出完整市场数据
|
||||
if marketData, ok := ctx.MarketDataMap[pos.Symbol]; ok {
|
||||
|
||||
Reference in New Issue
Block a user