grid2: simplify isCompleteGridOrderBook

This commit is contained in:
c9s 2023-03-08 16:02:31 +08:00
parent a75bc2e590
commit c860e45c34
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 15 additions and 6 deletions

View File

@ -1527,7 +1527,8 @@ func (s *Strategy) replayOrderHistory(ctx context.Context, grid *Grid, orderBook
// isCompleteGridOrderBook checks if the number of open orders == gridNum - 1 and all orders are active order
func isCompleteGridOrderBook(orderBook *bbgo.ActiveOrderBook, gridNum int64) bool {
tmpOrders := orderBook.Orders()
return len(tmpOrders) == int(gridNum)-1 && types.OrdersAll(tmpOrders, types.IsActiveOrder)
activeOrders := types.OrdersActive(tmpOrders)
return len(activeOrders) == int(gridNum)-1
}
func findEarliestOrderID(orders []types.Order) (uint64, bool) {

View File

@ -395,17 +395,25 @@ func (o Order) SlackAttachment() slack.Attachment {
}
}
func OrdersFilled(in []Order) (out []Order) {
func OrdersFilter(in []Order, f func(o Order) bool) (out []Order) {
for _, o := range in {
switch o.Status {
case OrderStatusFilled:
o2 := o
out = append(out, o2)
if f(o) {
out = append(out, o)
}
}
return out
}
func OrdersActive(in []Order) []Order {
return OrdersFilter(in, IsActiveOrder)
}
func OrdersFilled(in []Order) (out []Order) {
return OrdersFilter(in, func(o Order) bool {
return o.Status == OrderStatusFilled
})
}
func OrdersAll(orders []Order, f func(o Order) bool) bool {
for _, o := range orders {
if !f(o) {