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
}
func (s *Strategy) recoverGrid(ctx context.Context, session *bbgo.ExchangeSession) 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
}
func (s *Strategy) recoverGrid(ctx context.Context, historyService types.ExchangeTradeHistoryService, openOrders []types.Order) error {
lastOrderID := uint64(0)
if len(openOrders) > 0 {
firstOrderTime := openOrders[0].CreationTime.Time()
lastOrderTime := firstOrderTime
for _, o := range openOrders {
@ -1020,6 +1006,7 @@ func (s *Strategy) recoverGrid(ctx context.Context, session *bbgo.ExchangeSessio
lastOrderTime = createTime
}
}
}
// Allocate a local order book
orderBook := bbgo.NewActiveOrderBook(s.Symbol)
@ -1092,6 +1079,7 @@ func (s *Strategy) recoverGrid(ctx context.Context, session *bbgo.ExchangeSessio
return nil
}
// scanMissingGridOrders finds the missing grid order prices
func (s *Strategy) scanMissingGridOrders(orderBook *bbgo.ActiveOrderBook, grid *Grid) PriceMap {
// Add all open orders to the local order book
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) {
defer wg.Done()