mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
move to onAuth
This commit is contained in:
parent
c5449374cd
commit
5ff3828ec1
|
@ -23,9 +23,29 @@ type SyncActiveOrdersOpts struct {
|
||||||
exchange types.Exchange
|
exchange types.Exchange
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Strategy) initializeRecoverCh() bool {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
|
alreadyInitialize := false
|
||||||
|
|
||||||
|
if s.activeOrdersRecoverCh == nil {
|
||||||
|
s.logger.Info("initialize recover channel")
|
||||||
|
s.activeOrdersRecoverCh = make(chan struct{}, 1)
|
||||||
|
} else {
|
||||||
|
s.logger.Info("already initialize recover channel, trigger active orders recover")
|
||||||
|
alreadyInitialize = true
|
||||||
|
s.activeOrdersRecoverCh <- struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return alreadyInitialize
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Strategy) recoverActiveOrdersPeriodically(ctx context.Context) {
|
func (s *Strategy) recoverActiveOrdersPeriodically(ctx context.Context) {
|
||||||
// every time we activeOrdersRecoverCh receive signal, do active orders recover
|
// every time we activeOrdersRecoverCh receive signal, do active orders recover
|
||||||
s.activeOrdersRecoverCh = make(chan struct{}, 1)
|
if alreadyInitialize := s.initializeRecoverCh(); alreadyInitialize {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// make ticker's interval random in 25 min ~ 35 min
|
// make ticker's interval random in 25 min ~ 35 min
|
||||||
interval := util.MillisecondsJitter(25*time.Minute, 10*60*1000)
|
interval := util.MillisecondsJitter(25*time.Minute, 10*60*1000)
|
||||||
|
|
|
@ -1967,14 +1967,8 @@ 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 TriggerPrice is zero, that means we need to open the grid when start up
|
||||||
if s.TriggerPrice.IsZero() {
|
if s.TriggerPrice.IsZero() {
|
||||||
// must call the openGrid method inside the OnStart callback because
|
session.UserDataStream.OnAuth(func() {
|
||||||
// it needs to receive the trades from the user data stream
|
s.logger.Infof("user data stream authenticated, start the process")
|
||||||
//
|
|
||||||
// should try to avoid blocking the user data stream
|
|
||||||
// callbacks are blocking operation
|
|
||||||
session.UserDataStream.OnStart(func() {
|
|
||||||
s.logger.Infof("user data stream started, initializing grid...")
|
|
||||||
|
|
||||||
if !bbgo.IsBackTesting {
|
if !bbgo.IsBackTesting {
|
||||||
time.AfterFunc(3*time.Second, func() {
|
time.AfterFunc(3*time.Second, func() {
|
||||||
if err := s.startProcess(ctx, session); err != nil {
|
if err := s.startProcess(ctx, session); err != nil {
|
||||||
|
@ -1989,17 +1983,6 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
session.UserDataStream.OnAuth(func() {
|
|
||||||
time.AfterFunc(util.MillisecondsJitter(5*time.Second, 1000*10), func() {
|
|
||||||
select {
|
|
||||||
case s.activeOrdersRecoverCh <- struct{}{}:
|
|
||||||
s.logger.Info("trigger active orders recover when on auth")
|
|
||||||
default:
|
|
||||||
s.logger.Warn("failed to trigger active orders recover when on auth")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user