pkg/exchange: add QueryOrder api

This commit is contained in:
Edwin 2023-08-15 11:43:43 +08:00
parent bc9f7b7a1e
commit 3207a8227c
4 changed files with 51 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"
@ -183,6 +184,39 @@ 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) 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)