diff --git a/pkg/exchange/batch/closedorders.go b/pkg/exchange/batch/closedorders.go index 77e37690b6..c642833a35 100644 --- a/pkg/exchange/batch/closedorders.go +++ b/pkg/exchange/batch/closedorders.go @@ -12,7 +12,13 @@ type ClosedOrderBatchQuery struct { types.ExchangeTradeHistoryService } -func (q *ClosedOrderBatchQuery) Query(ctx context.Context, symbol string, startTime, endTime time.Time, lastOrderID uint64, opts ...Option) (c chan types.Order, errC chan error) { +func (q *ClosedOrderBatchQuery) Query(ctx context.Context, symbol string, startTime, endTime time.Time, lastOrderID uint64) (c chan types.Order, errC chan error) { + jump := 30 * 24 * time.Hour + timeRangeProvider, ok := q.ExchangeTradeHistoryService.(types.ExchangeTimeRangeProvider) + if ok { + jump = timeRangeProvider.GetMaxOrderHistoryTimeRange() + } + query := &AsyncTimeRangedBatchQuery{ Type: types.Order{}, Q: func(startTime, endTime time.Time) (interface{}, error) { @@ -29,11 +35,7 @@ func (q *ClosedOrderBatchQuery) Query(ctx context.Context, symbol string, startT } return strconv.FormatUint(order.OrderID, 10) }, - JumpIfEmpty: 30 * 24 * time.Hour, - } - - for _, opt := range opts { - opt(query) + JumpIfEmpty: jump, } c = make(chan types.Order, 100) diff --git a/pkg/exchange/batch/option.go b/pkg/exchange/batch/option.go deleted file mode 100644 index 67f18e6087..0000000000 --- a/pkg/exchange/batch/option.go +++ /dev/null @@ -1,12 +0,0 @@ -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 - } -} diff --git a/pkg/exchange/batch/trade.go b/pkg/exchange/batch/trade.go index 4fce26b651..e406da7c21 100644 --- a/pkg/exchange/batch/trade.go +++ b/pkg/exchange/batch/trade.go @@ -17,12 +17,18 @@ type TradeBatchQuery struct { types.ExchangeTradeHistoryService } -func (e TradeBatchQuery) Query(ctx context.Context, symbol string, options *types.TradeQueryOptions, opts ...Option) (c chan types.Trade, errC chan error) { +func (e TradeBatchQuery) Query(ctx context.Context, symbol string, options *types.TradeQueryOptions) (c chan types.Trade, errC chan error) { if options.EndTime == nil { now := time.Now() options.EndTime = &now } + jump := 3 * 24 * time.Hour + timeRangeProvider, ok := e.ExchangeTradeHistoryService.(types.ExchangeTimeRangeProvider) + if ok { + jump = timeRangeProvider.GetMaxTradeHistoryTimeRange() + } + startTime := *options.StartTime endTime := *options.EndTime query := &AsyncTimeRangedBatchQuery{ @@ -42,11 +48,7 @@ func (e TradeBatchQuery) Query(ctx context.Context, symbol string, options *type } return trade.Key().String() }, - JumpIfEmpty: 24 * time.Hour, - } - - for _, opt := range opts { - opt(query) + JumpIfEmpty: jump, } c = make(chan types.Trade, 100) diff --git a/pkg/exchange/bybit/exchange.go b/pkg/exchange/bybit/exchange.go index 7cec992c68..6740563044 100644 --- a/pkg/exchange/bybit/exchange.go +++ b/pkg/exchange/bybit/exchange.go @@ -47,6 +47,7 @@ var ( _ types.ExchangeTradeService = &Exchange{} _ types.Exchange = &Exchange{} _ types.ExchangeOrderQueryService = &Exchange{} + _ types.ExchangeTimeRangeProvider = &Exchange{} ) type Exchange struct { @@ -592,6 +593,20 @@ func (e *Exchange) GetAllFeeRates(ctx context.Context) (bybitapi.FeeRates, error return *feeRates, nil } +// GetMaxTradeHistoryTimeRange returns a time range of 180 days, which is the maximum supported by the exchange. +// +// see more: QueryTrades +func (e *Exchange) GetMaxTradeHistoryTimeRange() time.Duration { + return 180 * 24 * time.Hour +} + +// GetMaxOrderHistoryTimeRange returns 0 since the exchange not supported by time-range-based query +// +// see more: QueryClosedOrders +func (e *Exchange) GetMaxOrderHistoryTimeRange() time.Duration { + return 0 +} + func (e *Exchange) NewStream() types.Stream { return NewStream(e.key, e.secret, e) } diff --git a/pkg/types/exchange.go b/pkg/types/exchange.go index 70e3c13798..da3fe3d568 100644 --- a/pkg/types/exchange.go +++ b/pkg/types/exchange.go @@ -122,6 +122,11 @@ type ExchangeTradeHistoryService interface { QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []Order, err error) } +type ExchangeTimeRangeProvider interface { + GetMaxTradeHistoryTimeRange() time.Duration + GetMaxOrderHistoryTimeRange() time.Duration +} + type ExchangeMarketDataService interface { NewStream() Stream