refactor order related functions into core api

This commit is contained in:
c9s 2022-12-24 15:58:02 +08:00
parent a46b3fe908
commit 6b75150983
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 35 additions and 35 deletions

View File

@ -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 ==================")

View File

@ -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
}