fix(交易信号): 修复未追踪仓位时平均单价渲染为 %!f(<nil>) 的问题
- 本地无均价记录时回退使用上游 totalAvgPx - 跟单模板在无均价时不再输出平均单价行
This commit is contained in:
@@ -117,8 +117,7 @@ func TestConvertThenRenderCopyTradeTinyPEPE(t *testing.T) {
|
||||
}
|
||||
data["pushedAt"] = "2026.08.22 10:04:29"
|
||||
|
||||
const tmpl = "{{case .side \"LONG\" \"多单\" \"SHORT\" \"空单\"}}{{case .action \"OPEN\" \"开仓\" \"ADD\" \"加仓\" \"CLOSE\" \"平仓\" \"REDUCE\" \"减仓\"}}\n交易品种: {{case .symbol \"ETHUSDT\" \"ETH\" \"BTCUSDT\" \"BTC\" \"SOLUSDT\" \"SOL\" \"BNBUSDT\" \"BNB\" .symbol}}\n{{case .action \"OPEN\" \"开仓价格\" \"ADD\" \"加仓价格\" \"CLOSE\" \"平仓价格\" \"REDUCE\" \"减仓价格\"}}: {{printf \"%.2f\" .price}}\n{{case .action \"OPEN\" \"开仓数量\" \"ADD\" \"加仓数量\" \"CLOSE\" \"平仓数量\" \"REDUCE\" \"减仓数量\"}}: {{printf \"%.2f\" .quantity}}\n平均单价: {{printf \"%.2f\" .avgPrice}}\n{{if or (eq .action \"OPEN\") (eq .action \"ADD\")}}{{if .leverage}}杠杆: {{.leverage}}x\n{{end}}{{end}}策略: {{case .strategyCode \"BLONG\" \"B龙策略\" .strategyCode}}\n推送时间: {{.pushedAt}}"
|
||||
out, err := engine.NewRenderer().Render(tmpl, data)
|
||||
out, err := engine.NewRenderer().Render(copyTradeTmpl, data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -138,3 +137,46 @@ func TestConvertThenRenderCopyTradeTinyPEPE(t *testing.T) {
|
||||
t.Errorf("tiny price collapsed to 0.00:\n%s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertUntrackedReduceFallsBackToTotalAvgPx(t *testing.T) {
|
||||
_, data, err := NewConverter(nil).Convert([]byte(`{
|
||||
"signalId":"u1","strategyCode":"ALLINETH","symbol":"ETHUSDT",
|
||||
"side":"SHORT","action":"REDUCE","quantity":0.11,"price":1890,
|
||||
"totalAvgPx":1899.03,"posMarginRatio":0.5
|
||||
}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if data["avgPrice"] != 1899.03 {
|
||||
t.Fatalf("avgPrice=%v want 1899.03", data["avgPrice"])
|
||||
}
|
||||
out, err := engine.NewRenderer().Render(copyTradeTmpl, data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(out, "平均单价: 1899.03") {
|
||||
t.Fatalf("out=\n%s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertUntrackedCloseOmitsAvgPrice(t *testing.T) {
|
||||
_, data, err := NewConverter(nil).Convert([]byte(`{
|
||||
"signalId":"u2","strategyCode":"BLONG","symbol":"SOLUSDT",
|
||||
"side":"SHORT","action":"CLOSE","quantity":0.12,"price":101.32,"totalAvgPx":0
|
||||
}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := data["avgPrice"]; ok {
|
||||
t.Fatalf("avgPrice should be absent, got %v", data["avgPrice"])
|
||||
}
|
||||
out, err := engine.NewRenderer().Render(copyTradeTmpl, data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(out, "%!") || strings.Contains(out, "平均单价") {
|
||||
t.Fatalf("out=\n%s", out)
|
||||
}
|
||||
}
|
||||
|
||||
const copyTradeTmpl = "{{case .side \"LONG\" \"多单\" \"SHORT\" \"空单\"}}{{case .action \"OPEN\" \"开仓\" \"ADD\" \"加仓\" \"CLOSE\" \"平仓\" \"REDUCE\" \"减仓\"}}\n交易品种: {{case .symbol \"ETHUSDT\" \"ETH\" \"BTCUSDT\" \"BTC\" \"SOLUSDT\" \"SOL\" \"BNBUSDT\" \"BNB\" .symbol}}\n{{case .action \"OPEN\" \"开仓价格\" \"ADD\" \"加仓价格\" \"CLOSE\" \"平仓价格\" \"REDUCE\" \"减仓价格\"}}: {{printf \"%.2f\" .price}}\n{{case .action \"OPEN\" \"开仓数量\" \"ADD\" \"加仓数量\" \"CLOSE\" \"平仓数量\" \"REDUCE\" \"减仓数量\"}}: {{printf \"%.2f\" .quantity}}\n{{if .avgPrice}}平均单价: {{printf \"%.2f\" .avgPrice}}\n{{end}}{{if or (eq .action \"OPEN\") (eq .action \"ADD\")}}{{if .leverage}}杠杆: {{.leverage}}x\n{{end}}{{end}}策略: {{case .strategyCode \"BLONG\" \"B龙策略\" .strategyCode}}\n推送时间: {{.pushedAt}}"
|
||||
|
||||
Reference in New Issue
Block a user