mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
grid2: run recoverGrid only when user data stream is started
This commit is contained in:
parent
88116440ba
commit
6dfd18bd49
|
@ -1078,7 +1078,7 @@ func (s *Strategy) checkMinimalQuoteInvestment() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *Strategy) recoverGrid(ctx context.Context, historyService types.ExchangeTradeHistoryService, openOrders []types.Order) error {
|
||||
func (s *Strategy) recoverGridWithOpenOrders(ctx context.Context, historyService types.ExchangeTradeHistoryService, openOrders []types.Order) error {
|
||||
grid := s.newGrid()
|
||||
|
||||
// Add all open orders to the local order book
|
||||
|
@ -1444,26 +1444,6 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
|
|||
}
|
||||
}
|
||||
|
||||
if s.RecoverOrdersWhenStart {
|
||||
openOrders, err := session.Exchange.QueryOpenOrders(ctx, s.Symbol)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Infof("recoverWhenStart is set, found %d open orders, trying to recover grid orders...", len(openOrders))
|
||||
|
||||
if len(openOrders) > 0 {
|
||||
historyService, implemented := session.Exchange.(types.ExchangeTradeHistoryService)
|
||||
if !implemented {
|
||||
s.logger.Warn("ExchangeTradeHistoryService is not implemented, can not recover grid")
|
||||
} else {
|
||||
if err := s.recoverGrid(ctx, historyService, openOrders); err != nil {
|
||||
return errors.Wrap(err, "recover grid error")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
|
||||
|
@ -1492,6 +1472,14 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
|
|||
// if TriggerPrice is zero, that means we need to open the grid when start up
|
||||
if s.TriggerPrice.IsZero() {
|
||||
session.UserDataStream.OnStart(func() {
|
||||
// do recover only when triggerPrice is not set.
|
||||
if s.RecoverOrdersWhenStart {
|
||||
s.logger.Infof("recoverWhenStart is set, trying to recover grid orders...")
|
||||
if err := s.recoverGrid(ctx, session); err != nil {
|
||||
log.WithError(err).Errorf("recover error")
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.openGrid(ctx, session); err != nil {
|
||||
s.logger.WithError(err).Errorf("failed to setup grid orders")
|
||||
}
|
||||
|
@ -1501,6 +1489,30 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *Strategy) recoverGrid(ctx context.Context, session *bbgo.ExchangeSession) error {
|
||||
openOrders, err := session.Exchange.QueryOpenOrders(ctx, s.Symbol)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Infof("found %d open orders left on the %s order book", len(openOrders), s.Symbol)
|
||||
|
||||
// do recover only when openOrders > 0
|
||||
if len(openOrders) > 0 {
|
||||
historyService, implemented := session.Exchange.(types.ExchangeTradeHistoryService)
|
||||
if !implemented {
|
||||
s.logger.Warn("ExchangeTradeHistoryService is not implemented, can not recover grid")
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := s.recoverGridWithOpenOrders(ctx, historyService, openOrders); err != nil {
|
||||
return errors.Wrap(err, "recover grid error")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// openOrdersMismatches verifies if the open orders are on the grid pins
|
||||
// return true if mismatches
|
||||
func (s *Strategy) openOrdersMismatches(ctx context.Context, session *bbgo.ExchangeSession) (bool, error) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user