From a5fb408a167d8e69e24aa860be2c9be1f45aa168 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 7 Jan 2022 01:34:23 +0800 Subject: [PATCH] twap: refactor and call activeMakerOrders.GracefulCancel --- pkg/bbgo/active_book.go | 2 +- pkg/bbgo/twap_order_executor.go | 53 ++++----------------------------- 2 files changed, 7 insertions(+), 48 deletions(-) diff --git a/pkg/bbgo/active_book.go b/pkg/bbgo/active_book.go index 6158431d8..bee3fa8dc 100644 --- a/pkg/bbgo/active_book.go +++ b/pkg/bbgo/active_book.go @@ -63,7 +63,7 @@ func (b *LocalActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exch case <-time.After(3 * time.Second): case <-ctx.Done(): - break + return ctx.Err() } diff --git a/pkg/bbgo/twap_order_executor.go b/pkg/bbgo/twap_order_executor.go index 745079bf1..ef727ea6d 100644 --- a/pkg/bbgo/twap_order_executor.go +++ b/pkg/bbgo/twap_order_executor.go @@ -266,7 +266,7 @@ func (e *TwapExecution) updateOrder(ctx context.Context) error { } } - e.cancelActiveOrders(ctx) + e.cancelActiveOrders() } orderForm, err := e.newBestPriceOrder() @@ -284,51 +284,10 @@ func (e *TwapExecution) updateOrder(ctx context.Context) error { return nil } -func (e *TwapExecution) cancelActiveOrders(ctx context.Context) { - didCancel := false - for e.activeMakerOrders.NumOfOrders() > 0 { - didCancel = true - - orders := e.activeMakerOrders.Orders() - log.Infof("canceling %d open orders:", len(orders)) - e.activeMakerOrders.Print() - - if err := e.Session.Exchange.CancelOrders(ctx, orders...); err != nil { - log.WithError(err).Errorf("can not cancel %s orders", e.Symbol) - } - - select { - case <-ctx.Done(): - return - - case <-time.After(3 * time.Second): - - } - - // verify the current open orders via the RESTful API - if e.activeMakerOrders.NumOfOrders() > 0 { - log.Warnf("there are orders not cancelled, using REStful API to verify...") - openOrders, err := e.Session.Exchange.QueryOpenOrders(ctx, e.Symbol) - if err != nil { - log.WithError(err).Errorf("can not query %s open orders", e.Symbol) - continue - } - - openOrderStore := NewOrderStore(e.Symbol) - openOrderStore.Add(openOrders...) - - for _, o := range e.activeMakerOrders.Orders() { - // if it does not exist, we should remove it - if !openOrderStore.Exists(o.OrderID) { - e.activeMakerOrders.Remove(o) - } - } - } - } - - if didCancel { - log.Infof("orders are canceled successfully") - } +func (e *TwapExecution) cancelActiveOrders() { + gracefulCtx, gracefulCancel := context.WithTimeout(context.TODO(), 30 * time.Second) + defer gracefulCancel() + e.activeMakerOrders.GracefulCancel(gracefulCtx, e.Session.Exchange) } func (e *TwapExecution) orderUpdater(ctx context.Context) { @@ -340,7 +299,7 @@ func (e *TwapExecution) orderUpdater(ctx context.Context) { // 1. the given context is canceled. // 2. the base quantity equals to or greater than the target quantity defer func() { - e.cancelActiveOrders(context.Background()) + e.cancelActiveOrders() e.cancelUserDataStream() e.emitDone() }()