From 6b7515098365f813d945f4b4c0d2dfedf8e14f83 Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 24 Dec 2022 15:58:02 +0800 Subject: [PATCH] refactor order related functions into core api --- pkg/strategy/grid2/strategy.go | 37 ++-------------------------------- pkg/types/order.go | 33 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/pkg/strategy/grid2/strategy.go b/pkg/strategy/grid2/strategy.go index 948249d3d..f2e59f639 100644 --- a/pkg/strategy/grid2/strategy.go +++ b/pkg/strategy/grid2/strategy.go @@ -1068,7 +1068,7 @@ func (s *Strategy) recoverGrid(ctx context.Context, historyService types.Exchang } // we will only submit reverse orders for filled orders - filledOrders := ordersFilled(tmpOrders) + filledOrders := types.OrdersFilled(tmpOrders) s.logger.Infof("GRID RECOVER: found %d filled grid orders", len(filledOrders)) @@ -1146,49 +1146,16 @@ func (s *Strategy) replayOrderHistory(ctx context.Context, grid *Grid, orderBook return nil } -func isActiveOrder(o types.Order) bool { - return o.Status == types.OrderStatusNew || o.Status == types.OrderStatusPartiallyFilled -} - func isCompleteGridOrderBook(orderBook *bbgo.ActiveOrderBook, gridNum int64) bool { tmpOrders := orderBook.Orders() - if len(tmpOrders) == int(gridNum) && ordersAll(tmpOrders, isActiveOrder) { + if len(tmpOrders) == int(gridNum) && types.OrdersAll(tmpOrders, types.IsActiveOrder) { return true } return false } -func ordersFilled(in []types.Order) (out []types.Order) { - for _, o := range in { - switch o.Status { - case types.OrderStatusFilled: - o2 := o - out = append(out, o2) - } - } - return out -} - -func ordersAll(orders []types.Order, f func(o types.Order) bool) bool { - for _, o := range orders { - if !f(o) { - return false - } - } - return true -} - -func ordersAny(orders []types.Order, f func(o types.Order) bool) bool { - for _, o := range orders { - if f(o) { - return true - } - } - return false -} - func debugGrid(grid *Grid, book *bbgo.ActiveOrderBook) { fmt.Println("================== GRID ORDERS ==================") diff --git a/pkg/types/order.go b/pkg/types/order.go index 4444bac87..82456caf9 100644 --- a/pkg/types/order.go +++ b/pkg/types/order.go @@ -394,3 +394,36 @@ func (o Order) SlackAttachment() slack.Attachment { Footer: strings.ToLower(o.Exchange.String()) + templateutil.Render(" creation time {{ . }}", o.CreationTime.Time().Format(time.StampMilli)), } } + +func OrdersFilled(in []Order) (out []Order) { + for _, o := range in { + switch o.Status { + case OrderStatusFilled: + o2 := o + out = append(out, o2) + } + } + return out +} + +func OrdersAll(orders []Order, f func(o Order) bool) bool { + for _, o := range orders { + if !f(o) { + return false + } + } + return true +} + +func OrdersAny(orders []Order, f func(o Order) bool) bool { + for _, o := range orders { + if f(o) { + return true + } + } + return false +} + +func IsActiveOrder(o Order) bool { + return o.Status == OrderStatusNew || o.Status == OrderStatusPartiallyFilled +}