feat(去重逻辑): 增强信号去重机制以区分不同动作

Changes:
* 更新去重键计算逻辑,新增动作字段以区分不同的交易动作(如 REDUCE 和 CLOSE)
* 增加单元测试以验证不同动作在相同价格下的去重行为
* 更新文档以反映去重窗口的变化,确保准确性
This commit is contained in:
2026-08-25 14:12:34 +08:00
parent b396638438
commit 4d4dbd27b2
3 changed files with 71 additions and 2 deletions
+68
View File
@@ -67,6 +67,31 @@ func TestSignalHashIgnoresUnrelatedFields(t *testing.T) {
}
}
func TestSignalHashDistinguishesAction(t *testing.T) {
base := map[string]interface{}{
"strategyCode": "BLONG",
"symbol": "SOLUSDT",
"period": "30m",
"side": "SHORT",
"price": 101.32,
}
reduce := cloneFields(base)
reduce["action"] = "REDUCE"
closeMsg := cloneFields(base)
closeMsg["action"] = "CLOSE"
if SignalHash("trade-signal", reduce) == SignalHash("trade-signal", closeMsg) {
t.Fatal("REDUCE and CLOSE at the same price should hash differently")
}
}
func cloneFields(in map[string]interface{}) map[string]interface{} {
out := make(map[string]interface{}, len(in)+1)
for k, v := range in {
out[k] = v
}
return out
}
func TestMemoryDeduperClaimOnce(t *testing.T) {
d := NewMemoryDeduper()
ok, err := d.Claim(context.Background(), "abc")
@@ -131,6 +156,49 @@ func TestHandleDedupByStrategySymbolPeriodDirectionPrice(t *testing.T) {
}
}
func TestHandleDedupKeepsReduceThenClose(t *testing.T) {
dedup := NewMemoryDeduper()
var events []string
process := func(_ context.Context, req notify.Request) (notify.Result, error) {
events = append(events, req.Event)
return notify.Result{Matched: true}, nil
}
lookup := func(context.Context, string) (*model.Source, error) { return enabledSrc(), nil }
conv := tradesignal.NewConverter(nil)
reduce := []byte(`{
"signalId":"local-0f6f4b00e143f8ee:update:1787629790828:0.12",
"sourcePosId":"local-0f6f4b00e143f8ee","sourcePosIds":["3340443882114850823"],
"strategyCode":"BLONG","symbol":"SOLUSDT","side":"SHORT","action":"REDUCE",
"quantity":0.11,"price":101.32,"leverage":20,"period":"30m",
"eventTime":"2026-08-25T03:49:51.155Z","addCount":0,"totalPos":0.12,
"totalAvgPx":102.64,"posMarginRatio":0.478261,"oldQuantity":0.23,"deltaQuantity":-0.11
}`)
closeBody := []byte(`{
"signalId":"local-0f6f4b00e143f8ee:close:1787629790936:0",
"sourcePosId":"local-0f6f4b00e143f8ee","sourcePosIds":["3340443882114850823"],
"strategyCode":"BLONG","symbol":"SOLUSDT","side":"SHORT","action":"CLOSE",
"quantity":0.12,"price":101.32,"leverage":1,"period":"30m",
"eventTime":"2026-08-25T03:49:51.389Z","addCount":0,"totalPos":0,
"totalAvgPx":0,"posMarginRatio":1,"oldQuantity":0.12
}`)
if d := HandleMessage(context.Background(), HandleInput{
Body: reduce, SourceName: "trade-signal", MaxRetry: 3, Deduper: dedup,
}, conv, lookup, process); d != DispositionAck {
t.Fatalf("reduce=%v", d)
}
if d := HandleMessage(context.Background(), HandleInput{
Body: closeBody, SourceName: "trade-signal", MaxRetry: 3, Deduper: dedup,
}, conv, lookup, process); d != DispositionAck {
t.Fatalf("close=%v", d)
}
if len(events) != 2 {
t.Fatalf("reduce then close should both notify, got %v", events)
}
if events[0] != "trade.reduce" || events[1] != "trade.close" {
t.Fatalf("events=%v", events)
}
}
func TestHandleDedupKeepsDifferentSources(t *testing.T) {
dedup := NewMemoryDeduper()
var n atomic.Int32