mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 16:55:15 +00:00
grid2: pull out session dependency from the recoverGrid method
This commit is contained in:
parent
c721274adc
commit
8a45fe522e
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user