bbgo: apply logger into the order executor

This commit is contained in:
c9s 2023-03-02 16:16:14 +08:00
parent 385a97448d
commit 3cb190c2c7
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 20 additions and 6 deletions

View File

@ -310,7 +310,11 @@ func BatchPlaceOrder(ctx context.Context, exchange types.Exchange, submitOrders
type OrderCallback func(order types.Order) type OrderCallback func(order types.Order)
// BatchRetryPlaceOrder places the orders and retries the failed orders // 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 createdOrders types.OrderSlice
var werr error var werr error
@ -349,14 +353,14 @@ func BatchRetryPlaceOrder(ctx context.Context, exchange types.Exchange, errIdx [
batchRetryOrder: batchRetryOrder:
for retryRound := 0; len(errIdx) > 0 && retryRound < 10; retryRound++ { for retryRound := 0; len(errIdx) > 0 && retryRound < 10; retryRound++ {
// sleep for 200 millisecond between each retry // 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) time.Sleep(coolDownTime)
// reset error index since it's a new retry // reset error index since it's a new retry
errIdxNext = nil errIdxNext = nil
// iterate the error index and re-submit the order // 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 { for _, idx := range errIdx {
submitOrder := submitOrders[idx] submitOrder := submitOrders[idx]
@ -386,7 +390,7 @@ batchRetryOrder:
// bo = backoff.WithContext(bo, timeoutCtx) // bo = backoff.WithContext(bo, timeoutCtx)
if err2 := backoff.Retry(op, bo); err2 != nil { if err2 := backoff.Retry(op, bo); err2 != nil {
if err2 == context.Canceled { if err2 == context.Canceled {
log.Warnf("context canceled error, stop retry") logger.Warnf("context canceled error, stop retry")
break batchRetryOrder break batchRetryOrder
} }

View File

@ -36,6 +36,8 @@ type GeneralOrderExecutor struct {
orderStore *OrderStore orderStore *OrderStore
tradeCollector *TradeCollector tradeCollector *TradeCollector
logger log.FieldLogger
marginBaseMaxBorrowable, marginQuoteMaxBorrowable fixedpoint.Value marginBaseMaxBorrowable, marginQuoteMaxBorrowable fixedpoint.Value
disableNotify bool disableNotify bool
@ -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) { func (e *GeneralOrderExecutor) SubmitOrders(ctx context.Context, submitOrders ...types.SubmitOrder) (types.OrderSlice, error) {
formattedOrders, err := e.session.FormatOrders(submitOrders) formattedOrders, err := e.session.FormatOrders(submitOrders)
if err != nil { if err != nil {
@ -223,7 +229,7 @@ func (e *GeneralOrderExecutor) SubmitOrders(ctx context.Context, submitOrders ..
e.tradeCollector.Process() 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 return createdOrders, err
} }

View File

@ -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 // 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 // convert response
resp := &pb.SubmitOrderResponse{ resp := &pb.SubmitOrderResponse{

View File

@ -1641,6 +1641,10 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
}) })
orderExecutor.ActiveMakerOrders().OnFilled(s.newOrderUpdateHandler(ctx, session)) orderExecutor.ActiveMakerOrders().OnFilled(s.newOrderUpdateHandler(ctx, session))
if s.logger != nil {
orderExecutor.SetLogger(s.logger)
}
s.orderExecutor = orderExecutor s.orderExecutor = orderExecutor
s.OnGridProfit(func(stats *GridProfitStats, profit *GridProfit) { s.OnGridProfit(func(stats *GridProfitStats, profit *GridProfit) {