From 9d2e61dc6e46700af49cf430ae496d7538f26ed1 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 15 Nov 2024 23:35:18 +0800 Subject: [PATCH] tradingutil: improve UniversalCancelAllOrders by querying open orders --- pkg/strategy/xdepthmaker/strategy.go | 7 ++--- pkg/util/tradingutil/cancel.go | 39 ++++++++++++++++++---------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/pkg/strategy/xdepthmaker/strategy.go b/pkg/strategy/xdepthmaker/strategy.go index e18f4ab0b..ce36e4afa 100644 --- a/pkg/strategy/xdepthmaker/strategy.go +++ b/pkg/strategy/xdepthmaker/strategy.go @@ -607,8 +607,7 @@ func (s *Strategy) CrossRun( s.hedgerConnectivity = types.NewConnectivity() s.hedgerConnectivity.Bind(s.hedgeSession.UserDataStream) - connGroup := types.NewConnectivityGroup(s.makerConnectivity, s.hedgerConnectivity) - s.connectivityGroup = connGroup + s.connectivityGroup = types.NewConnectivityGroup(s.makerConnectivity, s.hedgerConnectivity) if s.RecoverTrade { go s.runTradeRecover(ctx) @@ -622,7 +621,7 @@ func (s *Strategy) CrossRun( return case <-time.After(3 * time.Minute): log.Panicf("authentication timeout, exiting...") - case <-connGroup.AllAuthedC(ctx): + case <-s.connectivityGroup.AllAuthedC(ctx): } log.Infof("user data stream authenticated, start placing orders...") @@ -1192,6 +1191,8 @@ func (s *Strategy) updateQuote(ctx context.Context, maxLayer int) { return } + s.logger.Infof("%d orders are generated, placing...", len(submitOrders)) + _, err = s.MakerOrderExecutor.SubmitOrders(ctx, submitOrders...) if err != nil { s.logger.WithError(err).Errorf("submit order error: %s", err.Error()) diff --git a/pkg/util/tradingutil/cancel.go b/pkg/util/tradingutil/cancel.go index 2c24a71ec..459e8b8e6 100644 --- a/pkg/util/tradingutil/cancel.go +++ b/pkg/util/tradingutil/cancel.go @@ -55,26 +55,39 @@ func UniversalCancelAllOrders(ctx context.Context, exchange types.Exchange, symb } } - if len(openOrders) == 0 { - log.Warnf("empty open orders, unable to call specific cancel all orders api, skip") - return nil - } + if len(openOrders) > 0 { + if service, ok := exchange.(CancelAllOrdersByGroupIDService); ok { + var groupIds = CollectOrderGroupIds(openOrders) + for _, groupId := range groupIds { + if _, err := service.CancelOrdersByGroupID(ctx, groupId); err != nil { + anyErr = err + } + } - if service, ok := exchange.(CancelAllOrdersByGroupIDService); ok { - var groupIds = CollectOrderGroupIds(openOrders) - for _, groupId := range groupIds { - if _, err := service.CancelOrdersByGroupID(ctx, groupId); err != nil { - anyErr = err + if anyErr == nil { + return nil } } - if anyErr == nil { - return nil + if anyErr != nil { + return anyErr } } - if anyErr != nil { - return anyErr + // if we have no open order, then use the exchange service query to get the open orders and then cancel them all + if len(openOrders) == 0 { + if len(symbol) == 0 { + log.Warnf("empty open orders, unable to call specific cancel all orders api, skip") + return nil + } + + var err error + openOrders, err = retry.QueryOpenOrdersUntilSuccessful(ctx, exchange, symbol) + if err != nil { + return err + } + + return retry.CancelOrdersUntilSuccessful(ctx, exchange, openOrders...) } return fmt.Errorf("unable to cancel all orders, openOrders:%+v", openOrders)