mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
use retry package
This commit is contained in:
parent
4aa6ea3a46
commit
c67737a6d6
17
pkg/exchange/retry/ticker.go
Normal file
17
pkg/exchange/retry/ticker.go
Normal 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
|
||||
}
|
|
@ -3,15 +3,15 @@ package dca2
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/exchange/retry"
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
func (s *Strategy) placeDCAOrders(ctx context.Context) error {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
@ -31,23 +31,17 @@ func (s *Strategy) placeDCAOrders(ctx context.Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *Strategy) getBestPriceUntilSuccess(ctx context.Context, short bool) (fixedpoint.Value, error) {
|
||||
var err error
|
||||
var ticker *types.Ticker
|
||||
for try := 1; try <= 100; try++ {
|
||||
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)
|
||||
func getBestPriceUntilSuccess(ctx context.Context, ex types.Exchange, symbol string, short bool) (fixedpoint.Value, error) {
|
||||
ticker, err := retry.QueryTickerUntilSuccessful(ctx, ex, symbol)
|
||||
if err != nil {
|
||||
return fixedpoint.Zero, err
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user