From d9474bb6e3ee2d2383fac3c1c41d7c1c7165222a Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 26 Oct 2023 18:58:20 +0800 Subject: [PATCH 1/2] pkg/types, pkg/exchange: add ExchangeTimeRangeProvider and use it as jumpIfEmpty param --- pkg/exchange/batch/closedorders.go | 14 ++++++++------ pkg/exchange/batch/option.go | 12 ------------ pkg/exchange/batch/trade.go | 14 ++++++++------ pkg/types/exchange.go | 5 +++++ 4 files changed, 21 insertions(+), 24 deletions(-) delete mode 100644 pkg/exchange/batch/option.go 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/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 From 1448c05f0c5d0439d8e9d684c7d931b57a5f57ff Mon Sep 17 00:00:00 2001 From: Edwin Date: Thu, 26 Oct 2023 18:58:40 +0800 Subject: [PATCH 2/2] pkg/exchange: support TimeRangeProvider in bybit --- pkg/exchange/bybit/exchange.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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) }