Skip to content

Commit cb4df28

Browse files
authored
Merge pull request #1653 from c9s/c9s/xgap-make-source-book-optional
FIX: [xgap] make sourceBook optional
2 parents 7a4f934 + a5831bb commit cb4df28

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

pkg/strategy/xgap/strategy.go

+24-6
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
221221
s.mu.Unlock()
222222
})
223223

224-
s.sourceBook = types.NewStreamBook(s.Symbol)
225-
s.sourceBook.BindStream(s.sourceSession.MarketDataStream)
224+
if s.SourceExchange != "" {
225+
s.sourceBook = types.NewStreamBook(s.Symbol)
226+
s.sourceBook.BindStream(s.sourceSession.MarketDataStream)
227+
}
226228

227229
s.tradingBook = types.NewStreamBook(s.Symbol)
228230
s.tradingBook.BindStream(s.tradingSession.MarketDataStream)
@@ -276,7 +278,7 @@ func (s *Strategy) placeOrders(ctx context.Context) {
276278
spread.String(), spreadPercentage.Percentage())
277279

278280
// use the source book price if the spread percentage greater than 5%
279-
if s.SimulatePrice && spreadPercentage.Compare(maxStepPercentageGap) > 0 {
281+
if s.SimulatePrice && s.sourceBook != nil && spreadPercentage.Compare(maxStepPercentageGap) > 0 {
280282
log.Warnf("spread too large (%s %s), using source book",
281283
spread.String(), spreadPercentage.Percentage())
282284
bestBid, hasBid = s.sourceBook.BestBid()
@@ -299,7 +301,7 @@ func (s *Strategy) placeOrders(ctx context.Context) {
299301
return
300302
}
301303

302-
} else {
304+
} else if s.sourceBook != nil {
303305
bestBid, hasBid = s.sourceBook.BestBid()
304306
bestAsk, hasAsk = s.sourceBook.BestAsk()
305307
}
@@ -309,9 +311,15 @@ func (s *Strategy) placeOrders(ctx context.Context) {
309311
return
310312
}
311313

314+
if bestBid.Price.IsZero() || bestAsk.Price.IsZero() {
315+
log.Warn("bid price or ask price is zero")
316+
return
317+
}
318+
312319
var spread = bestAsk.Price.Sub(bestBid.Price)
313320
var spreadPercentage = spread.Div(bestAsk.Price)
314-
log.Infof("spread=%s %s ask=%s bid=%s",
321+
322+
log.Infof("spread:%s %s ask:%s bid:%s",
315323
spread.String(), spreadPercentage.Percentage(),
316324
bestAsk.Price.String(), bestBid.Price.String())
317325
// var spreadPercentage = spread.Float64() / bestBid.Price.Float64()
@@ -328,6 +336,7 @@ func (s *Strategy) placeOrders(ctx context.Context) {
328336
log.Errorf("base balance %s not found", s.tradingMarket.BaseCurrency)
329337
return
330338
}
339+
331340
quoteBalance, ok := balances[s.tradingMarket.QuoteCurrency]
332341
if !ok {
333342
log.Errorf("quote balance %s not found", s.tradingMarket.QuoteCurrency)
@@ -346,9 +355,14 @@ func (s *Strategy) placeOrders(ctx context.Context) {
346355
return
347356
}
348357

349-
maxQuantity := fixedpoint.Min(baseBalance.Available, quoteBalance.Available.Div(price))
358+
maxQuantity := baseBalance.Available
359+
if !quoteBalance.Available.IsZero() {
360+
maxQuantity = fixedpoint.Min(maxQuantity, quoteBalance.Available.Div(price))
361+
}
362+
350363
quantity := minQuantity
351364

365+
// if we set the fixed quantity, we should use the fixed
352366
if s.Quantity.Sign() > 0 {
353367
quantity = fixedpoint.Max(s.Quantity, quantity)
354368
} else if s.SimulateVolume {
@@ -372,8 +386,12 @@ func (s *Strategy) placeOrders(ctx context.Context) {
372386
quantity = quantity.Mul(fixedpoint.NewFromFloat(jitter))
373387
}
374388

389+
log.Infof("%s quantity: %f", s.Symbol, quantity.Float64())
390+
375391
quantity = fixedpoint.Min(quantity, maxQuantity)
376392

393+
log.Infof("%s adjusted quantity: %f", s.Symbol, quantity.Float64())
394+
377395
orderForms := []types.SubmitOrder{
378396
{
379397
Symbol: s.Symbol,

0 commit comments

Comments
 (0)