xmaker: add interval jitter

This commit is contained in:
c9s 2021-05-09 20:03:06 +08:00
parent c278cdbf30
commit 2f326d0fed

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"math" "math"
"math/rand"
"sync" "sync"
"time" "time"
@ -30,10 +31,6 @@ func init() {
bbgo.RegisterStrategy(ID, &Strategy{}) bbgo.RegisterStrategy(ID, &Strategy{})
} }
func (s *Strategy) ID() string {
return ID
}
type State struct { type State struct {
HedgePosition fixedpoint.Value `json:"hedgePosition"` HedgePosition fixedpoint.Value `json:"hedgePosition"`
Position *bbgo.Position `json:"position,omitempty"` Position *bbgo.Position `json:"position,omitempty"`
@ -82,6 +79,10 @@ type Strategy struct {
stopC chan struct{} stopC chan struct{}
} }
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) { func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {
sourceSession, ok := sessions[s.SourceExchange] sourceSession, ok := sessions[s.SourceExchange]
if !ok { if !ok {
@ -113,6 +114,7 @@ func (s *Strategy) updateQuote(ctx context.Context) {
if s.activeMakerOrders.NumOfAsks() > 0 || s.activeMakerOrders.NumOfBids() > 0 { if s.activeMakerOrders.NumOfAsks() > 0 || s.activeMakerOrders.NumOfBids() > 0 {
log.Warnf("there are some %s orders not canceled, skipping placing maker orders", s.Symbol) log.Warnf("there are some %s orders not canceled, skipping placing maker orders", s.Symbol)
s.activeMakerOrders.Print()
return return
} }
@ -472,11 +474,12 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
s.stopC = make(chan struct{}) s.stopC = make(chan struct{})
go func() { go func() {
posTicker := time.NewTicker(s.HedgeInterval.Duration()) posTicker := time.NewTicker(durationJitter(s.HedgeInterval.Duration(), 200))
defer posTicker.Stop() defer posTicker.Stop()
ticker := time.NewTicker(s.UpdateInterval.Duration()) quoteTicker := time.NewTicker(durationJitter(s.UpdateInterval.Duration(), 200))
defer ticker.Stop() defer quoteTicker.Stop()
for { for {
select { select {
@ -486,7 +489,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
case <-ctx.Done(): case <-ctx.Done():
return return
case <-ticker.C: case <-quoteTicker.C:
s.updateQuote(ctx) s.updateQuote(ctx)
case <-posTicker.C: case <-posTicker.C:
@ -521,6 +524,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
} }
log.Warnf("%d orders are not cancelled yet...", len(orders)) log.Warnf("%d orders are not cancelled yet...", len(orders))
s.activeMakerOrders.Print()
} }
if err := s.Persistence.Save(s.state, ID, s.Symbol, stateKey); err != nil { if err := s.Persistence.Save(s.state, ID, s.Symbol, stateKey); err != nil {
@ -533,3 +537,8 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
return nil return nil
} }
func durationJitter(d time.Duration, jitterInMilliseconds int) time.Duration {
n := rand.Intn(jitterInMilliseconds)
return d + time.Duration(n) * time.Millisecond
}