From fd2032b825167d1292ee88ef59a03e1c268c9e69 Mon Sep 17 00:00:00 2001 From: gx578007 Date: Fri, 10 Mar 2023 15:33:19 +0800 Subject: [PATCH] FIX: [grid2] avoid handling one orderID twice --- pkg/strategy/grid2/strategy.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/strategy/grid2/strategy.go b/pkg/strategy/grid2/strategy.go index fb697b6ff..72c5bb174 100644 --- a/pkg/strategy/grid2/strategy.go +++ b/pkg/strategy/grid2/strategy.go @@ -186,6 +186,9 @@ type Strategy struct { gridClosedCallbacks []func() gridErrorCallbacks []func(err error) + // filledOrderIDMap is used to prevent processing the same order ID twice. + filledOrderIDMap *types.SyncOrderMap + // mu is used for locking the grid object field, avoid double grid opening mu sync.Mutex @@ -240,6 +243,7 @@ func (s *Strategy) Defaults() error { } func (s *Strategy) Initialize() error { + s.filledOrderIDMap = types.NewSyncOrderMap() s.logger = log.WithFields(s.LogFields) return nil } @@ -505,8 +509,8 @@ func (s *Strategy) processFilledOrder(o types.Order) { // we calculate profit only when the order is placed successfully if profit != nil { - s.logger.Infof("GENERATED GRID PROFIT: %+v", profit) s.GridProfitStats.AddProfit(profit) + s.logger.Infof("GENERATED GRID PROFIT: %+v; TOTAL GRID PROFIT BECOMES: %f", profit, s.GridProfitStats.TotalQuoteProfit.Float64()) s.EmitGridProfit(s.GridProfitStats, profit) } } @@ -518,6 +522,12 @@ func (s *Strategy) handleOrderFilled(o types.Order) { return } + if s.filledOrderIDMap.Exists(o.OrderID) { + s.logger.Warn("duplicated id (%d) of filled order detected", o.OrderID) + return + } + s.filledOrderIDMap.Add(o) + s.logger.Infof("GRID ORDER FILLED: %s", o.String()) s.updateFilledOrderMetrics(o) s.processFilledOrder(o) @@ -2130,4 +2140,4 @@ func queryOpenOrdersUntilSuccessful(ctx context.Context, ex types.Exchange, symb err = generalBackoff(ctx, op) return openOrders, err -} +} \ No newline at end of file