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: [bybit] use fee currency of trade #1772

Merged
merged 1 commit into from
Oct 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pkg/exchange/bybit/bybitapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ func TestClient(t *testing.T) {
}
})

t.Run("GetTrade", func(t *testing.T) {
cursor := ""
for {
req := client.NewGetExecutionListRequest().Limit(50)
if len(cursor) != 0 {
req = req.Cursor(cursor)
}
trades, err := req.Do(ctx)
assert.NoError(t, err)

for _, o := range trades.List {
t.Logf("openOrders: %+v", o)
}
if len(trades.NextPageCursor) == 0 {
break
}
cursor = trades.NextPageCursor
}
})

t.Run("PlaceOrderRequest", func(t *testing.T) {
req := client.NewPlaceOrderRequest().
Symbol("DOTUSDT").
Expand Down
19 changes: 11 additions & 8 deletions pkg/exchange/bybit/bybitapi/get_execution_list_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ type Trade struct {
Side Side `json:"side"`
OrderType OrderType `json:"orderType"`
// ExecFee is supported on restful API v5, but not on websocket API.
ExecFee fixedpoint.Value `json:"execFee"`
ExecId string `json:"execId"`
ExecPrice fixedpoint.Value `json:"execPrice"`
ExecQty fixedpoint.Value `json:"execQty"`
ExecTime types.MillisecondTimestamp `json:"execTime"`
IsMaker bool `json:"isMaker"`
ExecFee fixedpoint.Value `json:"execFee"`
ExecId string `json:"execId"`
ExecPrice fixedpoint.Value `json:"execPrice"`
ExecQty fixedpoint.Value `json:"execQty"`
ExecTime types.MillisecondTimestamp `json:"execTime"`
IsMaker bool `json:"isMaker"`
FeeRate fixedpoint.Value `json:"feeRate"`
FeeCurrency string `json:"feeCurrency"`
}

//go:generate GetRequest -url "/v5/execution/list" -type GetExecutionListRequest -responseDataType .TradesResponse -rateLimiter 5+15/1s
Expand All @@ -38,8 +40,9 @@ type GetExecutionListRequest struct {

category Category `param:"category,query" validValues:"spot"`

symbol *string `param:"symbol,query"`
orderId *string `param:"orderId,query"`
symbol *string `param:"symbol,query"`
orderId *string `param:"orderId,query"`
orderLinkId *string `param:"orderLinkId,query"`

// startTime the start timestamp (ms)
// startTime and endTime are not passed, return 7 days by default;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions pkg/exchange/bybit/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func v3ToGlobalTrade(trade v3.Trade) (*types.Trade, error) {
}, nil
}

func toGlobalTrade(trade bybitapi.Trade, feeDetail SymbolFeeDetail) (*types.Trade, error) {
func toGlobalTrade(trade bybitapi.Trade) (*types.Trade, error) {
side, err := toGlobalSideType(trade.Side)
if err != nil {
return nil, fmt.Errorf("unexpected side: %s, err: %w", trade.Side, err)
Expand All @@ -339,8 +339,6 @@ func toGlobalTrade(trade bybitapi.Trade, feeDetail SymbolFeeDetail) (*types.Trad
return nil, fmt.Errorf("unexpected trade id: %s, err: %w", trade.ExecId, err)
}

fc, _ := calculateFee(trade, feeDetail)

return &types.Trade{
ID: tradeIdNum,
OrderID: orderIdNum,
Expand All @@ -354,7 +352,7 @@ func toGlobalTrade(trade bybitapi.Trade, feeDetail SymbolFeeDetail) (*types.Trad
IsMaker: trade.IsMaker,
Time: types.Time(trade.ExecTime),
Fee: trade.ExecFee,
FeeCurrency: fc,
FeeCurrency: trade.FeeCurrency,
IsMargin: false,
IsFutures: false,
IsIsolated: false,
Expand Down
16 changes: 7 additions & 9 deletions pkg/exchange/bybit/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,17 @@ func (e *Exchange) QueryOrder(ctx context.Context, q types.OrderQuery) (*types.O
return toGlobalOrder(res.List[0])
}

// QueryOrderTrades You can query by symbol, baseCoin, orderId and orderLinkId, and if you pass multiple params,
// the system will process them according to this priority: orderId > orderLinkId > symbol > baseCoin.
func (e *Exchange) QueryOrderTrades(ctx context.Context, q types.OrderQuery) (trades []types.Trade, err error) {
req := e.client.NewGetExecutionListRequest()
if len(q.ClientOrderID) != 0 {
log.Warn("!!!BYBIT EXCHANGE API NOTICE!!! Bybit does not support searching for trades using OrderClientId.")
req.OrderLinkId(q.ClientOrderID)
}

if len(q.OrderID) == 0 {
return nil, errors.New("orderID is required parameter")
if len(q.OrderID) != 0 {
req.OrderLinkId(q.OrderID)
}
req := e.client.NewGetExecutionListRequest().OrderId(q.OrderID)

if len(q.Symbol) != 0 {
req.Symbol(q.Symbol)
Expand Down Expand Up @@ -437,11 +439,7 @@ func (e *Exchange) queryTrades(ctx context.Context, req *bybitapi.GetExecutionLi
}

for _, trade := range res.List {
feeRate, err := pollAndGetFeeRate(ctx, trade.Symbol, e.FeeRatePoller, e.marketsInfo)
if err != nil {
return nil, fmt.Errorf("failed to get fee rate, err: %v", err)
}
trade, err := toGlobalTrade(trade, feeRate)
trade, err := toGlobalTrade(trade)
if err != nil {
return nil, fmt.Errorf("failed to convert trade, err: %v", err)
}
Expand Down
Loading