diff --git a/rest/api_order.go b/rest/api_order.go index 1b863d2..8822229 100644 --- a/rest/api_order.go +++ b/rest/api_order.go @@ -221,8 +221,7 @@ func (b *ByBit) getOrders(orderID string, orderLinkID string, sort string, order // stopOrderStatus 条件单状态: Untriggered: 等待市价触发条件单; Triggered: 市价已触发条件单; Cancelled: 取消; Active: 条件单触发成功且下单成功; Rejected: 条件触发成功但下单失败 // limit: 一页数量,默认一页展示20条数据;最大支持50条每页 func (b *ByBit) GetStopOrders(orderID string, orderLinkID string, stopOrderStatus string, order string, - page int, limit int, symbol string) (result []Order, err error) { - var cResult OrderListResult + page int, limit int, symbol string) (result GetStopOrdersResult, err error) { if limit == 0 { limit = 20 @@ -245,16 +244,15 @@ func (b *ByBit) GetStopOrders(orderID string, orderLinkID string, stopOrderStatu params["page"] = page params["limit"] = limit var resp []byte - resp, err = b.SignedRequest(http.MethodGet, "open-api/stop-order/list", params, &cResult) + resp, err = b.SignedRequest(http.MethodGet, "open-api/stop-order/list", params, &result) if err != nil { return } - if cResult.RetCode != 0 { - err = fmt.Errorf("%v body: [%v]", cResult.RetMsg, string(resp)) + if result.RetCode != 0 { + err = fmt.Errorf("%v body: [%v]", result.RetMsg, string(resp)) return } - result = cResult.Result.Data return } @@ -388,3 +386,23 @@ func (b *ByBit) CancelStopOrder(orderID string, symbol string) (result Order, er result = cResult.Result return } + +// CancelAllStopOrders 撤消全部条件委托单 +// symbol: +func (b *ByBit) CancelAllStopOrders(symbol string) (result []StopOrderV2, err error) { + var cResult CancelStopOrdersV2Result + params := map[string]interface{}{} + params["symbol"] = symbol + var resp []byte + resp, err = b.SignedRequest(http.MethodPost, "v2/private/stop-order/cancelAll", params, &cResult) + if err != nil { + return + } + if cResult.RetCode != 0 { + err = fmt.Errorf("%v body: [%v]", cResult.RetMsg, string(resp)) + return + } + + result = cResult.Result + return +} diff --git a/rest/api_test.go b/rest/api_test.go index 6b369f7..fd344a9 100644 --- a/rest/api_test.go +++ b/rest/api_test.go @@ -243,16 +243,31 @@ func TestByBit_GetOrder(t *testing.T) { func TestByBit_GetStopOrders(t *testing.T) { b := newByBit() symbol := "BTCUSD" - orders, err := b.GetStopOrders("", "", "", "", 0, 10, symbol) + // Untriggered: 等待市价触发条件单; Triggered: 市价已触发条件单; Cancelled: 取消; Active: 条件单触发成功且下单成功; Rejected: 条件触发成功但下单失败 + status := "Untriggered,Triggered,Active" + result, err := b.GetStopOrders("", "", status, "", 0, 10, symbol) assert.Nil(t, err) //t.Logf("%#v", orders) - for _, order := range orders { - if order.ExtFields != nil { - t.Logf("%#v %v", order, *order.ExtFields) - } else { - t.Logf("%#v", order) - } + for _, order := range result.Result.Data { + //if order.ExtFields != nil { + // t.Logf("%#v %v", order, *order.ExtFields) + //} else { + t.Logf("CreatedAt: %v %#v", order.CreatedAt.Local(), order) + //} + } +} + +func TestByBit_GetStopOrders2(t *testing.T) { + b := newByBit() + symbol := "BTCUSD" + //stopOrderID := "8a84cd9b-a3d4-4354-b2d7-e3b805369b77" + stopOrderID := "ccdcbdae-2eb8-4b8f-92de-32cc5ee18de4" + order, err := b.GetOrderByID(stopOrderID, "", symbol) + if err != nil { + t.Error(err) + return } + t.Logf("%#v", order) } func TestByBit_CancelOrder(t *testing.T) { @@ -285,6 +300,14 @@ func TestByBit_CancelStopOrder(t *testing.T) { t.Logf("%#v", order) } +func TestByBit_CancelAllStopOrders(t *testing.T) { + b := newByBit() + symbol := "BTCUSD" + orders, err := b.CancelAllStopOrders(symbol) + assert.Nil(t, err) + t.Logf("%#v", orders) +} + func TestByBit_GetLeverages(t *testing.T) { b := newByBit() l, err := b.GetLeverages() diff --git a/rest/result.go b/rest/result.go index 031736a..b6a9062 100644 --- a/rest/result.go +++ b/rest/result.go @@ -239,29 +239,30 @@ type OrderListResult struct { // Order ... type Order struct { - OrderID string `json:"order_id"` - StopOrderID string `json:"stop_order_id"` - UserID int `json:"user_id"` - Symbol string `json:"symbol"` - Side string `json:"side"` - OrderType string `json:"order_type"` - Price float64 `json:"price"` - Qty float64 `json:"qty"` - TimeInForce string `json:"time_in_force"` - StopOrderType string `json:"stop_order_type,omitempty"` - StopPx sjson.Number `json:"stop_px,omitempty"` - OrderStatus string `json:"order_status"` - LastExecTime string `json:"last_exec_time"` - LastExecPrice float64 `json:"last_exec_price"` - LeavesQty float64 `json:"leaves_qty"` - CumExecQty float64 `json:"cum_exec_qty"` - CumExecValue float64 `json:"cum_exec_value"` - CumExecFee float64 `json:"cum_exec_fee"` - RejectReason string `json:"reject_reason"` - OrderLinkID string `json:"order_link_id"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` - ExtFields *ExtFields `json:"ext_fields,omitempty"` + OrderID string `json:"order_id"` + //StopOrderID string `json:"stop_order_id"` + UserID int `json:"user_id"` + Symbol string `json:"symbol"` + Side string `json:"side"` + OrderType string `json:"order_type"` + Price float64 `json:"price"` + Qty float64 `json:"qty"` + TimeInForce string `json:"time_in_force"` + //StopOrderType string `json:"stop_order_type,omitempty"` + //StopPx sjson.Number `json:"stop_px,omitempty"` + OrderStatus string `json:"order_status"` + //StopOrderStatus string `json:"stop_order_status"` + LastExecTime string `json:"last_exec_time"` + LastExecPrice float64 `json:"last_exec_price"` + LeavesQty float64 `json:"leaves_qty"` + CumExecQty float64 `json:"cum_exec_qty"` + CumExecValue float64 `json:"cum_exec_value"` + CumExecFee float64 `json:"cum_exec_fee"` + RejectReason string `json:"reject_reason"` + OrderLinkID string `json:"order_link_id"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + ExtFields *ExtFields `json:"ext_fields,omitempty"` } type ExtFields struct { @@ -416,8 +417,8 @@ type OrderV2 struct { OrderStatus string `json:"order_status"` LastExecTime sjson.Number `json:"last_exec_time"` LastExecPrice sjson.Number `json:"last_exec_price"` - LeavesQty int `json:"leaves_qty"` - CumExecQty int `json:"cum_exec_qty"` + LeavesQty float64 `json:"leaves_qty"` + CumExecQty float64 `json:"cum_exec_qty"` CumExecValue sjson.Number `json:"cum_exec_value"` CumExecFee sjson.Number `json:"cum_exec_fee"` RejectReason string `json:"reject_reason"` @@ -473,3 +474,76 @@ type QueryOrderResult struct { RateLimitResetMs int64 `json:"rate_limit_reset_ms"` RateLimit int `json:"rate_limit"` } + +type StopOrderV2 struct { + ClOrdID string `json:"clOrdID"` + UserID int64 `json:"user_id"` + Symbol string `json:"symbol"` + Side string `json:"side"` + OrderType string `json:"order_type"` + Price sjson.Number `json:"price"` + Qty float64 `json:"qty"` + TimeInForce string `json:"time_in_force"` + CreateType string `json:"create_type"` + CancelType string `json:"cancel_type"` + OrderStatus string `json:"order_status"` + LeavesQty float64 `json:"leaves_qty"` + LeavesValue string `json:"leaves_value"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + CrossStatus string `json:"cross_status"` + CrossSeq float64 `json:"cross_seq"` + StopOrderType string `json:"stop_order_type"` + TriggerBy string `json:"trigger_by"` + BasePrice sjson.Number `json:"base_price"` + ExpectedDirection string `json:"expected_direction"` +} + +type CancelStopOrdersV2Result struct { + RetCode int `json:"ret_code"` + RetMsg string `json:"ret_msg"` + ExtCode string `json:"ext_code"` + ExtInfo string `json:"ext_info"` + Result []StopOrderV2 `json:"result"` + TimeNow string `json:"time_now"` + RateLimitStatus int `json:"rate_limit_status"` + RateLimitResetMs int64 `json:"rate_limit_reset_ms"` + RateLimit int `json:"rate_limit"` +} + +type StopOrder struct { + UserID int64 `json:"user_id"` + StopOrderStatus string `json:"stop_order_status"` + Symbol string `json:"symbol"` + Side string `json:"side"` + OrderType string `json:"order_type"` + Price float64 `json:"price"` + Qty float64 `json:"qty"` + TimeInForce string `json:"time_in_force"` + StopOrderType string `json:"stop_order_type"` + TriggerBy string `json:"trigger_by"` + BasePrice float64 `json:"base_price"` + OrderLinkID string `json:"order_link_id"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + StopPx float64 `json:"stop_px"` + StopOrderID string `json:"stop_order_id"` +} + +type GetStopOrdersResultData struct { + CurrentPage int `json:"current_page"` + LastPage int `json:"last_page"` + Data []StopOrder `json:"data"` +} + +type GetStopOrdersResult struct { + RetCode int `json:"ret_code"` + RetMsg string `json:"ret_msg"` + ExtCode string `json:"ext_code"` + Result GetStopOrdersResultData `json:"result"` + ExtInfo interface{} `json:"ext_info"` + TimeNow string `json:"time_now"` + RateLimitStatus int `json:"rate_limit_status"` + RateLimitResetMs int64 `json:"rate_limit_reset_ms"` + RateLimit int `json:"rate_limit"` +}