tradingutil: improve UniversalCancelAllOrders by querying open orders

This commit is contained in:
c9s 2024-11-15 23:35:18 +08:00
parent 8dcd6f2f72
commit 9d2e61dc6e
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 30 additions and 16 deletions

View File

@ -607,8 +607,7 @@ func (s *Strategy) CrossRun(
s.hedgerConnectivity = types.NewConnectivity() s.hedgerConnectivity = types.NewConnectivity()
s.hedgerConnectivity.Bind(s.hedgeSession.UserDataStream) s.hedgerConnectivity.Bind(s.hedgeSession.UserDataStream)
connGroup := types.NewConnectivityGroup(s.makerConnectivity, s.hedgerConnectivity) s.connectivityGroup = types.NewConnectivityGroup(s.makerConnectivity, s.hedgerConnectivity)
s.connectivityGroup = connGroup
if s.RecoverTrade { if s.RecoverTrade {
go s.runTradeRecover(ctx) go s.runTradeRecover(ctx)
@ -622,7 +621,7 @@ func (s *Strategy) CrossRun(
return return
case <-time.After(3 * time.Minute): case <-time.After(3 * time.Minute):
log.Panicf("authentication timeout, exiting...") log.Panicf("authentication timeout, exiting...")
case <-connGroup.AllAuthedC(ctx): case <-s.connectivityGroup.AllAuthedC(ctx):
} }
log.Infof("user data stream authenticated, start placing orders...") log.Infof("user data stream authenticated, start placing orders...")
@ -1192,6 +1191,8 @@ func (s *Strategy) updateQuote(ctx context.Context, maxLayer int) {
return return
} }
s.logger.Infof("%d orders are generated, placing...", len(submitOrders))
_, err = s.MakerOrderExecutor.SubmitOrders(ctx, submitOrders...) _, err = s.MakerOrderExecutor.SubmitOrders(ctx, submitOrders...)
if err != nil { if err != nil {
s.logger.WithError(err).Errorf("submit order error: %s", err.Error()) s.logger.WithError(err).Errorf("submit order error: %s", err.Error())

View File

@ -55,26 +55,39 @@ func UniversalCancelAllOrders(ctx context.Context, exchange types.Exchange, symb
} }
} }
if len(openOrders) == 0 { if len(openOrders) > 0 {
log.Warnf("empty open orders, unable to call specific cancel all orders api, skip") if service, ok := exchange.(CancelAllOrdersByGroupIDService); ok {
return nil var groupIds = CollectOrderGroupIds(openOrders)
} for _, groupId := range groupIds {
if _, err := service.CancelOrdersByGroupID(ctx, groupId); err != nil {
anyErr = err
}
}
if service, ok := exchange.(CancelAllOrdersByGroupIDService); ok { if anyErr == nil {
var groupIds = CollectOrderGroupIds(openOrders) return nil
for _, groupId := range groupIds {
if _, err := service.CancelOrdersByGroupID(ctx, groupId); err != nil {
anyErr = err
} }
} }
if anyErr == nil { if anyErr != nil {
return nil return anyErr
} }
} }
if anyErr != nil { // if we have no open order, then use the exchange service query to get the open orders and then cancel them all
return anyErr 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) return fmt.Errorf("unable to cancel all orders, openOrders:%+v", openOrders)