grid2: add scanOrderCreationTimeRange func

This commit is contained in:
c9s 2022-12-23 23:41:36 +08:00
parent 606b4650b3
commit ae20cef8f4
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -1006,23 +1006,11 @@ func (s *Strategy) recoverGrid(ctx context.Context, historyService types.Exchang
now := time.Now() now := time.Now()
firstOrderTime := now.AddDate(0, -1, 0) firstOrderTime := now.AddDate(0, -1, 0)
lastOrderTime := firstOrderTime lastOrderTime := firstOrderTime
if len(openOrders) > 0 { if since, until, ok := scanOrderCreationTimeRange(openOrders); ok {
lastOrderID = openOrders[0].OrderID firstOrderTime = since
firstOrderTime = openOrders[0].CreationTime.Time() lastOrderTime = until
lastOrderTime = firstOrderTime
for _, o := range openOrders {
if o.OrderID < lastOrderID {
lastOrderID = o.OrderID
}
createTime := o.CreationTime.Time()
if createTime.Before(firstOrderTime) {
firstOrderTime = createTime
} else if createTime.After(lastOrderTime) {
lastOrderTime = createTime
}
}
} }
_ = lastOrderTime
// Allocate a local order book // Allocate a local order book
orderBook := bbgo.NewActiveOrderBook(s.Symbol) orderBook := bbgo.NewActiveOrderBook(s.Symbol)
@ -1132,6 +1120,26 @@ func ordersFilled(in []types.Order) (out []types.Order) {
return out return out
} }
// scanOrderCreationTimeRange finds the earliest creation time and the latest creation time from the given orders
func scanOrderCreationTimeRange(orders []types.Order) (time.Time, time.Time, bool) {
if len(orders) == 0 {
return time.Time{}, time.Time{}, false
}
firstOrderTime := orders[0].CreationTime.Time()
lastOrderTime := firstOrderTime
for _, o := range orders {
createTime := o.CreationTime.Time()
if createTime.Before(firstOrderTime) {
firstOrderTime = createTime
} else if createTime.After(lastOrderTime) {
lastOrderTime = createTime
}
}
return firstOrderTime, lastOrderTime, true
}
// scanMissingGridOrders finds the missing grid order prices // scanMissingGridOrders finds the missing grid order prices
func scanMissingGridOrders(orderBook *bbgo.ActiveOrderBook, grid *Grid) PriceMap { func scanMissingGridOrders(orderBook *bbgo.ActiveOrderBook, grid *Grid) PriceMap {
// Add all open orders to the local order book // Add all open orders to the local order book