grid2: pull out session dependency from the recoverGrid method

This commit is contained in:
c9s 2022-12-23 16:48:40 +08:00
parent c721274adc
commit 8a45fe522e
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -989,35 +989,22 @@ func (s *Strategy) checkMinimalQuoteInvestment() error {
return nil return nil
} }
func (s *Strategy) recoverGrid(ctx context.Context, session *bbgo.ExchangeSession) error { func (s *Strategy) recoverGrid(ctx context.Context, historyService types.ExchangeTradeHistoryService, openOrders []types.Order) error {
historyService, implemented := session.Exchange.(types.ExchangeTradeHistoryService)
if !implemented {
return nil
}
openOrders, err := session.Exchange.QueryOpenOrders(ctx, s.Symbol)
if err != nil {
return err
}
// no open orders, the grid is not placed yet
if len(openOrders) == 0 {
return nil
}
lastOrderID := uint64(0) lastOrderID := uint64(0)
firstOrderTime := openOrders[0].CreationTime.Time() if len(openOrders) > 0 {
lastOrderTime := firstOrderTime firstOrderTime := openOrders[0].CreationTime.Time()
for _, o := range openOrders { lastOrderTime := firstOrderTime
if o.OrderID > lastOrderID { for _, o := range openOrders {
lastOrderID = o.OrderID if o.OrderID > lastOrderID {
} lastOrderID = o.OrderID
}
createTime := o.CreationTime.Time() createTime := o.CreationTime.Time()
if createTime.Before(firstOrderTime) { if createTime.Before(firstOrderTime) {
firstOrderTime = createTime firstOrderTime = createTime
} else if createTime.After(lastOrderTime) { } else if createTime.After(lastOrderTime) {
lastOrderTime = createTime lastOrderTime = createTime
}
} }
} }
@ -1092,6 +1079,7 @@ func (s *Strategy) recoverGrid(ctx context.Context, session *bbgo.ExchangeSessio
return nil return nil
} }
// scanMissingGridOrders finds the missing grid order prices
func (s *Strategy) scanMissingGridOrders(orderBook *bbgo.ActiveOrderBook, grid *Grid) PriceMap { func (s *Strategy) 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
gridPrices := make(PriceMap) gridPrices := make(PriceMap)
@ -1179,6 +1167,20 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
} }
} }
openOrders, err := session.Exchange.QueryOpenOrders(ctx, s.Symbol)
if err != nil {
return err
}
if len(openOrders) > 0 {
historyService, implemented := session.Exchange.(types.ExchangeTradeHistoryService)
if implemented {
if err := s.recoverGrid(ctx, historyService, openOrders); err != nil {
return err
}
}
}
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()