move logerr to util

This commit is contained in:
narumi 2024-04-17 15:27:46 +08:00
parent fe53319817
commit 94c126dd83
6 changed files with 50 additions and 115 deletions

View File

@ -199,21 +199,3 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
return nil 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
}

View File

@ -9,9 +9,10 @@ import (
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint" "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/strategy/common"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
) )
const ID = "emacross" const ID = "emacross"
@ -85,10 +86,10 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
opts.Tags = []string{"emaCrossOver"} opts.Tags = []string{"emaCrossOver"}
_, err := s.Strategy.OrderExecutor.OpenPosition(ctx, opts) _, err := s.Strategy.OrderExecutor.OpenPosition(ctx, opts)
logErr(err, "unable to open position") util.LogErr(err, "unable to open position")
case indicatorv2.CrossUnder: case indicatorv2.CrossUnder:
err := s.Strategy.OrderExecutor.ClosePosition(ctx, fixedpoint.One) 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 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
}

View File

@ -12,6 +12,7 @@ import (
. "github.com/c9s/bbgo/pkg/indicator/v2" . "github.com/c9s/bbgo/pkg/indicator/v2"
"github.com/c9s/bbgo/pkg/strategy/common" "github.com/c9s/bbgo/pkg/strategy/common"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
) )
const ID = "liquiditymaker" const ID = "liquiditymaker"
@ -137,11 +138,11 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
defer wg.Done() defer wg.Done()
if err := s.liquidityOrderBook.GracefulCancel(ctx, s.Session.Exchange); err != nil { 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 { 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) 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 return
} }
if _, err := s.Session.UpdateAccount(ctx); err != nil { if _, err := s.Session.UpdateAccount(ctx); err != nil {
logErr(err, "unable to update account") util.LogErr(err, "unable to update account")
return return
} }
@ -211,7 +212,7 @@ func (s *Strategy) placeAdjustmentOrders(ctx context.Context) {
} }
createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, adjOrders...) 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 return
} }
@ -220,12 +221,12 @@ func (s *Strategy) placeAdjustmentOrders(ctx context.Context) {
func (s *Strategy) placeLiquidityOrders(ctx context.Context) { func (s *Strategy) placeLiquidityOrders(ctx context.Context) {
err := s.liquidityOrderBook.GracefulCancel(ctx, s.Session.Exchange) err := s.liquidityOrderBook.GracefulCancel(ctx, s.Session.Exchange)
if logErr(err, "unable to cancel orders") { if util.LogErr(err, "unable to cancel orders") {
return return
} }
ticker, err := s.Session.Exchange.QueryTicker(ctx, s.Symbol) 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 return
} }
@ -235,7 +236,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) {
} }
if _, err := s.Session.UpdateAccount(ctx); err != nil { if _, err := s.Session.UpdateAccount(ctx); err != nil {
logErr(err, "unable to update account") util.LogErr(err, "unable to update account")
return return
} }
@ -310,7 +311,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) {
orderForms := append(bidOrders, askOrders...) orderForms := append(bidOrders, askOrders...)
createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, orderForms...) 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 return
} }
@ -353,24 +354,6 @@ func filterAskOrders(askOrders []types.SubmitOrder, available fixedpoint.Value)
return out 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( func preloadKLines(
inc *KLineStream, session *bbgo.ExchangeSession, symbol string, interval types.Interval, inc *KLineStream, session *bbgo.ExchangeSession, symbol string, interval types.Interval,
) { ) {

View File

@ -5,13 +5,12 @@ import (
"fmt" "fmt"
"sync" "sync"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
indicatorv2 "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/strategy/common"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
) )
const ID = "rsicross" const ID = "rsicross"
@ -79,7 +78,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
// opts.Price = closePrice // opts.Price = closePrice
opts.Tags = []string{"rsiCrossOver"} opts.Tags = []string{"rsiCrossOver"}
if _, err := s.OrderExecutor.OpenPosition(ctx, opts); err != nil { 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: 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 { 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 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
}

View File

@ -13,6 +13,7 @@ import (
. "github.com/c9s/bbgo/pkg/indicator/v2" . "github.com/c9s/bbgo/pkg/indicator/v2"
"github.com/c9s/bbgo/pkg/strategy/common" "github.com/c9s/bbgo/pkg/strategy/common"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
) )
const ID = "scmaker" const ID = "scmaker"
@ -143,10 +144,10 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
defer wg.Done() defer wg.Done()
err := s.liquidityOrderBook.GracefulCancel(ctx, s.Session.Exchange) 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) 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 return nil
@ -194,12 +195,12 @@ func (s *Strategy) placeAdjustmentOrders(ctx context.Context) {
} }
ticker, err := s.Session.Exchange.QueryTicker(ctx, s.Symbol) 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 return
} }
if _, err := s.Session.UpdateAccount(ctx); err != nil { if _, err := s.Session.UpdateAccount(ctx); err != nil {
logErr(err, "unable to update account") util.LogErr(err, "unable to update account")
return return
} }
@ -249,7 +250,7 @@ func (s *Strategy) placeAdjustmentOrders(ctx context.Context) {
} }
createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, adjOrders...) 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 return
} }
@ -258,7 +259,7 @@ func (s *Strategy) placeAdjustmentOrders(ctx context.Context) {
func (s *Strategy) placeLiquidityOrders(ctx context.Context) { func (s *Strategy) placeLiquidityOrders(ctx context.Context) {
ticker, err := s.Session.Exchange.QueryTicker(ctx, s.Symbol) 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 return
} }
@ -268,7 +269,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) {
} }
err = s.liquidityOrderBook.GracefulCancel(ctx, s.Session.Exchange) err = s.liquidityOrderBook.GracefulCancel(ctx, s.Session.Exchange)
if logErr(err, "unable to cancel orders") { if util.LogErr(err, "unable to cancel orders") {
return return
} }
@ -282,7 +283,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) {
} }
if _, err := s.Session.UpdateAccount(ctx); err != nil { if _, err := s.Session.UpdateAccount(ctx); err != nil {
logErr(err, "unable to update account") util.LogErr(err, "unable to update account")
return return
} }
@ -448,7 +449,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) {
makerQuota.Commit() makerQuota.Commit()
createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, liqOrders...) 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 return
} }
@ -473,21 +474,3 @@ func profitProtectedPrice(
} }
return price 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
}

23
pkg/util/logerr.go Normal file
View File

@ -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
}