From 18478cf4c8c56a0736ab2a658d3c63fb080323ef Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 24 Feb 2023 12:50:43 +0800 Subject: [PATCH] bbgo: apply backoff to submitOrders --- go.mod | 1 + go.sum | 2 ++ pkg/bbgo/order_execution.go | 36 +++++++++++++++++++----------- pkg/bbgo/order_executor_general.go | 5 ++--- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index a5141178f6..e1b60948af 100644 --- a/go.mod +++ b/go.mod @@ -68,6 +68,7 @@ require ( github.com/bitly/go-simplejson v0.5.0 // indirect github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect + github.com/cenkalti/backoff/v4 v4.2.0 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/cockroachdb/apd v1.1.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect diff --git a/go.sum b/go.sum index 37585ff52c..b5698fb3d0 100644 --- a/go.sum +++ b/go.sum @@ -90,6 +90,8 @@ github.com/c9s/requestgen v1.3.0 h1:3cTHvWIlrc37nGEdJLIO07XaVidDeOwcew06csBz++U= github.com/c9s/requestgen v1.3.0/go.mod h1:5n9FU3hr5307IiXAmbMiZbHYaPiys1u9jCWYexZr9qA= github.com/c9s/rockhopper v1.2.2-0.20220617053729-ffdc87df194b h1:wT8c03PHLv7+nZUIGqxAzRvIfYHNxMCNVWwvdGkOXTs= github.com/c9s/rockhopper v1.2.2-0.20220617053729-ffdc87df194b/go.mod h1:EKObf66Cp7erWxym2de+07qNN5T1N9PXxHdh97N44EQ= +github.com/cenkalti/backoff/v4 v4.2.0 h1:HN5dHm3WBOgndBH6E8V0q2jIYIR3s9yglV8k/+MN3u4= +github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= diff --git a/pkg/bbgo/order_execution.go b/pkg/bbgo/order_execution.go index 6a72b23fbd..fe3e98169e 100644 --- a/pkg/bbgo/order_execution.go +++ b/pkg/bbgo/order_execution.go @@ -5,6 +5,7 @@ import ( "fmt" "time" + "github.com/cenkalti/backoff/v4" "github.com/pkg/errors" log "github.com/sirupsen/logrus" "go.uber.org/multierr" @@ -311,14 +312,14 @@ 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, error) { var createdOrders types.OrderSlice - var err error + var werr error // if the errIdx is nil, then we should iterate all the submit orders if len(errIdx) == 0 { for i, submitOrder := range submitOrders { createdOrder, err2 := exchange.SubmitOrder(ctx, submitOrder) if err2 != nil { - err = multierr.Append(err, err2) + werr = multierr.Append(werr, err2) errIdx = append(errIdx, i) } else if createdOrder != nil { // if the order is successfully created, than we should copy the order tag @@ -343,19 +344,28 @@ func BatchRetryPlaceOrder(ctx context.Context, exchange types.Exchange, errIdx [ // iterate the error index and re-submit the order for _, idx := range errIdx { submitOrder := submitOrders[idx] - createdOrder, err2 := exchange.SubmitOrder(ctx, submitOrder) - if err2 != nil { - err = multierr.Append(err, err2) - errIdxNext = append(errIdxNext, idx) - } else if createdOrder != nil { - // if the order is successfully created, than we should copy the order tag - createdOrder.Tag = submitOrder.Tag - if orderCallback != nil { - orderCallback(*createdOrder) + op := func() error { + // can allocate permanent error backoff.Permanent(err) to stop backoff + createdOrder, err2 := exchange.SubmitOrder(ctx, submitOrder) + if err2 == nil && createdOrder != nil { + // if the order is successfully created, than we should copy the order tag + createdOrder.Tag = submitOrder.Tag + + if orderCallback != nil { + orderCallback(*createdOrder) + } + + createdOrders = append(createdOrders, *createdOrder) } - createdOrders = append(createdOrders, *createdOrder) + return err2 + } + + // if err2 := backoff.Retry(op, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 99)); err2 != nil { + if err2 := backoff.Retry(op, backoff.NewExponentialBackOff()); err2 != nil { + werr = multierr.Append(werr, err2) + errIdxNext = append(errIdxNext, idx) } } @@ -363,5 +373,5 @@ func BatchRetryPlaceOrder(ctx context.Context, exchange types.Exchange, errIdx [ errIdx = errIdxNext } - return createdOrders, err + return createdOrders, werr } diff --git a/pkg/bbgo/order_executor_general.go b/pkg/bbgo/order_executor_general.go index 28ddce61ee..746c45ed1a 100644 --- a/pkg/bbgo/order_executor_general.go +++ b/pkg/bbgo/order_executor_general.go @@ -220,11 +220,10 @@ func (e *GeneralOrderExecutor) SubmitOrders(ctx context.Context, submitOrders .. orderCreateCallback := func(createdOrder types.Order) { e.orderStore.Add(createdOrder) e.activeMakerOrders.Add(createdOrder) + e.tradeCollector.Process() } - createdOrders, err := BatchRetryPlaceOrder(ctx, e.session.Exchange, nil, orderCreateCallback, formattedOrders...) - e.tradeCollector.Process() - return createdOrders, err + return BatchRetryPlaceOrder(ctx, e.session.Exchange, nil, orderCreateCallback, formattedOrders...) } type OpenPositionOptions struct {