@@ -221,8 +221,10 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
221
221
s .mu .Unlock ()
222
222
})
223
223
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
+ }
226
228
227
229
s .tradingBook = types .NewStreamBook (s .Symbol )
228
230
s .tradingBook .BindStream (s .tradingSession .MarketDataStream )
@@ -276,7 +278,7 @@ func (s *Strategy) placeOrders(ctx context.Context) {
276
278
spread .String (), spreadPercentage .Percentage ())
277
279
278
280
// 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 {
280
282
log .Warnf ("spread too large (%s %s), using source book" ,
281
283
spread .String (), spreadPercentage .Percentage ())
282
284
bestBid , hasBid = s .sourceBook .BestBid ()
@@ -299,7 +301,7 @@ func (s *Strategy) placeOrders(ctx context.Context) {
299
301
return
300
302
}
301
303
302
- } else {
304
+ } else if s . sourceBook != nil {
303
305
bestBid , hasBid = s .sourceBook .BestBid ()
304
306
bestAsk , hasAsk = s .sourceBook .BestAsk ()
305
307
}
@@ -309,9 +311,15 @@ func (s *Strategy) placeOrders(ctx context.Context) {
309
311
return
310
312
}
311
313
314
+ if bestBid .Price .IsZero () || bestAsk .Price .IsZero () {
315
+ log .Warn ("bid price or ask price is zero" )
316
+ return
317
+ }
318
+
312
319
var spread = bestAsk .Price .Sub (bestBid .Price )
313
320
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" ,
315
323
spread .String (), spreadPercentage .Percentage (),
316
324
bestAsk .Price .String (), bestBid .Price .String ())
317
325
// var spreadPercentage = spread.Float64() / bestBid.Price.Float64()
@@ -328,6 +336,7 @@ func (s *Strategy) placeOrders(ctx context.Context) {
328
336
log .Errorf ("base balance %s not found" , s .tradingMarket .BaseCurrency )
329
337
return
330
338
}
339
+
331
340
quoteBalance , ok := balances [s .tradingMarket .QuoteCurrency ]
332
341
if ! ok {
333
342
log .Errorf ("quote balance %s not found" , s .tradingMarket .QuoteCurrency )
@@ -346,9 +355,14 @@ func (s *Strategy) placeOrders(ctx context.Context) {
346
355
return
347
356
}
348
357
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
+
350
363
quantity := minQuantity
351
364
365
+ // if we set the fixed quantity, we should use the fixed
352
366
if s .Quantity .Sign () > 0 {
353
367
quantity = fixedpoint .Max (s .Quantity , quantity )
354
368
} else if s .SimulateVolume {
@@ -372,8 +386,12 @@ func (s *Strategy) placeOrders(ctx context.Context) {
372
386
quantity = quantity .Mul (fixedpoint .NewFromFloat (jitter ))
373
387
}
374
388
389
+ log .Infof ("%s quantity: %f" , s .Symbol , quantity .Float64 ())
390
+
375
391
quantity = fixedpoint .Min (quantity , maxQuantity )
376
392
393
+ log .Infof ("%s adjusted quantity: %f" , s .Symbol , quantity .Float64 ())
394
+
377
395
orderForms := []types.SubmitOrder {
378
396
{
379
397
Symbol : s .Symbol ,
0 commit comments