Merge pull request #1285 from bailantaotao/edwin/query-order

FEATURE: [bybit] implement ExchangeOrderQueryService interface
This commit is contained in:
bailantaotao 2023-08-16 22:07:36 +08:00 committed by GitHub
commit b6c9f14dcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 3 deletions

View File

@ -28,6 +28,11 @@ func (g *GetOrderHistoriesRequest) OrderId(orderId string) *GetOrderHistoriesReq
return g
}
func (g *GetOrderHistoriesRequest) OrderLinkId(orderLinkId string) *GetOrderHistoriesRequest {
g.orderLinkId = &orderLinkId
return g
}
func (g *GetOrderHistoriesRequest) OrderFilter(orderFilter string) *GetOrderHistoriesRequest {
g.orderFilter = &orderFilter
return g
@ -93,6 +98,14 @@ func (g *GetOrderHistoriesRequest) GetQueryParameters() (url.Values, error) {
params["orderId"] = orderId
} else {
}
// check orderLinkId field -> json key orderLinkId
if g.orderLinkId != nil {
orderLinkId := *g.orderLinkId
// assign parameter of orderLinkId
params["orderLinkId"] = orderLinkId
} else {
}
// check orderFilter field -> json key orderFilter
if g.orderFilter != nil {
orderFilter := *g.orderFilter

View File

@ -9,14 +9,15 @@ import (
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Result
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Result
//go:generate GetRequest -url "/v5/order/history" -type GetOrderHistoriesRequest -responseDataType .OpenOrdersResponse
//go:generate GetRequest -url "/v5/order/history" -type GetOrderHistoriesRequest -responseDataType .OrdersResponse
type GetOrderHistoriesRequest struct {
client requestgen.AuthenticatedAPIClient
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"`
// orderFilter supports 3 types of Order:
// 1. active order, 2. StopOrder: conditional order, 3. tpslOrder: spot TP/SL order
// Normal spot: return Order active order by default

View File

@ -2,6 +2,7 @@ package bybit
import (
"context"
"errors"
"fmt"
"strconv"
"time"
@ -45,6 +46,7 @@ var (
_ types.ExchangeMinimal = &Exchange{}
_ types.ExchangeTradeService = &Exchange{}
_ types.Exchange = &Exchange{}
_ types.ExchangeOrderQueryService = &Exchange{}
)
type Exchange struct {
@ -183,6 +185,78 @@ func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders [
return orders, nil
}
func (e *Exchange) QueryOrder(ctx context.Context, q types.OrderQuery) (*types.Order, error) {
if len(q.OrderID) == 0 && len(q.ClientOrderID) == 0 {
return nil, errors.New("one of OrderID/ClientOrderID is required parameter")
}
if len(q.OrderID) != 0 && len(q.ClientOrderID) != 0 {
return nil, errors.New("only accept one parameter of OrderID/ClientOrderID")
}
req := e.client.NewGetOrderHistoriesRequest()
if len(q.Symbol) != 0 {
req.Symbol(q.Symbol)
}
if len(q.OrderID) != 0 {
req.OrderId(q.OrderID)
}
if len(q.ClientOrderID) != 0 {
req.OrderLinkId(q.ClientOrderID)
}
res, err := req.Do(ctx)
if err != nil {
return nil, fmt.Errorf("failed to query order, queryConfig: %+v, err: %w", q, err)
}
if len(res.List) != 1 {
return nil, fmt.Errorf("unexpected order length, queryConfig: %+v", q)
}
return toGlobalOrder(res.List[0])
}
func (e *Exchange) QueryOrderTrades(ctx context.Context, q types.OrderQuery) (trades []types.Trade, err error) {
if len(q.ClientOrderID) != 0 {
log.Warn("!!!BYBIT EXCHANGE API NOTICE!!! Bybit does not support searching for trades using OrderClientId.")
}
if len(q.OrderID) == 0 {
return nil, errors.New("orderID is required parameter")
}
req := e.v3client.NewGetTradesRequest().OrderId(q.OrderID)
if len(q.Symbol) != 0 {
req.Symbol(q.Symbol)
}
if err := tradeRateLimiter.Wait(ctx); err != nil {
return nil, fmt.Errorf("trade rate limiter wait error: %w", err)
}
response, err := req.Do(ctx)
if err != nil {
return nil, fmt.Errorf("failed to query order trades, err: %w", err)
}
var errs error
for _, trade := range response.List {
res, err := v3ToGlobalTrade(trade)
if err != nil {
errs = multierr.Append(errs, err)
continue
}
trades = append(trades, *res)
}
if errs != nil {
return nil, errs
}
return trades, nil
}
func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (*types.Order, error) {
if len(order.Market.Symbol) == 0 {
return nil, fmt.Errorf("order.Market.Symbol is required: %+v", order)