mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
Merge pull request #1364 from bailantaotao/edwin/make-jump-to-option
FEATURE: [batch] add a jumpIfEmpty to batch trade option
This commit is contained in:
commit
d762366a83
12
pkg/exchange/batch/option.go
Normal file
12
pkg/exchange/batch/option.go
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
package batch
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Option func(query *AsyncTimeRangedBatchQuery)
|
||||||
|
|
||||||
|
// JumpIfEmpty jump the startTime + duration when the result is empty
|
||||||
|
func JumpIfEmpty(duration time.Duration) Option {
|
||||||
|
return func(query *AsyncTimeRangedBatchQuery) {
|
||||||
|
query.JumpIfEmpty = duration
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,7 +17,7 @@ type TradeBatchQuery struct {
|
||||||
types.ExchangeTradeHistoryService
|
types.ExchangeTradeHistoryService
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e TradeBatchQuery) Query(ctx context.Context, symbol string, options *types.TradeQueryOptions) (c chan types.Trade, errC chan error) {
|
func (e TradeBatchQuery) Query(ctx context.Context, symbol string, options *types.TradeQueryOptions, opts ...Option) (c chan types.Trade, errC chan error) {
|
||||||
if options.EndTime == nil {
|
if options.EndTime == nil {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
options.EndTime = &now
|
options.EndTime = &now
|
||||||
|
@ -45,6 +45,10 @@ func (e TradeBatchQuery) Query(ctx context.Context, symbol string, options *type
|
||||||
JumpIfEmpty: 24 * time.Hour,
|
JumpIfEmpty: 24 * time.Hour,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(query)
|
||||||
|
}
|
||||||
|
|
||||||
c = make(chan types.Trade, 100)
|
c = make(chan types.Trade, 100)
|
||||||
errC = query.Query(ctx, c, startTime, endTime)
|
errC = query.Query(ctx, c, startTime, endTime)
|
||||||
return c, errC
|
return c, errC
|
||||||
|
|
|
@ -26,15 +26,15 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// https://bybit-exchange.github.io/docs/zh-TW/v5/rate-limit
|
// https://bybit-exchange.github.io/docs/zh-TW/v5/rate-limit
|
||||||
// sharedRateLimiter indicates that the API belongs to the public API.
|
// GET/POST method (shared): 120 requests per second for 5 consecutive seconds
|
||||||
//
|
|
||||||
// The default order limiter apply 5 requests per second and a 5 initial bucket
|
|
||||||
// this includes QueryMarkets, QueryTicker, QueryAccountBalances, GetFeeRates
|
|
||||||
var (
|
var (
|
||||||
sharedRateLimiter = rate.NewLimiter(rate.Every(time.Second/5), 5)
|
// sharedRateLimiter indicates that the API belongs to the public API.
|
||||||
tradeRateLimiter = rate.NewLimiter(rate.Every(time.Second/5), 5)
|
// The default order limiter apply 5 requests per second and a 5 initial bucket
|
||||||
orderRateLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 10)
|
// this includes QueryMarkets, QueryTicker, QueryAccountBalances, GetFeeRates
|
||||||
closedOrderQueryLimiter = rate.NewLimiter(rate.Every(time.Second), 1)
|
sharedRateLimiter = rate.NewLimiter(rate.Every(time.Second/5), 5)
|
||||||
|
queryOrderTradeRateLimiter = rate.NewLimiter(rate.Every(time.Second/5), 5)
|
||||||
|
orderRateLimiter = rate.NewLimiter(rate.Every(time.Second/10), 10)
|
||||||
|
closedOrderQueryLimiter = rate.NewLimiter(rate.Every(time.Second), 1)
|
||||||
|
|
||||||
log = logrus.WithFields(logrus.Fields{
|
log = logrus.WithFields(logrus.Fields{
|
||||||
"exchange": "bybit",
|
"exchange": "bybit",
|
||||||
|
@ -159,7 +159,7 @@ func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders [
|
||||||
req = req.Cursor(cursor)
|
req = req.Cursor(cursor)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = tradeRateLimiter.Wait(ctx); err != nil {
|
if err = queryOrderTradeRateLimiter.Wait(ctx); err != nil {
|
||||||
return nil, fmt.Errorf("place order rate limiter wait error: %w", err)
|
return nil, fmt.Errorf("place order rate limiter wait error: %w", err)
|
||||||
}
|
}
|
||||||
res, err := req.Do(ctx)
|
res, err := req.Do(ctx)
|
||||||
|
@ -232,7 +232,7 @@ func (e *Exchange) QueryOrderTrades(ctx context.Context, q types.OrderQuery) (tr
|
||||||
req.Symbol(q.Symbol)
|
req.Symbol(q.Symbol)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tradeRateLimiter.Wait(ctx); err != nil {
|
if err := queryOrderTradeRateLimiter.Wait(ctx); err != nil {
|
||||||
return nil, fmt.Errorf("trade rate limiter wait error: %w", err)
|
return nil, fmt.Errorf("trade rate limiter wait error: %w", err)
|
||||||
}
|
}
|
||||||
response, err := req.Do(ctx)
|
response, err := req.Do(ctx)
|
||||||
|
@ -463,7 +463,7 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
|
||||||
}
|
}
|
||||||
req.Limit(limit)
|
req.Limit(limit)
|
||||||
|
|
||||||
if err := tradeRateLimiter.Wait(ctx); err != nil {
|
if err := queryOrderTradeRateLimiter.Wait(ctx); err != nil {
|
||||||
return nil, fmt.Errorf("trade rate limiter wait error: %w", err)
|
return nil, fmt.Errorf("trade rate limiter wait error: %w", err)
|
||||||
}
|
}
|
||||||
response, err := req.Do(ctx)
|
response, err := req.Do(ctx)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user