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,23 +989,9 @@ 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)
if len(openOrders) > 0 {
firstOrderTime := openOrders[0].CreationTime.Time() firstOrderTime := openOrders[0].CreationTime.Time()
lastOrderTime := firstOrderTime lastOrderTime := firstOrderTime
for _, o := range openOrders { for _, o := range openOrders {
@ -1020,6 +1006,7 @@ func (s *Strategy) recoverGrid(ctx context.Context, session *bbgo.ExchangeSessio
lastOrderTime = createTime lastOrderTime = createTime
} }
} }
}
// Allocate a local order book // Allocate a local order book
orderBook := bbgo.NewActiveOrderBook(s.Symbol) orderBook := bbgo.NewActiveOrderBook(s.Symbol)
@ -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()