Skip to content

Commit

Permalink
bbgo: apply backoff to submitOrders
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Feb 24, 2023
1 parent 3acb0a0 commit 18478cf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
36 changes: 23 additions & 13 deletions pkg/bbgo/order_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -343,25 +344,34 @@ 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)
}
}

// update the error index
errIdx = errIdxNext
}

return createdOrders, err
return createdOrders, werr
}
5 changes: 2 additions & 3 deletions pkg/bbgo/order_executor_general.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 18478cf

Please sign in to comment.