Skip to content

Commit

Permalink
bbgo: apply logger into the order executor
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Mar 2, 2023
1 parent 385a974 commit 3cb190c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
12 changes: 8 additions & 4 deletions pkg/bbgo/order_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,11 @@ func BatchPlaceOrder(ctx context.Context, exchange types.Exchange, submitOrders
type OrderCallback func(order types.Order)

// BatchRetryPlaceOrder places the orders and retries the failed orders
func BatchRetryPlaceOrder(ctx context.Context, exchange types.Exchange, errIdx []int, orderCallback OrderCallback, submitOrders ...types.SubmitOrder) (types.OrderSlice, []int, error) {
func BatchRetryPlaceOrder(ctx context.Context, exchange types.Exchange, errIdx []int, orderCallback OrderCallback, logger log.FieldLogger, submitOrders ...types.SubmitOrder) (types.OrderSlice, []int, error) {
if logger == nil {
logger = log.StandardLogger()
}

var createdOrders types.OrderSlice
var werr error

Expand Down Expand Up @@ -349,14 +353,14 @@ func BatchRetryPlaceOrder(ctx context.Context, exchange types.Exchange, errIdx [
batchRetryOrder:
for retryRound := 0; len(errIdx) > 0 && retryRound < 10; retryRound++ {
// sleep for 200 millisecond between each retry
log.Warnf("retry round #%d, cooling down for %s", retryRound+1, coolDownTime)
logger.Warnf("retry round #%d, cooling down for %s", retryRound+1, coolDownTime)
time.Sleep(coolDownTime)

// reset error index since it's a new retry
errIdxNext = nil

// iterate the error index and re-submit the order
log.Warnf("starting retry round #%d...", retryRound+1)
logger.Warnf("starting retry round #%d...", retryRound+1)
for _, idx := range errIdx {
submitOrder := submitOrders[idx]

Expand Down Expand Up @@ -386,7 +390,7 @@ batchRetryOrder:
// bo = backoff.WithContext(bo, timeoutCtx)
if err2 := backoff.Retry(op, bo); err2 != nil {
if err2 == context.Canceled {
log.Warnf("context canceled error, stop retry")
logger.Warnf("context canceled error, stop retry")
break batchRetryOrder
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/bbgo/order_executor_general.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type GeneralOrderExecutor struct {
orderStore *OrderStore
tradeCollector *TradeCollector

logger log.FieldLogger

marginBaseMaxBorrowable, marginQuoteMaxBorrowable fixedpoint.Value

disableNotify bool
Expand Down Expand Up @@ -211,6 +213,10 @@ func (e *GeneralOrderExecutor) FastSubmitOrders(ctx context.Context, submitOrder

}

func (e *GeneralOrderExecutor) SetLogger(logger log.FieldLogger) {
e.logger = logger
}

func (e *GeneralOrderExecutor) SubmitOrders(ctx context.Context, submitOrders ...types.SubmitOrder) (types.OrderSlice, error) {
formattedOrders, err := e.session.FormatOrders(submitOrders)
if err != nil {
Expand All @@ -223,7 +229,7 @@ func (e *GeneralOrderExecutor) SubmitOrders(ctx context.Context, submitOrders ..
e.tradeCollector.Process()
}

createdOrders, _, err := BatchRetryPlaceOrder(ctx, e.session.Exchange, nil, orderCreateCallback, formattedOrders...)
createdOrders, _, err := BatchRetryPlaceOrder(ctx, e.session.Exchange, nil, orderCreateCallback, log.StandardLogger(), formattedOrders...)
return createdOrders, err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *TradingService) SubmitOrder(ctx context.Context, request *pb.SubmitOrde
}

// we will return this error later because some orders could be succeeded
createdOrders, _, err := bbgo.BatchRetryPlaceOrder(ctx, session.Exchange, nil, nil, submitOrders...)
createdOrders, _, err := bbgo.BatchRetryPlaceOrder(ctx, session.Exchange, nil, nil, log.StandardLogger(), submitOrders...)

// convert response
resp := &pb.SubmitOrderResponse{
Expand Down
4 changes: 4 additions & 0 deletions pkg/strategy/grid2/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,10 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
})
orderExecutor.ActiveMakerOrders().OnFilled(s.newOrderUpdateHandler(ctx, session))

if s.logger != nil {
orderExecutor.SetLogger(s.logger)
}

s.orderExecutor = orderExecutor

s.OnGridProfit(func(stats *GridProfitStats, profit *GridProfit) {
Expand Down

0 comments on commit 3cb190c

Please sign in to comment.