diff --git a/pkg/exchange/retry/order.go b/pkg/exchange/retry/order.go index 3a61fd805..d27f297ef 100644 --- a/pkg/exchange/retry/order.go +++ b/pkg/exchange/retry/order.go @@ -9,8 +9,6 @@ import ( "github.com/cenkalti/backoff/v4" - "github.com/c9s/bbgo/pkg/exchange/max" - maxapi "github.com/c9s/bbgo/pkg/exchange/max/maxapi" "github.com/c9s/bbgo/pkg/types" ) @@ -53,7 +51,6 @@ func QueryOrderUntilCanceled( func QueryOrderUntilFilled( ctx context.Context, queryOrderService types.ExchangeOrderQueryService, symbol string, orderId uint64, ) (o *types.Order, err error) { - _, isMax := queryOrderService.(*max.Exchange) var op = func() (err2 error) { o, err2 = queryOrderService.QueryOrder(ctx, types.OrderQuery{ Symbol: symbol, @@ -70,16 +67,7 @@ func QueryOrderUntilFilled( // for final status return nil error to stop the retry switch o.Status { - case types.OrderStatusFilled: - if isMax { - // for MAX exchange, the order state done is filled but finalizing is not filled - if o.OriginalStatus == string(maxapi.OrderStateDone) { - return nil - } - } else { - return nil - } - case types.OrderStatusCanceled: + case types.OrderStatusFilled, types.OrderStatusCanceled: return nil } diff --git a/pkg/exchange/retry/trade.go b/pkg/exchange/retry/trade.go new file mode 100644 index 000000000..d9ed55f24 --- /dev/null +++ b/pkg/exchange/retry/trade.go @@ -0,0 +1,42 @@ +package retry + +import ( + "context" + "fmt" + + "github.com/c9s/bbgo/pkg/types" +) + +func QueryTradesUntilSuccessful( + ctx context.Context, ex types.ExchangeTradeHistoryService, symbol string, q *types.TradeQueryOptions, +) (trades []types.Trade, err error) { + var op = func() (err2 error) { + trades, err2 = ex.QueryTrades(ctx, symbol, q) + for _, trade := range trades { + if trade.FeeProcessing { + return fmt.Errorf("trade fee of #%d (order #%d) is still processing", trade.ID, trade.OrderID) + } + } + return err2 + } + + err = GeneralBackoff(ctx, op) + return trades, err +} + +func QueryTradesUntilSuccessfulLite( + ctx context.Context, ex types.ExchangeTradeHistoryService, symbol string, q *types.TradeQueryOptions, +) (trades []types.Trade, err error) { + var op = func() (err2 error) { + trades, err2 = ex.QueryTrades(ctx, symbol, q) + for _, trade := range trades { + if trade.FeeProcessing { + return fmt.Errorf("trade fee of #%d (order #%d) is still processing", trade.ID, trade.OrderID) + } + } + return err2 + } + + err = GeneralLiteBackoff(ctx, op) + return trades, err +} diff --git a/pkg/strategy/grid2/grid_recover.go b/pkg/strategy/grid2/grid_recover.go index ae47b9ee8..954775b95 100644 --- a/pkg/strategy/grid2/grid_recover.go +++ b/pkg/strategy/grid2/grid_recover.go @@ -10,7 +10,6 @@ import ( "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/exchange" - 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" @@ -72,33 +71,6 @@ func (s *Strategy) recoverByScanningTrades(ctx context.Context, session *bbgo.Ex // add open orders into avtive maker orders s.addOrdersToActiveOrderBook(openOrders) - // following is for MAX - if isMax { - var doneOrders []types.Order - for _, filledOrder := range filledOrders { - if filledOrder.OriginalStatus != string(maxapi.OrderStateDone) { - order, err := retry.QueryOrderUntilFilled(ctx, s.orderQueryService, filledOrder.Symbol, filledOrder.OrderID) - if err != nil { - return errors.Wrap(err, "unable to query orders until filled, please check it") - } - - if order == nil { - return fmt.Errorf("after QueryOrderUntilFilled, order and error are both nil. Please check it") - } - - doneOrders = append(doneOrders, *order) - } else { - doneOrders = append(doneOrders, filledOrder) - } - } - - if len(filledOrders) != len(doneOrders) { - return fmt.Errorf("num of filled orders (%d) and num of done orders (%d) should be the same", len(filledOrders), len(doneOrders)) - } - - filledOrders = doneOrders - } - // emit the filled orders activeOrderBook := s.orderExecutor.ActiveMakerOrders() for _, filledOrder := range filledOrders { @@ -298,7 +270,7 @@ func (s *Strategy) queryTradesToUpdateTwinOrdersMap(ctx context.Context, queryTr var fromTradeID uint64 = 0 var limit int64 = 1000 for { - trades, err := queryTradesService.QueryTrades(ctx, s.Symbol, &types.TradeQueryOptions{ + trades, err := retry.QueryTradesUntilSuccessful(ctx, queryTradesService, s.Symbol, &types.TradeQueryOptions{ StartTime: &since, EndTime: &until, LastTradeID: fromTradeID, diff --git a/pkg/strategy/grid2/recover.go b/pkg/strategy/grid2/recover.go index 4b657c608..0dc185a37 100644 --- a/pkg/strategy/grid2/recover.go +++ b/pkg/strategy/grid2/recover.go @@ -317,7 +317,7 @@ func queryTradesToUpdateTwinOrderBook( var fromTradeID uint64 = 0 var limit int64 = 1000 for { - trades, err := queryTradesService.QueryTrades(ctx, symbol, &types.TradeQueryOptions{ + trades, err := retry.QueryTradesUntilSuccessful(ctx, queryTradesService, symbol, &types.TradeQueryOptions{ StartTime: &since, EndTime: &until, LastTradeID: fromTradeID, diff --git a/pkg/strategy/grid2/strategy.go b/pkg/strategy/grid2/strategy.go index c742de011..b33ab3b24 100644 --- a/pkg/strategy/grid2/strategy.go +++ b/pkg/strategy/grid2/strategy.go @@ -417,7 +417,7 @@ func (s *Strategy) aggregateOrderQuoteAmountAndFee(o types.Order) (fixedpoint.Va s.logger.Warnf("GRID: missing #%d order trades or missing trade fee, pulling order trades from API", o.OrderID) // if orderQueryService is supported, use it to query the trades of the filled order - apiOrderTrades, err := s.orderQueryService.QueryOrderTrades(context.Background(), types.OrderQuery{ + apiOrderTrades, err := retry.QueryOrderTradesUntilSuccessful(context.Background(), s.orderQueryService, types.OrderQuery{ Symbol: o.Symbol, OrderID: strconv.FormatUint(o.OrderID, 10), })