From e5033c093a3886307c81a347fff072b4f951241e Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 17 Nov 2023 16:46:16 +0800 Subject: [PATCH] grid2: check order's original status for updating --- pkg/strategy/grid2/active_order_recover.go | 16 +++++++++---- pkg/strategy/grid2/recover.go | 28 +++++++++++++++------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/pkg/strategy/grid2/active_order_recover.go b/pkg/strategy/grid2/active_order_recover.go index 9e407b726..d06f316fc 100644 --- a/pkg/strategy/grid2/active_order_recover.go +++ b/pkg/strategy/grid2/active_order_recover.go @@ -4,14 +4,16 @@ import ( "context" "time" - "github.com/c9s/bbgo/pkg/bbgo" - "github.com/c9s/bbgo/pkg/exchange/retry" - "github.com/c9s/bbgo/pkg/types" - "github.com/c9s/bbgo/pkg/util" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" "github.com/sirupsen/logrus" "go.uber.org/multierr" + + "github.com/c9s/bbgo/pkg/bbgo" + "github.com/c9s/bbgo/pkg/exchange/max" + "github.com/c9s/bbgo/pkg/exchange/retry" + "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/util" ) type SyncActiveOrdersOpts struct { @@ -89,6 +91,11 @@ func (s *Strategy) recoverActiveOrdersPeriodically(ctx context.Context) { } } +func isMaxExchange(ex interface{}) bool { + _, yes := ex.(*max.Exchange) + return yes +} + func syncActiveOrders(ctx context.Context, opts SyncActiveOrdersOpts) error { opts.logger.Infof("[ActiveOrderRecover] syncActiveOrders") @@ -115,6 +122,7 @@ func syncActiveOrders(ctx context.Context, opts SyncActiveOrdersOpts) error { var errs error // update active orders not in open orders for _, activeOrder := range activeOrders { + if _, exist := openOrdersMap[activeOrder.OrderID]; exist { // no need to sync active order already in active orderbook, because we only need to know if it filled or not. delete(openOrdersMap, activeOrder.OrderID) diff --git a/pkg/strategy/grid2/recover.go b/pkg/strategy/grid2/recover.go index 9418dd5a8..cae47b303 100644 --- a/pkg/strategy/grid2/recover.go +++ b/pkg/strategy/grid2/recover.go @@ -6,11 +6,13 @@ import ( "strconv" "time" + "github.com/pkg/errors" + "github.com/c9s/bbgo/pkg/bbgo" + maxapi "github.com/c9s/bbgo/pkg/exchange/max/maxapi" "github.com/c9s/bbgo/pkg/exchange/retry" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" - "github.com/pkg/errors" ) var syncWindow = -3 * time.Minute @@ -267,24 +269,34 @@ func buildTwinOrderBook(pins []Pin, orders []types.Order) (*TwinOrderBook, error return book, nil } -func syncActiveOrder(ctx context.Context, activeOrderBook *bbgo.ActiveOrderBook, orderQueryService types.ExchangeOrderQueryService, orderID uint64, syncBefore time.Time) (bool, error) { +func syncActiveOrder( + ctx context.Context, activeOrderBook *bbgo.ActiveOrderBook, orderQueryService types.ExchangeOrderQueryService, + orderID uint64, syncBefore time.Time, +) (isOrderUpdated bool, err error) { + isMax := isMaxExchange(orderQueryService) + updatedOrder, err := retry.QueryOrderUntilSuccessful(ctx, orderQueryService, types.OrderQuery{ Symbol: activeOrderBook.Symbol, OrderID: strconv.FormatUint(orderID, 10), }) - isActiveOrderBookUpdated := false - if err != nil { - return isActiveOrderBookUpdated, err + return isOrderUpdated, err } - isActiveOrderBookUpdated = updatedOrder.UpdateTime.Before(syncBefore) - if isActiveOrderBookUpdated { + // maxapi.OrderStateFinalizing does not mean the fee is calculated + // we should only consider order state done for MAX + if isMax && updatedOrder.OriginalStatus != string(maxapi.OrderStateDone) { + return isOrderUpdated, nil + } + + // should only trigger order update when the updated time is old enough + isOrderUpdated = updatedOrder.UpdateTime.Before(syncBefore) + if isOrderUpdated { activeOrderBook.Update(*updatedOrder) } - return isActiveOrderBookUpdated, nil + return isOrderUpdated, nil } func queryTradesToUpdateTwinOrderBook(