Skip to content

Commit

Permalink
xgap: add MaxJitterQuantity and fix source book subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Nov 15, 2024
1 parent 69f49d6 commit 23db7fa
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions pkg/strategy/xgap/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ type Strategy struct {
SourceSymbol string `json:"sourceSymbol"`
SourceExchange string `json:"sourceExchange"`

MinSpread fixedpoint.Value `json:"minSpread"`
Quantity fixedpoint.Value `json:"quantity"`
DryRun bool `json:"dryRun"`
MinSpread fixedpoint.Value `json:"minSpread"`
Quantity fixedpoint.Value `json:"quantity"`
MaxJitterQuantity fixedpoint.Value `json:"maxJitterQuantity"`

DryRun bool `json:"dryRun"`

DailyMaxVolume fixedpoint.Value `json:"dailyMaxVolume,omitempty"`
DailyTargetVolume fixedpoint.Value `json:"dailyTargetVolume,omitempty"`
Expand Down Expand Up @@ -113,7 +115,7 @@ func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {
}

sourceSession.Subscribe(types.KLineChannel, s.SourceSymbol, types.SubscribeOptions{Interval: "1m"})
sourceSession.Subscribe(types.BookChannel, s.SourceSymbol, types.SubscribeOptions{Depth: types.DepthLevel5})
sourceSession.Subscribe(types.BookChannel, s.SourceSymbol, types.SubscribeOptions{Depth: types.DepthLevelFull})
}

tradingSession, ok := sessions[s.TradingExchange]
Expand Down Expand Up @@ -327,26 +329,30 @@ func (s *Strategy) placeOrders(ctx context.Context) {
quantity = fixedpoint.Max(s.Quantity, quantity)
} else if s.SimulateVolume {
s.mu.Lock()
if s.lastTradingKLine.Volume.Sign() > 0 && s.lastSourceKLine.Volume.Sign() > 0 {
lastTradingKLine := s.lastTradingKLine
lastSourceKLine := s.lastSourceKLine
s.mu.Unlock()

if lastTradingKLine.Volume.Sign() > 0 && lastSourceKLine.Volume.Sign() > 0 {
log.Infof("trading exchange %s price: %s volume: %s",
s.Symbol, s.lastTradingKLine.Close.String(), s.lastTradingKLine.Volume.String())
s.Symbol, lastTradingKLine.Close.String(), lastTradingKLine.Volume.String())
log.Infof("source exchange %s price: %s volume: %s",
s.Symbol, s.lastSourceKLine.Close.String(), s.lastSourceKLine.Volume.String())
s.Symbol, lastSourceKLine.Close.String(), lastSourceKLine.Volume.String())

volumeDiff := s.lastSourceKLine.Volume.Sub(lastTradingKLine.Volume)

volumeDiff := s.lastSourceKLine.Volume.Sub(s.lastTradingKLine.Volume)
// change the current quantity only diff is positive
if volumeDiff.Sign() > 0 {
quantity = volumeDiff
}
}
s.mu.Unlock()
} else if s.DailyTargetVolume.Sign() > 0 {
numOfTicks := (24 * time.Hour) / s.UpdateInterval.Duration()
quantity = fixedpoint.NewFromFloat(s.DailyTargetVolume.Float64() / float64(numOfTicks))
quantity = quantityJitter(quantity, 0.02)
} else {
// plus a 2% quantity jitter
quantity = quantityJitter(quantity, 0.02)
}

if s.MaxJitterQuantity.Sign() > 0 {
quantity = quantityJitter2(quantity, s.MaxJitterQuantity)
}

log.Infof("%s quantity: %f", s.Symbol, quantity.Float64())
Expand Down Expand Up @@ -398,3 +404,9 @@ func quantityJitter(q fixedpoint.Value, rg float64) fixedpoint.Value {
jitter := 1.0 + math.Max(rg, rand.Float64())
return q.Mul(fixedpoint.NewFromFloat(jitter))
}

func quantityJitter2(q, maxJitterQ fixedpoint.Value) fixedpoint.Value {
rg := maxJitterQ.Sub(q).Float64()
randQuantity := fixedpoint.NewFromFloat(q.Float64() + rg*rand.Float64())
return randQuantity
}

0 comments on commit 23db7fa

Please sign in to comment.