Merge pull request #1530 from c9s/edwin/okx/fix-trade-id

FIX: [okx] fix trade id
This commit is contained in:
bailantaotao 2024-02-15 22:33:10 +08:00 committed by GitHub
commit 4bd5c62646
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 19 deletions

View File

@ -150,12 +150,9 @@ func toGlobalTrades(orderDetails []okexapi.OrderDetails) ([]types.Trade, error)
} }
func tradeToGlobal(trade okexapi.Trade) types.Trade { func tradeToGlobal(trade okexapi.Trade) types.Trade {
// ** We use the bill id as the trade id, because okx uses billId to perform pagination. **
billID := trade.BillId
side := toGlobalSide(trade.Side) side := toGlobalSide(trade.Side)
return types.Trade{ return types.Trade{
ID: uint64(billID), ID: uint64(trade.TradeId),
OrderID: uint64(trade.OrderId), OrderID: uint64(trade.OrderId),
Exchange: types.ExchangeOKEx, Exchange: types.ExchangeOKEx,
Price: trade.FillPrice, Price: trade.FillPrice,

View File

@ -115,7 +115,7 @@ func Test_tradeToGlobal(t *testing.T) {
t.Run("succeeds with sell/taker", func(t *testing.T) { t.Run("succeeds with sell/taker", func(t *testing.T) {
assert.Equal(tradeToGlobal(res), types.Trade{ assert.Equal(tradeToGlobal(res), types.Trade{
ID: uint64(665951654138736652), ID: uint64(724072849),
OrderID: uint64(665951654130348158), OrderID: uint64(665951654130348158),
Exchange: types.ExchangeOKEx, Exchange: types.ExchangeOKEx,
Price: fixedpoint.NewFromFloat(46446.4), Price: fixedpoint.NewFromFloat(46446.4),
@ -135,7 +135,7 @@ func Test_tradeToGlobal(t *testing.T) {
newRes := res newRes := res
newRes.Side = okexapi.SideTypeBuy newRes.Side = okexapi.SideTypeBuy
assert.Equal(tradeToGlobal(newRes), types.Trade{ assert.Equal(tradeToGlobal(newRes), types.Trade{
ID: uint64(665951654138736652), ID: uint64(724072849),
OrderID: uint64(665951654130348158), OrderID: uint64(665951654130348158),
Exchange: types.ExchangeOKEx, Exchange: types.ExchangeOKEx,
Price: fixedpoint.NewFromFloat(46446.4), Price: fixedpoint.NewFromFloat(46446.4),
@ -155,7 +155,7 @@ func Test_tradeToGlobal(t *testing.T) {
newRes := res newRes := res
newRes.ExecutionType = okexapi.LiquidityTypeMaker newRes.ExecutionType = okexapi.LiquidityTypeMaker
assert.Equal(tradeToGlobal(newRes), types.Trade{ assert.Equal(tradeToGlobal(newRes), types.Trade{
ID: uint64(665951654138736652), ID: uint64(724072849),
OrderID: uint64(665951654130348158), OrderID: uint64(665951654130348158),
Exchange: types.ExchangeOKEx, Exchange: types.ExchangeOKEx,
Price: fixedpoint.NewFromFloat(46446.4), Price: fixedpoint.NewFromFloat(46446.4),
@ -176,7 +176,7 @@ func Test_tradeToGlobal(t *testing.T) {
newRes.Side = okexapi.SideTypeBuy newRes.Side = okexapi.SideTypeBuy
newRes.ExecutionType = okexapi.LiquidityTypeMaker newRes.ExecutionType = okexapi.LiquidityTypeMaker
assert.Equal(tradeToGlobal(newRes), types.Trade{ assert.Equal(tradeToGlobal(newRes), types.Trade{
ID: uint64(665951654138736652), ID: uint64(724072849),
OrderID: uint64(665951654130348158), OrderID: uint64(665951654130348158),
Exchange: types.ExchangeOKEx, Exchange: types.ExchangeOKEx,
Price: fixedpoint.NewFromFloat(46446.4), Price: fixedpoint.NewFromFloat(46446.4),

View File

@ -535,6 +535,7 @@ REMARK: If your start time is 90 days earlier, we will update it to now - 90 day
** StartTime, EndTime, FromTradeId can be used together. ** ** StartTime, EndTime, FromTradeId can be used together. **
If you want to query all trades within a large time range (e.g. total orders > 100), we recommend using batch.TradeBatchQuery. If you want to query all trades within a large time range (e.g. total orders > 100), we recommend using batch.TradeBatchQuery.
We don't support the last trade id as a filter because okx supports bill ID only.
*/ */
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) { func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
if symbol == "" { if symbol == "" {
@ -544,11 +545,11 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
req := e.client.NewGetTransactionHistoryRequest().InstrumentID(toLocalSymbol(symbol)) req := e.client.NewGetTransactionHistoryRequest().InstrumentID(toLocalSymbol(symbol))
limit := options.Limit limit := options.Limit
req.Limit(uint64(limit))
if limit > defaultQueryLimit || limit <= 0 { if limit > defaultQueryLimit || limit <= 0 {
log.Infof("limit is exceeded default limit %d or zero, got: %d, use default limit", defaultQueryLimit, limit) log.Infof("limit is exceeded default limit %d or zero, got: %d, use default limit", defaultQueryLimit, limit)
req.Limit(defaultQueryLimit) limit = defaultQueryLimit
} }
req.Limit(uint64(limit))
var newStartTime time.Time var newStartTime time.Time
if options.StartTime != nil { if options.StartTime != nil {
@ -569,8 +570,14 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
} }
req.EndTime(options.EndTime.UTC()) req.EndTime(options.EndTime.UTC())
} }
req.Before(strconv.FormatUint(options.LastTradeID, 10))
if options.LastTradeID != 0 {
// we don't support the last trade id as a filter because okx supports bill ID only.
// we don't have any more fields (types.Trade) to store it.
log.Infof("Last trade id not supported on QueryTrades")
}
for {
if err := queryTradeLimiter.Wait(ctx); err != nil { if err := queryTradeLimiter.Wait(ctx); err != nil {
return nil, fmt.Errorf("query trades rate limiter wait error: %w", err) return nil, fmt.Errorf("query trades rate limiter wait error: %w", err)
} }
@ -584,6 +591,19 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
trades = append(trades, tradeToGlobal(trade)) trades = append(trades, tradeToGlobal(trade))
} }
tradeLen := int64(len(response))
// a defensive programming to ensure the length of order response is expected.
if tradeLen > limit {
return nil, fmt.Errorf("unexpected trade length %d", tradeLen)
}
if tradeLen < limit {
break
}
// use Before filter to get all data.
req.Before(response[tradeLen-1].BillId.String())
}
return trades, nil return trades, nil
} }