FIX: [grid2] avoid handling one orderID twice

This commit is contained in:
gx578007 2023-03-10 15:33:19 +08:00
parent 78d65d74d2
commit fd2032b825

View File

@ -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
}
}