Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REFACTOR: improve batch query #1371

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions pkg/exchange/batch/closedorders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
12 changes: 0 additions & 12 deletions pkg/exchange/batch/option.go

This file was deleted.

14 changes: 8 additions & 6 deletions pkg/exchange/batch/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions pkg/exchange/bybit/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var (
_ types.ExchangeTradeService = &Exchange{}
_ types.Exchange = &Exchange{}
_ types.ExchangeOrderQueryService = &Exchange{}
_ types.ExchangeTimeRangeProvider = &Exchange{}
)

type Exchange struct {
Expand Down Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions pkg/types/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading