kucoin: rewrite cancel all orders request

This commit is contained in:
c9s 2021-12-31 01:50:56 +08:00
parent 6addd503aa
commit e09b4fa5fb
3 changed files with 65 additions and 45 deletions

View File

@ -3,10 +3,11 @@ package main
import (
"context"
"github.com/c9s/bbgo/pkg/exchange/kucoin/kucoinapi"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/c9s/bbgo/pkg/exchange/kucoin/kucoinapi"
)
func init() {
@ -16,6 +17,7 @@ func init() {
cancelOrderCmd.Flags().String("client-order-id", "", "client order id")
cancelOrderCmd.Flags().String("order-id", "", "order id")
cancelOrderCmd.Flags().Bool("all", false, "cancel all")
ordersCmd.AddCommand(cancelOrderCmd)
placeOrderCmd.Flags().String("symbol", "", "symbol")
@ -145,8 +147,24 @@ var cancelOrderCmd = &cobra.Command{
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
req := client.TradeService.NewCancelOrderRequest()
cancelAll, err := cmd.Flags().GetBool("all")
if err != nil {
return err
}
if cancelAll {
req := client.TradeService.NewCancelAllOrderRequest()
req.TradeType("TRADE")
response, err := req.Do(context.Background())
if err != nil {
return err
}
logrus.Infof("cancel all order response: %+v", response)
return nil
}
req := client.TradeService.NewCancelOrderRequest()
orderID, err := cmd.Flags().GetString("order-id")
if err != nil {
return err

View File

@ -1,8 +1,9 @@
// Code generated by "requestgen -type CancelAllOrderRequest"; DO NOT EDIT.
// Code generated by "requestgen -method DELETE -responseType .APIResponse -responseDataField Data -url /api/v1/orders -type CancelAllOrderRequest -responseDataType .CancelOrderResponse"; DO NOT EDIT.
package kucoinapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
@ -107,3 +108,34 @@ func (r *CancelAllOrderRequest) GetSlugsMap() (map[string]string, error) {
return slugs, nil
}
func (r *CancelAllOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, error) {
params, err := r.GetParameters()
if err != nil {
return nil, err
}
query := url.Values{}
apiURL := "/api/v1/orders"
req, err := r.client.NewAuthenticatedRequest(ctx, "DELETE", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := r.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data CancelOrderResponse
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -2,6 +2,7 @@ package kucoinapi
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data
//go:generate -command DeleteRequest requestgen -method DELETE -responseType .APIResponse -responseDataField Data
import (
"context"
@ -255,13 +256,6 @@ type PlaceOrderRequest struct {
timeInForce *TimeInForceType `param:"timeInForce,required"`
}
//go:generate requestgen -type CancelOrderRequest
type CancelOrderRequest struct {
client *RestClient
orderID *string `param:"orderID"`
clientOrderID *string `param:"clientOrderID"`
}
type CancelOrderResponse struct {
CancelledOrderIDs []string `json:"cancelledOrderIds,omitempty"`
@ -271,6 +265,15 @@ type CancelOrderResponse struct {
ClientOrderID string `json:"clientOid,omitempty"`
}
//go:generate requestgen -type CancelOrderRequest
type CancelOrderRequest struct {
client requestgen.AuthenticatedAPIClient
orderID *string `param:"orderID"`
clientOrderID *string `param:"clientOrderID"`
}
func (r *CancelOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, error) {
if r.orderID == nil && r.clientOrderID == nil {
return nil, errors.New("either orderID or clientOrderID is required for canceling order")
@ -310,47 +313,14 @@ func (r *CancelOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, erro
return apiResponse.Data, nil
}
//go:generate requestgen -type CancelAllOrderRequest
//go:generate DeleteRequest -url /api/v1/orders -type CancelAllOrderRequest -responseDataType .CancelOrderResponse
type CancelAllOrderRequest struct {
client *RestClient
client requestgen.AuthenticatedAPIClient
symbol *string `param:"symbol"`
tradeType *string `param:"tradeType"`
}
func (r *CancelAllOrderRequest) Do(ctx context.Context) (*CancelOrderResponse, error) {
params, err := r.GetParametersQuery()
if err != nil {
return nil, err
}
req, err := r.client.NewAuthenticatedRequest(ctx, "DELETE", "/api/v1/orders", params, nil)
if err != nil {
return nil, err
}
response, err := r.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data *CancelOrderResponse `json:"data"`
}
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
if apiResponse.Data == nil {
return nil, errors.New("api error: [" + apiResponse.Code + "] " + apiResponse.Message)
}
return apiResponse.Data, nil
}
// Request via this endpoint to place 5 orders at the same time.
// The order type must be a limit order of the same symbol.
// The interface currently only supports spot trading