grid2: pull out start process to a function

This commit is contained in:
c9s 2023-03-05 17:07:01 +08:00
parent fc1effff97
commit 0f307bba7d
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -1792,17 +1792,10 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
session.UserDataStream.OnStart(func() {
s.logger.Infof("user data stream started, initializing grid...")
if s.RecoverOrdersWhenStart && !bbgo.IsBackTesting {
// do recover only when triggerPrice is not set and not in the back-test mode
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")
}
}
// avoid using goroutine here for back-test
if err := s.openGrid(ctx, session); err != nil {
s.logger.WithError(err).Errorf("failed to setup grid orders")
if !bbgo.IsBackTesting {
go s.startProcess(ctx, session)
} else {
s.startProcess(ctx, session)
}
})
}
@ -1810,6 +1803,21 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
return nil
}
func (s *Strategy) startProcess(ctx context.Context, session *bbgo.ExchangeSession) {
if s.RecoverOrdersWhenStart {
// do recover only when triggerPrice is not set and not in the back-test mode
s.logger.Infof("recoverWhenStart is set, trying to recover grid orders...")
if err := s.recoverGrid(ctx, session); err != nil {
s.logger.WithError(err).Errorf("recover error")
}
}
// avoid using goroutine here for back-test
if err := s.openGrid(ctx, session); err != nil {
s.logger.WithError(err).Errorf("failed to setup grid orders")
}
}
func (s *Strategy) recoverGrid(ctx context.Context, session *bbgo.ExchangeSession) error {
openOrders, err := session.Exchange.QueryOpenOrders(ctx, s.Symbol)
if err != nil {