@@ -26,7 +26,6 @@ var defaultMargin = fixedpoint.NewFromFloat(0.003)
26
26
var two = fixedpoint .NewFromInt (2 )
27
27
28
28
var lastPriceModifier = fixedpoint .NewFromFloat (1.001 )
29
- var minGap = fixedpoint .NewFromFloat (1.02 )
30
29
31
30
const priceUpdateTimeout = 30 * time .Second
32
31
@@ -134,6 +133,8 @@ type Strategy struct {
134
133
135
134
reportProfitStatsRateLimiter * rate.Limiter
136
135
circuitBreakerAlertLimiter * rate.Limiter
136
+
137
+ logger logrus.FieldLogger
137
138
}
138
139
139
140
func (s * Strategy ) ID () string {
@@ -189,6 +190,12 @@ func aggregatePrice(pvs types.PriceVolumeSlice, requiredQuantity fixedpoint.Valu
189
190
func (s * Strategy ) Initialize () error {
190
191
s .bidPriceHeartBeat = types .NewPriceHeartBeat (priceUpdateTimeout )
191
192
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
+ })
192
199
return nil
193
200
}
194
201
@@ -208,7 +215,7 @@ func (s *Strategy) getBollingerTrend(quote *Quote) int {
208
215
lastDownBand := fixedpoint .NewFromFloat (s .boll .DownBand .Last (0 ))
209
216
lastUpBand := fixedpoint .NewFromFloat (s .boll .UpBand .Last (0 ))
210
217
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" ,
212
219
lastUpBand .Float64 (),
213
220
lastDownBand .Float64 (),
214
221
quote .BestBidPrice .Float64 (),
@@ -231,7 +238,7 @@ func (s *Strategy) applyBollingerMargin(
231
238
lastUpBand := fixedpoint .NewFromFloat (s .boll .UpBand .Last (0 ))
232
239
233
240
if lastUpBand .IsZero () || lastDownBand .IsZero () {
234
- log .Warnf ("bollinger band value is zero, skipping" )
241
+ s . logger .Warnf ("bollinger band value is zero, skipping" )
235
242
return nil
236
243
}
237
244
@@ -245,7 +252,7 @@ func (s *Strategy) applyBollingerMargin(
245
252
// so that 1.x can multiply the original bid margin
246
253
bollMargin := s .BollBandMargin .Mul (ratio ).Mul (factor )
247
254
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)" ,
249
256
s .Symbol ,
250
257
quote .BidMargin .Float64 (),
251
258
bollMargin .Float64 (),
@@ -262,7 +269,7 @@ func (s *Strategy) applyBollingerMargin(
262
269
// so that the original bid margin can be multiplied by 1.x
263
270
bollMargin := s .BollBandMargin .Mul (ratio ).Mul (factor )
264
271
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)" ,
266
273
s .Symbol ,
267
274
quote .AskMargin .Float64 (),
268
275
bollMargin .Float64 (),
@@ -281,7 +288,7 @@ func (s *Strategy) applyBollingerMargin(
281
288
282
289
func (s * Strategy ) updateQuote (ctx context.Context ) {
283
290
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 )
285
292
s .activeMakerOrders .Print ()
286
293
return
287
294
}
@@ -293,7 +300,7 @@ func (s *Strategy) updateQuote(ctx context.Context) {
293
300
if s .CircuitBreaker != nil {
294
301
now := time .Now ()
295
302
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 )
297
304
298
305
if s .circuitBreakerAlertLimiter .AllowN (now , 1 ) {
299
306
bbgo .Notify ("Strategy is halted, reason: %s" , reason )
@@ -316,22 +323,22 @@ func (s *Strategy) updateQuote(ctx context.Context) {
316
323
bookLastUpdateTime := s .book .LastUpdateTime ()
317
324
318
325
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" ,
320
327
s .Symbol ,
321
328
time .Since (bookLastUpdateTime ))
322
329
return
323
330
}
324
331
325
332
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" ,
327
334
s .Symbol ,
328
335
time .Since (bookLastUpdateTime ))
329
336
return
330
337
}
331
338
332
339
sourceBook := s .book .CopyDepth (10 )
333
340
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 )
335
342
return
336
343
}
337
344
@@ -369,13 +376,13 @@ func (s *Strategy) updateQuote(ctx context.Context) {
369
376
if b .Available .Compare (minAvailable ) > 0 {
370
377
hedgeQuota .BaseAsset .Add (b .Available .Sub (minAvailable ))
371
378
} 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 ())
373
380
disableMakerBid = true
374
381
}
375
382
} else if b .Available .Compare (s .sourceMarket .MinQuantity ) > 0 {
376
383
hedgeQuota .BaseAsset .Add (b .Available )
377
384
} 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 ())
379
386
disableMakerBid = true
380
387
}
381
388
}
@@ -388,13 +395,13 @@ func (s *Strategy) updateQuote(ctx context.Context) {
388
395
if b .Available .Compare (minAvailable ) > 0 {
389
396
hedgeQuota .QuoteAsset .Add (b .Available .Sub (minAvailable ))
390
397
} 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 ())
392
399
disableMakerAsk = true
393
400
}
394
401
} else if b .Available .Compare (s .sourceMarket .MinNotional ) > 0 {
395
402
hedgeQuota .QuoteAsset .Add (b .Available )
396
403
} 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 ())
398
405
disableMakerAsk = true
399
406
}
400
407
}
@@ -421,7 +428,15 @@ func (s *Strategy) updateQuote(ctx context.Context) {
421
428
422
429
bestBidPrice := bestBid .Price
423
430
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
+ }
425
440
426
441
var submitOrders []types.SubmitOrder
427
442
var accumulativeBidQuantity , accumulativeAskQuantity fixedpoint.Value
@@ -455,6 +470,14 @@ func (s *Strategy) updateQuote(ctx context.Context) {
455
470
bidPrice := quote .BestBidPrice
456
471
askPrice := quote .BestAskPrice
457
472
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
+
458
481
bidMarginMetrics .With (labels ).Set (quote .BidMargin .Float64 ())
459
482
askMarginMetrics .With (labels ).Set (quote .AskMargin .Float64 ())
460
483
0 commit comments