use retry package

This commit is contained in:
chiahung.lin 2023-12-06 16:16:17 +08:00
parent 4aa6ea3a46
commit c67737a6d6
2 changed files with 28 additions and 17 deletions

View File

@ -0,0 +1,17 @@
package retry
import (
"context"
"github.com/c9s/bbgo/pkg/types"
)
func QueryTickerUntilSuccessful(ctx context.Context, ex types.Exchange, symbol string) (ticker *types.Ticker, err error) {
var op = func() (err2 error) {
ticker, err2 = ex.QueryTicker(ctx, symbol)
return err2
}
err = GeneralBackoff(ctx, op)
return ticker, err
}

View File

@ -3,15 +3,15 @@ package dca2
import ( import (
"context" "context"
"fmt" "fmt"
"time"
"github.com/c9s/bbgo/pkg/exchange/retry"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
func (s *Strategy) placeDCAOrders(ctx context.Context) error { func (s *Strategy) placeDCAOrders(ctx context.Context) error {
s.logger.Infof("[DCA] start placing maker orders") s.logger.Infof("[DCA] start placing maker orders")
price, err := s.getBestPriceUntilSuccess(ctx, s.Short) price, err := getBestPriceUntilSuccess(ctx, s.Session.Exchange, s.Symbol, s.Short)
if err != nil { if err != nil {
return err return err
} }
@ -31,23 +31,17 @@ func (s *Strategy) placeDCAOrders(ctx context.Context) error {
return nil return nil
} }
func (s *Strategy) getBestPriceUntilSuccess(ctx context.Context, short bool) (fixedpoint.Value, error) { func getBestPriceUntilSuccess(ctx context.Context, ex types.Exchange, symbol string, short bool) (fixedpoint.Value, error) {
var err error ticker, err := retry.QueryTickerUntilSuccessful(ctx, ex, symbol)
var ticker *types.Ticker if err != nil {
for try := 1; try <= 100; try++ { return fixedpoint.Zero, err
ticker, err = s.Session.Exchange.QueryTicker(ctx, s.Symbol)
if err == nil && ticker != nil {
s.logger.Infof("ticker: %s", ticker.String())
if short {
return ticker.Buy, nil
}
return ticker.Sell, nil
}
time.Sleep(1 * time.Second)
} }
return fixedpoint.Zero, err if short {
return ticker.Buy, nil
} else {
return ticker.Sell, nil
}
} }
func (s *Strategy) generateDCAOrders(short bool, budget, price, priceDeviation fixedpoint.Value, maxOrderNum int64) ([]types.SubmitOrder, error) { func (s *Strategy) generateDCAOrders(short bool, budget, price, priceDeviation fixedpoint.Value, maxOrderNum int64) ([]types.SubmitOrder, error) {