Skip to content

Commit 4ab2bf7

Browse files
committed
xmaker: add price checker and field logger
1 parent c8c1a09 commit 4ab2bf7

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

pkg/strategy/xmaker/strategy.go

+38-15
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ var defaultMargin = fixedpoint.NewFromFloat(0.003)
2626
var two = fixedpoint.NewFromInt(2)
2727

2828
var lastPriceModifier = fixedpoint.NewFromFloat(1.001)
29-
var minGap = fixedpoint.NewFromFloat(1.02)
3029

3130
const priceUpdateTimeout = 30 * time.Second
3231

@@ -134,6 +133,8 @@ type Strategy struct {
134133

135134
reportProfitStatsRateLimiter *rate.Limiter
136135
circuitBreakerAlertLimiter *rate.Limiter
136+
137+
logger logrus.FieldLogger
137138
}
138139

139140
func (s *Strategy) ID() string {
@@ -189,6 +190,12 @@ func aggregatePrice(pvs types.PriceVolumeSlice, requiredQuantity fixedpoint.Valu
189190
func (s *Strategy) Initialize() error {
190191
s.bidPriceHeartBeat = types.NewPriceHeartBeat(priceUpdateTimeout)
191192
s.askPriceHeartBeat = types.NewPriceHeartBeat(priceUpdateTimeout)
193+
194+
s.logger = logrus.WithFields(logrus.Fields{
195+
"symbol": s.Symbol,
196+
"strategy": ID,
197+
"strategy_id": s.InstanceID(),
198+
})
192199
return nil
193200
}
194201

@@ -208,7 +215,7 @@ func (s *Strategy) getBollingerTrend(quote *Quote) int {
208215
lastDownBand := fixedpoint.NewFromFloat(s.boll.DownBand.Last(0))
209216
lastUpBand := fixedpoint.NewFromFloat(s.boll.UpBand.Last(0))
210217

211-
log.Infof("bollinger band: up/down = %f/%f, bid/ask = %f/%f",
218+
s.logger.Infof("bollinger band: up/down = %f/%f, bid/ask = %f/%f",
212219
lastUpBand.Float64(),
213220
lastDownBand.Float64(),
214221
quote.BestBidPrice.Float64(),
@@ -231,7 +238,7 @@ func (s *Strategy) applyBollingerMargin(
231238
lastUpBand := fixedpoint.NewFromFloat(s.boll.UpBand.Last(0))
232239

233240
if lastUpBand.IsZero() || lastDownBand.IsZero() {
234-
log.Warnf("bollinger band value is zero, skipping")
241+
s.logger.Warnf("bollinger band value is zero, skipping")
235242
return nil
236243
}
237244

@@ -245,7 +252,7 @@ func (s *Strategy) applyBollingerMargin(
245252
// so that 1.x can multiply the original bid margin
246253
bollMargin := s.BollBandMargin.Mul(ratio).Mul(factor)
247254

248-
log.Infof("%s bollband downtrend: increasing bid margin %f (bidMargin) + %f (bollMargin) = %f (finalBidMargin)",
255+
s.logger.Infof("%s bollband downtrend: increasing bid margin %f (bidMargin) + %f (bollMargin) = %f (finalBidMargin)",
249256
s.Symbol,
250257
quote.BidMargin.Float64(),
251258
bollMargin.Float64(),
@@ -262,7 +269,7 @@ func (s *Strategy) applyBollingerMargin(
262269
// so that the original bid margin can be multiplied by 1.x
263270
bollMargin := s.BollBandMargin.Mul(ratio).Mul(factor)
264271

265-
log.Infof("%s bollband uptrend adjusting bid margin %f (askMargin) + %f (bollMargin) = %f (finalAskMargin)",
272+
s.logger.Infof("%s bollband uptrend adjusting bid margin %f (askMargin) + %f (bollMargin) = %f (finalAskMargin)",
266273
s.Symbol,
267274
quote.AskMargin.Float64(),
268275
bollMargin.Float64(),
@@ -281,7 +288,7 @@ func (s *Strategy) applyBollingerMargin(
281288

282289
func (s *Strategy) updateQuote(ctx context.Context) {
283290
if err := s.activeMakerOrders.GracefulCancel(ctx, s.makerSession.Exchange); err != nil {
284-
log.Warnf("there are some %s orders not canceled, skipping placing maker orders", s.Symbol)
291+
s.logger.Warnf("there are some %s orders not canceled, skipping placing maker orders", s.Symbol)
285292
s.activeMakerOrders.Print()
286293
return
287294
}
@@ -293,7 +300,7 @@ func (s *Strategy) updateQuote(ctx context.Context) {
293300
if s.CircuitBreaker != nil {
294301
now := time.Now()
295302
if reason, halted := s.CircuitBreaker.IsHalted(now); halted {
296-
log.Warnf("[arbWorker] strategy is halted, reason: %s", reason)
303+
s.logger.Warnf("[arbWorker] strategy is halted, reason: %s", reason)
297304

298305
if s.circuitBreakerAlertLimiter.AllowN(now, 1) {
299306
bbgo.Notify("Strategy is halted, reason: %s", reason)
@@ -316,22 +323,22 @@ func (s *Strategy) updateQuote(ctx context.Context) {
316323
bookLastUpdateTime := s.book.LastUpdateTime()
317324

318325
if _, err := s.bidPriceHeartBeat.Update(bestBid); err != nil {
319-
log.WithError(err).Errorf("quote update error, %s price not updating, order book last update: %s ago",
326+
s.logger.WithError(err).Errorf("quote update error, %s price not updating, order book last update: %s ago",
320327
s.Symbol,
321328
time.Since(bookLastUpdateTime))
322329
return
323330
}
324331

325332
if _, err := s.askPriceHeartBeat.Update(bestAsk); err != nil {
326-
log.WithError(err).Errorf("quote update error, %s price not updating, order book last update: %s ago",
333+
s.logger.WithError(err).Errorf("quote update error, %s price not updating, order book last update: %s ago",
327334
s.Symbol,
328335
time.Since(bookLastUpdateTime))
329336
return
330337
}
331338

332339
sourceBook := s.book.CopyDepth(10)
333340
if valid, err := sourceBook.IsValid(); !valid {
334-
log.WithError(err).Errorf("%s invalid copied order book, skip quoting: %v", s.Symbol, err)
341+
s.logger.WithError(err).Errorf("%s invalid copied order book, skip quoting: %v", s.Symbol, err)
335342
return
336343
}
337344

@@ -369,13 +376,13 @@ func (s *Strategy) updateQuote(ctx context.Context) {
369376
if b.Available.Compare(minAvailable) > 0 {
370377
hedgeQuota.BaseAsset.Add(b.Available.Sub(minAvailable))
371378
} else {
372-
log.Warnf("%s maker bid disabled: insufficient base balance %s", s.Symbol, b.String())
379+
s.logger.Warnf("%s maker bid disabled: insufficient base balance %s", s.Symbol, b.String())
373380
disableMakerBid = true
374381
}
375382
} else if b.Available.Compare(s.sourceMarket.MinQuantity) > 0 {
376383
hedgeQuota.BaseAsset.Add(b.Available)
377384
} else {
378-
log.Warnf("%s maker bid disabled: insufficient base balance %s", s.Symbol, b.String())
385+
s.logger.Warnf("%s maker bid disabled: insufficient base balance %s", s.Symbol, b.String())
379386
disableMakerBid = true
380387
}
381388
}
@@ -388,13 +395,13 @@ func (s *Strategy) updateQuote(ctx context.Context) {
388395
if b.Available.Compare(minAvailable) > 0 {
389396
hedgeQuota.QuoteAsset.Add(b.Available.Sub(minAvailable))
390397
} else {
391-
log.Warnf("%s maker ask disabled: insufficient quote balance %s", s.Symbol, b.String())
398+
s.logger.Warnf("%s maker ask disabled: insufficient quote balance %s", s.Symbol, b.String())
392399
disableMakerAsk = true
393400
}
394401
} else if b.Available.Compare(s.sourceMarket.MinNotional) > 0 {
395402
hedgeQuota.QuoteAsset.Add(b.Available)
396403
} else {
397-
log.Warnf("%s maker ask disabled: insufficient quote balance %s", s.Symbol, b.String())
404+
s.logger.Warnf("%s maker ask disabled: insufficient quote balance %s", s.Symbol, b.String())
398405
disableMakerAsk = true
399406
}
400407
}
@@ -421,7 +428,15 @@ func (s *Strategy) updateQuote(ctx context.Context) {
421428

422429
bestBidPrice := bestBid.Price
423430
bestAskPrice := bestAsk.Price
424-
log.Infof("%s book ticker: best ask / best bid = %v / %v", s.Symbol, bestAskPrice, bestBidPrice)
431+
s.logger.Infof("%s book ticker: best ask / best bid = %v / %v", s.Symbol, bestAskPrice, bestBidPrice)
432+
433+
if bestBidPrice.Compare(bestAskPrice) > 0 {
434+
log.Errorf("best bid price %f is higher than best ask price %f, skip quoting",
435+
bestBidPrice.Float64(),
436+
bestAskPrice.Float64(),
437+
)
438+
return
439+
}
425440

426441
var submitOrders []types.SubmitOrder
427442
var accumulativeBidQuantity, accumulativeAskQuantity fixedpoint.Value
@@ -455,6 +470,14 @@ func (s *Strategy) updateQuote(ctx context.Context) {
455470
bidPrice := quote.BestBidPrice
456471
askPrice := quote.BestAskPrice
457472

473+
if bidPrice.Compare(askPrice) > 0 {
474+
log.Errorf("maker bid price %f is higher than maker ask price %f, skip quoting",
475+
bidPrice.Float64(),
476+
askPrice.Float64(),
477+
)
478+
return
479+
}
480+
458481
bidMarginMetrics.With(labels).Set(quote.BidMargin.Float64())
459482
askMarginMetrics.With(labels).Set(quote.AskMargin.Float64())
460483

0 commit comments

Comments
 (0)