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

FEATURE: [okx] query recent trades #1583

Merged
merged 2 commits into from Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 41 additions & 20 deletions pkg/exchange/okex/exchange.go
Expand Up @@ -55,6 +55,7 @@ const (
defaultQueryLimit = 100

maxHistoricalDataQueryPeriod = 90 * 24 * time.Hour
threeDaysHistoricalPeriod = 3 * 24 * time.Hour
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use maxTradeHistoryPeriod ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have one that supports 90 days, called maxHistoricalDataQueryPeriod.

And now we have another one that's only 3 days, so it's called threeDaysHistoricalPeriod XD.

)

var log = logrus.WithFields(logrus.Fields{
Expand All @@ -66,7 +67,8 @@ var ErrSymbolRequired = errors.New("symbol is a required parameter")
type Exchange struct {
key, secret, passphrase string

client *okexapi.RestClient
client *okexapi.RestClient
timeNowFunc func() time.Time
}

func New(key, secret, passphrase string) *Exchange {
Expand All @@ -77,10 +79,11 @@ func New(key, secret, passphrase string) *Exchange {
}

return &Exchange{
key: key,
secret: secret,
passphrase: passphrase,
client: client,
key: key,
secret: secret,
passphrase: passphrase,
client: client,
timeNowFunc: time.Now,
}
}

Expand Down Expand Up @@ -564,33 +567,31 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
return nil, ErrSymbolRequired
}

req := e.client.NewGetTransactionHistoryRequest().InstrumentID(toLocalSymbol(symbol))

limit := options.Limit
if limit > defaultQueryLimit || limit <= 0 {
log.Infof("limit is exceeded default limit %d or zero, got: %d, use default limit", defaultQueryLimit, limit)
limit = defaultQueryLimit
}
req.Limit(uint64(limit))

var newStartTime time.Time
timeNow := e.timeNowFunc()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be just now ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, by using that timeNowFunc(), we can test the function.

newStartTime := timeNow.Add(-threeDaysHistoricalPeriod)
if options.StartTime != nil {
newStartTime = *options.StartTime
if time.Since(newStartTime) > maxHistoricalDataQueryPeriod {
newStartTime = time.Now().Add(-maxHistoricalDataQueryPeriod)
if timeNow.Sub(newStartTime) > maxHistoricalDataQueryPeriod {
newStartTime = timeNow.Add(-maxHistoricalDataQueryPeriod)
log.Warnf("!!!OKX EXCHANGE API NOTICE!!! The trade API cannot query data beyond 90 days from the current date, update %s -> %s", *options.StartTime, newStartTime)
}
req.StartTime(newStartTime.UTC())
}

endTime := timeNow
if options.EndTime != nil {
if options.EndTime.Before(newStartTime) {
return nil, fmt.Errorf("end time %s before start %s", *options.EndTime, newStartTime)
}
if options.EndTime.Sub(newStartTime) > maxHistoricalDataQueryPeriod {
return nil, fmt.Errorf("start time %s and end time %s cannot greater than 90 days", newStartTime, options.EndTime)
}
req.EndTime(options.EndTime.UTC())
endTime = *options.EndTime
}

if options.LastTradeID != 0 {
Expand All @@ -599,12 +600,33 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
log.Infof("Last trade id not supported on QueryTrades")
}

for {
if err := queryTradeLimiter.Wait(ctx); err != nil {
return nil, fmt.Errorf("query trades rate limiter wait error: %w", err)
}
if timeNow.Sub(newStartTime) <= threeDaysHistoricalPeriod {
c := e.client.NewGetThreeDaysTransactionHistoryRequest().
InstrumentID(toLocalSymbol(symbol)).
StartTime(newStartTime).
EndTime(endTime).
Limit(uint64(limit))
return getTrades(ctx, limit, func(ctx context.Context, billId string) ([]okexapi.Trade, error) {
c.Before(billId)
return c.Do(ctx)
})
}

response, err := req.Do(ctx)
c := e.client.NewGetTransactionHistoryRequest().
InstrumentID(toLocalSymbol(symbol)).
StartTime(newStartTime).
EndTime(endTime).
Limit(uint64(limit))
return getTrades(ctx, limit, func(ctx context.Context, billId string) ([]okexapi.Trade, error) {
c.Before(billId)
return c.Do(ctx)
})
}

func getTrades(ctx context.Context, limit int64, doFunc func(ctx context.Context, billId string) ([]okexapi.Trade, error)) (trades []types.Trade, err error) {
billId := "0"
for {
response, err := doFunc(ctx, billId)
if err != nil {
return nil, fmt.Errorf("failed to query trades, err: %w", err)
}
Expand All @@ -623,9 +645,8 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
break
}
// use Before filter to get all data.
req.Before(response[tradeLen-1].BillId.String())
billId = response[tradeLen-1].BillId.String()
}

return trades, nil
}

Expand Down