Skip to content

Commit

Permalink
Add order history function (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidphay authored Jun 29, 2023
1 parent 77e1b6b commit 765bddc
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 12 deletions.
40 changes: 40 additions & 0 deletions integrationtest/v5/order/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,46 @@ func TestGetOpenOrders(t *testing.T) {
}
}

func TestGetHistoryOrders(t *testing.T) {
client := bybit.NewTestClient().WithAuthFromEnv()
var orderID string
category := bybit.CategoryV5Spot
symbol := bybit.SymbolV5BTCUSDT
{
price := "10000.0"
res, err := client.V5().Order().CreateOrder(bybit.V5CreateOrderParam{
Category: category,
Symbol: symbol,
Side: bybit.SideBuy,
OrderType: bybit.OrderTypeLimit,
Qty: "0.01",
Price: &price,
})
require.NoError(t, err)
orderID = res.Result.OrderID
}

res, err := client.V5().Order().GetHistoryOrders(bybit.V5GetHistoryOrdersParam{
Category: category,
Symbol: &symbol,
})
require.NoError(t, err)
{
goldenFilename := "./testdata/v5-get-history-orders.json"
testhelper.Compare(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
}

{
_, err := client.V5().Order().CancelOrder(bybit.V5CancelOrderParam{
Category: category,
Symbol: symbol,
OrderID: &orderID,
})
require.NoError(t, err)
}
}

func TestCancelAllOrders(t *testing.T) {
client := bybit.NewTestClient().WithAuthFromEnv()
category := bybit.CategoryV5Spot
Expand Down
41 changes: 41 additions & 0 deletions integrationtest/v5/order/testdata/v5-get-history-orders.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"category": "spot",
"nextPageCursor": "1368933863545500672%3A1677925633316%2C1368933863545500672%3A1677925633316",
"list": [
{
"symbol": "BTCUSDT",
"orderType": "Limit",
"orderLinkId": "1677925633310702",
"orderId": "1368933863545500672",
"cancelType": "UNKNOWN",
"avgPrice": "0.00",
"stopOrderType": "",
"lastPriceOnCreated": "",
"orderStatus": "New",
"takeProfit": "",
"cumExecValue": "0.00000000",
"triggerDirection": 0,
"isLeverage": "0",
"rejectReason": "EC_NoError",
"price": "10000.00",
"orderIv": "",
"createdTime": "1677925633316",
"tpTriggerBy": "",
"positionIdx": 0,
"timeInForce": "GTC",
"leavesValue": "100.00000000",
"updatedTime": "1677925633323",
"side": "Buy",
"triggerPrice": "0.00",
"cumExecFee": "0",
"leavesQty": "0.010000",
"slTriggerBy": "",
"closeOnTrigger": false,
"cumExecQty": "0.000000",
"reduceOnly": false,
"qty": "0.010000",
"stopLoss": "",
"triggerBy": ""
}
]
}
61 changes: 49 additions & 12 deletions v5_order_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ type V5OrderServiceI interface {
CreateOrder(V5CreateOrderParam) (*V5CreateOrderResponse, error)
AmendOrder(V5AmendOrderParam) (*V5AmendOrderResponse, error)
CancelOrder(V5CancelOrderParam) (*V5CancelOrderResponse, error)
GetOpenOrders(V5GetOpenOrdersParam) (*V5GetOpenOrdersResponse, error)
GetOpenOrders(V5GetOpenOrdersParam) (*V5GetOrdersResponse, error)
CancelAllOrders(V5CancelAllOrdersParam) (*V5CancelAllOrdersResponse, error)
GetHistoryOrders(V5GetHistoryOrdersParam) (*V5GetOrdersResponse, error)
}

// V5OrderService :
Expand Down Expand Up @@ -193,20 +194,36 @@ type V5GetOpenOrdersParam struct {
Cursor *string `url:"cursor,omitempty"`
}

// V5GetOpenOrdersResponse :
type V5GetOpenOrdersResponse struct {
// V5GetGetHistoryOrderssParam :
type V5GetHistoryOrdersParam struct {
Category CategoryV5 `url:"category"`

Symbol *SymbolV5 `url:"symbol,omitempty"`
BaseCoin *Coin `url:"baseCoin,omitempty"`
OrderID *string `url:"orderId,omitempty"`
OrderLinkID *string `url:"orderLinkId,omitempty"`
OrderFilter *OrderFilter `url:"orderFilter,omitempty"` // If not passed, Order by default
OrderStatus *OrderStatus `url:"orderStatus,omitempty"`
StartTime *int `url:"startTime,omitempty"`
EndTime *int `url:"endTime,omitempty"`
Limit *int `url:"limit,omitempty"`
Cursor *string `url:"cursor,omitempty"`
}

// V5GetOrdersResponse :
type V5GetOrdersResponse struct {
CommonV5Response `json:",inline"`
Result V5GetOpenOrdersResult `json:"result"`
Result V5GetOrdersResult `json:"result"`
}

// V5GetOpenOrdersResult :
type V5GetOpenOrdersResult struct {
Category CategoryV5 `json:"category"`
NextPageCursor string `json:"nextPageCursor"`
List []V5GetOpenOrder `json:"list"`
// V5GetOrdersResult :
type V5GetOrdersResult struct {
Category CategoryV5 `json:"category"`
NextPageCursor string `json:"nextPageCursor"`
List []V5GetOrder `json:"list"`
}

type V5GetOpenOrder struct {
type V5GetOrder struct {
Symbol SymbolV5 `json:"symbol"`
OrderType OrderType `json:"orderType"`
OrderLinkID string `json:"orderLinkId"`
Expand Down Expand Up @@ -243,8 +260,8 @@ type V5GetOpenOrder struct {
}

// GetOpenOrders :
func (s *V5OrderService) GetOpenOrders(param V5GetOpenOrdersParam) (*V5GetOpenOrdersResponse, error) {
var res V5GetOpenOrdersResponse
func (s *V5OrderService) GetOpenOrders(param V5GetOpenOrdersParam) (*V5GetOrdersResponse, error) {
var res V5GetOrdersResponse

if param.Category == "" {
return nil, fmt.Errorf("Category needed")
Expand All @@ -262,6 +279,26 @@ func (s *V5OrderService) GetOpenOrders(param V5GetOpenOrdersParam) (*V5GetOpenOr
return &res, nil
}

// GetHistoryOrders :
func (s *V5OrderService) GetHistoryOrders(param V5GetHistoryOrdersParam) (*V5GetOrdersResponse, error) {
var res V5GetOrdersResponse

if param.Category == "" {
return nil, fmt.Errorf("Category needed")
}

queryString, err := query.Values(param)
if err != nil {
return nil, err
}

if err := s.client.getV5Privately("/v5/order/history", queryString, &res); err != nil {
return nil, err
}

return &res, nil
}

// V5CancelAllOrdersParam :
// If you pass multiple of these params, the system will process one of param, which priority is symbol > baseCoin > settleCoin.
type V5CancelAllOrdersParam struct {
Expand Down
143 changes: 143 additions & 0 deletions v5_order_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,149 @@ func TestV5Order_GetOpenOrders(t *testing.T) {
})
}

func TestV5Order_GetHistoryOrders(t *testing.T) {
t.Run("success", func(t *testing.T) {
symbol := SymbolV5BTCUSDT
param := V5GetHistoryOrdersParam{
Category: CategoryV5Linear,
Symbol: &symbol,
}

path := "/v5/order/history"
method := http.MethodGet
status := http.StatusOK
respBody := map[string]interface{}{
"result": map[string]interface{}{
"category": "linear",
"nextPageCursor": "",
"list": []map[string]interface{}{
{
"symbol": "ETHUSDT",
"orderType": "Limit",
"orderLinkId": "1672217748277652",
"orderId": "1321052653536515584",
"cancelType": "UNKNOWN",
"avgPrice": "",
"stopOrderType": "tpslOrder",
"lastPriceOnCreated": "",
"orderStatus": "Cancelled",
"takeProfit": "",
"cumExecValue": "0",
"triggerDirection": 0,
"isLeverage": "0",
"rejectReason": "",
"price": "1000",
"orderIv": "",
"createdTime": "1672217748287",
"tpTriggerBy": "",
"positionIdx": 0,
"timeInForce": "GTC",
"leavesValue": "500",
"updatedTime": "1672217748287",
"side": "Buy",
"triggerPrice": "1500",
"cumExecFee": "0",
"leavesQty": "0",
"slTriggerBy": "",
"closeOnTrigger": false,
"cumExecQty": "0",
"reduceOnly": false,
"qty": "0.5",
"stopLoss": "",
"triggerBy": "1192.5",
},
},
},
}

bytesBody, err := json.Marshal(respBody)
require.NoError(t, err)

server, teardown := testhelper.NewServer(
testhelper.WithHandlerOption(path, method, status, bytesBody),
)
defer teardown()

client := NewTestClient().
WithBaseURL(server.URL).
WithAuth("test", "test")

resp, err := client.V5().Order().GetHistoryOrders(param)
require.NoError(t, err)

require.NotNil(t, resp)
testhelper.Compare(t, respBody["result"], resp.Result)
})
t.Run("authentication required", func(t *testing.T) {
symbol := SymbolV5BTCUSDT
param := V5GetHistoryOrdersParam{
Category: CategoryV5Linear,
Symbol: &symbol,
}

path := "/v5/order/history"
method := http.MethodGet
status := http.StatusOK
respBody := map[string]interface{}{
"result": map[string]interface{}{
"category": "linear",
"nextPageCursor": "",
"list": []map[string]interface{}{
{
"symbol": "ETHUSDT",
"orderType": "Limit",
"orderLinkId": "1672217748277652",
"orderId": "1321052653536515584",
"cancelType": "UNKNOWN",
"avgPrice": "",
"stopOrderType": "tpslOrder",
"lastPriceOnCreated": "",
"orderStatus": "Cancelled",
"takeProfit": "",
"cumExecValue": "0",
"triggerDirection": 0,
"isLeverage": "0",
"rejectReason": "",
"price": "1000",
"orderIv": "",
"createdTime": "1672217748287",
"tpTriggerBy": "",
"positionIdx": 0,
"timeInForce": "GTC",
"leavesValue": "500",
"updatedTime": "1672217748287",
"side": "Buy",
"triggerPrice": "1500",
"cumExecFee": "0",
"leavesQty": "0",
"slTriggerBy": "",
"closeOnTrigger": false,
"cumExecQty": "0",
"reduceOnly": false,
"qty": "0.5",
"stopLoss": "",
"triggerBy": "1192.5",
},
},
},
}

bytesBody, err := json.Marshal(respBody)
require.NoError(t, err)

server, teardown := testhelper.NewServer(
testhelper.WithHandlerOption(path, method, status, bytesBody),
)
defer teardown()

client := NewTestClient().
WithBaseURL(server.URL)

_, err = client.V5().Order().GetHistoryOrders(param)
assert.Error(t, err)
})
}

func TestV5Order_CancelAllOrders(t *testing.T) {
t.Run("success", func(t *testing.T) {
t.Run("linear", func(t *testing.T) {
Expand Down

0 comments on commit 765bddc

Please sign in to comment.