fix(交易信号): 修复未追踪仓位时平均单价渲染为 %!f(<nil>) 的问题

- 本地无均价记录时回退使用上游 totalAvgPx
- 跟单模板在无均价时不再输出平均单价行
This commit is contained in:
2026-10-10 00:48:35 +08:00
parent 931130038b
commit 8e6a1332f0
5 changed files with 62 additions and 8 deletions
+15 -4
View File
@@ -50,8 +50,7 @@ func (c *Converter) Convert(body []byte) (string, map[string]interface{}, error)
return "", nil, fmt.Errorf("%w: %v", ErrPositionStore, err)
}
var opts FormatOptions
if snap.HasAvg {
avg := snap.AvgPrice
if avg, ok := avgPriceFor(snap, out); ok {
opts.AvgPrice = &avg
}
text := Format(out, opts)
@@ -60,12 +59,24 @@ func (c *Converter) Convert(body []byte) (string, map[string]interface{}, error)
return "", nil, err
}
data["formatted"] = text
if snap.HasAvg {
data["avgPrice"] = snap.AvgPrice
if opts.AvgPrice != nil {
data["avgPrice"] = *opts.AvgPrice
}
return "trade." + strings.ToLower(out.Action), data, nil
}
// avgPriceFor prefers the tracked average and falls back to the upstream
// totalAvgPx when the tracker never saw the position open.
func avgPriceFor(snap Snapshot, sig *Signal) (float64, bool) {
if snap.HasAvg {
return snap.AvgPrice, true
}
if sig.TotalAvgPx != nil && *sig.TotalAvgPx > 0 {
return *sig.TotalAvgPx, true
}
return 0, false
}
func (c *Converter) overrideFor(code string) *config.StrategyOverride {
if c == nil || len(c.overrides) == 0 || code == "" {
return nil