From 94c126dd833cfeb9912949d7d39f1e979f3a8d21 Mon Sep 17 00:00:00 2001 From: narumi Date: Wed, 17 Apr 2024 15:27:46 +0800 Subject: [PATCH] move logerr to util --- pkg/strategy/atrpin/strategy.go | 18 ------------ pkg/strategy/emacross/strategy.go | 25 +++-------------- pkg/strategy/liquiditymaker/strategy.go | 37 +++++++------------------ pkg/strategy/rsicross/strategy.go | 25 ++--------------- pkg/strategy/scmaker/strategy.go | 37 +++++++------------------ pkg/util/logerr.go | 23 +++++++++++++++ 6 files changed, 50 insertions(+), 115 deletions(-) create mode 100644 pkg/util/logerr.go diff --git a/pkg/strategy/atrpin/strategy.go b/pkg/strategy/atrpin/strategy.go index 87b8bdf49..c5c8dbedf 100644 --- a/pkg/strategy/atrpin/strategy.go +++ b/pkg/strategy/atrpin/strategy.go @@ -199,21 +199,3 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se return nil } - -func logErr(err error, msgAndArgs ...interface{}) bool { - if err == nil { - return false - } - - if len(msgAndArgs) == 0 { - log.WithError(err).Error(err.Error()) - } else if len(msgAndArgs) == 1 { - msg := msgAndArgs[0].(string) - log.WithError(err).Error(msg) - } else if len(msgAndArgs) > 1 { - msg := msgAndArgs[0].(string) - log.WithError(err).Errorf(msg, msgAndArgs[1:]...) - } - - return true -} diff --git a/pkg/strategy/emacross/strategy.go b/pkg/strategy/emacross/strategy.go index 6762a4419..1f0dda77d 100644 --- a/pkg/strategy/emacross/strategy.go +++ b/pkg/strategy/emacross/strategy.go @@ -9,9 +9,10 @@ import ( "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" - "github.com/c9s/bbgo/pkg/indicator/v2" + indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2" "github.com/c9s/bbgo/pkg/strategy/common" "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/util" ) const ID = "emacross" @@ -85,10 +86,10 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. opts.Tags = []string{"emaCrossOver"} _, err := s.Strategy.OrderExecutor.OpenPosition(ctx, opts) - logErr(err, "unable to open position") + util.LogErr(err, "unable to open position") case indicatorv2.CrossUnder: err := s.Strategy.OrderExecutor.ClosePosition(ctx, fixedpoint.One) - logErr(err, "unable to submit close position order") + util.LogErr(err, "unable to submit close position order") } }) @@ -98,21 +99,3 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. return nil } - -func logErr(err error, msgAndArgs ...interface{}) bool { - if err == nil { - return false - } - - if len(msgAndArgs) == 0 { - log.WithError(err).Error(err.Error()) - } else if len(msgAndArgs) == 1 { - msg := msgAndArgs[0].(string) - log.WithError(err).Error(msg) - } else if len(msgAndArgs) > 1 { - msg := msgAndArgs[0].(string) - log.WithError(err).Errorf(msg, msgAndArgs[1:]...) - } - - return true -} diff --git a/pkg/strategy/liquiditymaker/strategy.go b/pkg/strategy/liquiditymaker/strategy.go index 187082d79..144753252 100644 --- a/pkg/strategy/liquiditymaker/strategy.go +++ b/pkg/strategy/liquiditymaker/strategy.go @@ -12,6 +12,7 @@ import ( . "github.com/c9s/bbgo/pkg/indicator/v2" "github.com/c9s/bbgo/pkg/strategy/common" "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/util" ) const ID = "liquiditymaker" @@ -137,11 +138,11 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. defer wg.Done() if err := s.liquidityOrderBook.GracefulCancel(ctx, s.Session.Exchange); err != nil { - logErr(err, "unable to cancel liquidity orders") + util.LogErr(err, "unable to cancel liquidity orders") } if err := s.adjustmentOrderBook.GracefulCancel(ctx, s.Session.Exchange); err != nil { - logErr(err, "unable to cancel adjustment orders") + util.LogErr(err, "unable to cancel adjustment orders") } }) @@ -156,12 +157,12 @@ func (s *Strategy) placeAdjustmentOrders(ctx context.Context) { } ticker, err := s.Session.Exchange.QueryTicker(ctx, s.Symbol) - if logErr(err, "unable to query ticker") { + if util.LogErr(err, "unable to query ticker") { return } if _, err := s.Session.UpdateAccount(ctx); err != nil { - logErr(err, "unable to update account") + util.LogErr(err, "unable to update account") return } @@ -211,7 +212,7 @@ func (s *Strategy) placeAdjustmentOrders(ctx context.Context) { } createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, adjOrders...) - if logErr(err, "unable to place liquidity orders") { + if util.LogErr(err, "unable to place liquidity orders") { return } @@ -220,12 +221,12 @@ func (s *Strategy) placeAdjustmentOrders(ctx context.Context) { func (s *Strategy) placeLiquidityOrders(ctx context.Context) { err := s.liquidityOrderBook.GracefulCancel(ctx, s.Session.Exchange) - if logErr(err, "unable to cancel orders") { + if util.LogErr(err, "unable to cancel orders") { return } ticker, err := s.Session.Exchange.QueryTicker(ctx, s.Symbol) - if logErr(err, "unable to query ticker") { + if util.LogErr(err, "unable to query ticker") { return } @@ -235,7 +236,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { } if _, err := s.Session.UpdateAccount(ctx); err != nil { - logErr(err, "unable to update account") + util.LogErr(err, "unable to update account") return } @@ -310,7 +311,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { orderForms := append(bidOrders, askOrders...) createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, orderForms...) - if logErr(err, "unable to place liquidity orders") { + if util.LogErr(err, "unable to place liquidity orders") { return } @@ -353,24 +354,6 @@ func filterAskOrders(askOrders []types.SubmitOrder, available fixedpoint.Value) return out } -func logErr(err error, msgAndArgs ...interface{}) bool { - if err == nil { - return false - } - - if len(msgAndArgs) == 0 { - log.WithError(err).Error(err.Error()) - } else if len(msgAndArgs) == 1 { - msg := msgAndArgs[0].(string) - log.WithError(err).Error(msg) - } else if len(msgAndArgs) > 1 { - msg := msgAndArgs[0].(string) - log.WithError(err).Errorf(msg, msgAndArgs[1:]...) - } - - return true -} - func preloadKLines( inc *KLineStream, session *bbgo.ExchangeSession, symbol string, interval types.Interval, ) { diff --git a/pkg/strategy/rsicross/strategy.go b/pkg/strategy/rsicross/strategy.go index 2ed468354..b0249d924 100644 --- a/pkg/strategy/rsicross/strategy.go +++ b/pkg/strategy/rsicross/strategy.go @@ -5,13 +5,12 @@ import ( "fmt" "sync" - log "github.com/sirupsen/logrus" - "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2" "github.com/c9s/bbgo/pkg/strategy/common" "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/util" ) const ID = "rsicross" @@ -79,7 +78,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se // opts.Price = closePrice opts.Tags = []string{"rsiCrossOver"} if _, err := s.OrderExecutor.OpenPosition(ctx, opts); err != nil { - logErr(err, "unable to open position") + util.LogErr(err, "unable to open position") } case indicatorv2.CrossUnder: @@ -88,7 +87,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se } if err := s.OrderExecutor.ClosePosition(ctx, fixedpoint.One); err != nil { - logErr(err, "failed to close position") + util.LogErr(err, "failed to close position") } } @@ -100,21 +99,3 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se return nil } - -func logErr(err error, msgAndArgs ...interface{}) bool { - if err == nil { - return false - } - - if len(msgAndArgs) == 0 { - log.WithError(err).Error(err.Error()) - } else if len(msgAndArgs) == 1 { - msg := msgAndArgs[0].(string) - log.WithError(err).Error(msg) - } else if len(msgAndArgs) > 1 { - msg := msgAndArgs[0].(string) - log.WithError(err).Errorf(msg, msgAndArgs[1:]...) - } - - return true -} diff --git a/pkg/strategy/scmaker/strategy.go b/pkg/strategy/scmaker/strategy.go index 5292ef662..ec7ee55a7 100644 --- a/pkg/strategy/scmaker/strategy.go +++ b/pkg/strategy/scmaker/strategy.go @@ -13,6 +13,7 @@ import ( . "github.com/c9s/bbgo/pkg/indicator/v2" "github.com/c9s/bbgo/pkg/strategy/common" "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/util" ) const ID = "scmaker" @@ -143,10 +144,10 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. defer wg.Done() err := s.liquidityOrderBook.GracefulCancel(ctx, s.Session.Exchange) - logErr(err, "unable to cancel liquidity orders") + util.LogErr(err, "unable to cancel liquidity orders") err = s.adjustmentOrderBook.GracefulCancel(ctx, s.Session.Exchange) - logErr(err, "unable to cancel adjustment orders") + util.LogErr(err, "unable to cancel adjustment orders") }) return nil @@ -194,12 +195,12 @@ func (s *Strategy) placeAdjustmentOrders(ctx context.Context) { } ticker, err := s.Session.Exchange.QueryTicker(ctx, s.Symbol) - if logErr(err, "unable to query ticker") { + if util.LogErr(err, "unable to query ticker") { return } if _, err := s.Session.UpdateAccount(ctx); err != nil { - logErr(err, "unable to update account") + util.LogErr(err, "unable to update account") return } @@ -249,7 +250,7 @@ func (s *Strategy) placeAdjustmentOrders(ctx context.Context) { } createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, adjOrders...) - if logErr(err, "unable to place liquidity orders") { + if util.LogErr(err, "unable to place liquidity orders") { return } @@ -258,7 +259,7 @@ func (s *Strategy) placeAdjustmentOrders(ctx context.Context) { func (s *Strategy) placeLiquidityOrders(ctx context.Context) { ticker, err := s.Session.Exchange.QueryTicker(ctx, s.Symbol) - if logErr(err, "unable to query ticker") { + if util.LogErr(err, "unable to query ticker") { return } @@ -268,7 +269,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { } err = s.liquidityOrderBook.GracefulCancel(ctx, s.Session.Exchange) - if logErr(err, "unable to cancel orders") { + if util.LogErr(err, "unable to cancel orders") { return } @@ -282,7 +283,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { } if _, err := s.Session.UpdateAccount(ctx); err != nil { - logErr(err, "unable to update account") + util.LogErr(err, "unable to update account") return } @@ -448,7 +449,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { makerQuota.Commit() createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, liqOrders...) - if logErr(err, "unable to place liquidity orders") { + if util.LogErr(err, "unable to place liquidity orders") { return } @@ -473,21 +474,3 @@ func profitProtectedPrice( } return price } - -func logErr(err error, msgAndArgs ...interface{}) bool { - if err == nil { - return false - } - - if len(msgAndArgs) == 0 { - log.WithError(err).Error(err.Error()) - } else if len(msgAndArgs) == 1 { - msg := msgAndArgs[0].(string) - log.WithError(err).Error(msg) - } else if len(msgAndArgs) > 1 { - msg := msgAndArgs[0].(string) - log.WithError(err).Errorf(msg, msgAndArgs[1:]...) - } - - return true -} diff --git a/pkg/util/logerr.go b/pkg/util/logerr.go new file mode 100644 index 000000000..187416edc --- /dev/null +++ b/pkg/util/logerr.go @@ -0,0 +1,23 @@ +package util + +import ( + log "github.com/sirupsen/logrus" +) + +func LogErr(err error, msgAndArgs ...interface{}) bool { + if err == nil { + return false + } + + if len(msgAndArgs) == 0 { + log.WithError(err).Error(err.Error()) + } else if len(msgAndArgs) == 1 { + msg := msgAndArgs[0].(string) + log.WithError(err).Error(msg) + } else if len(msgAndArgs) > 1 { + msg := msgAndArgs[0].(string) + log.WithError(err).Errorf(msg, msgAndArgs[1:]...) + } + + return true +}