mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-24 07:45:15 +00:00
bbgo: apply logger into the order executor
This commit is contained in:
parent
385a97448d
commit
3cb190c2c7
|
@ -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
|
||||
|
||||
|
@ -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]
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -36,6 +36,8 @@ type GeneralOrderExecutor struct {
|
|||
orderStore *OrderStore
|
||||
tradeCollector *TradeCollector
|
||||
|
||||
logger log.FieldLogger
|
||||
|
||||
marginBaseMaxBorrowable, marginQuoteMaxBorrowable fixedpoint.Value
|
||||
|
||||
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) {
|
||||
formattedOrders, err := e.session.FormatOrders(submitOrders)
|
||||
if err != nil {
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user