From 342b0dd1ddce50d1cc378495b95c8ae3f76ab492 Mon Sep 17 00:00:00 2001 From: ycdesu Date: Tue, 16 Mar 2021 22:31:17 +0800 Subject: [PATCH] ftx: cancel orders --- pkg/exchange/ftx/exchange.go | 14 ++++++++++- pkg/exchange/ftx/rest_order_request.go | 33 ++++++++++++++++++++++++++ pkg/exchange/ftx/rest_responses.go | 11 ++++++--- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/pkg/exchange/ftx/exchange.go b/pkg/exchange/ftx/exchange.go index df83ca626..8d0cc5464 100644 --- a/pkg/exchange/ftx/exchange.go +++ b/pkg/exchange/ftx/exchange.go @@ -161,7 +161,19 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, } func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) error { - panic("implement me") + for _, o := range orders { + rest := e.newRest() + if len(o.ClientOrderID) > 0 { + if _, err := rest.CancelOrderByClientID(ctx, o.ClientOrderID); err != nil { + return err + } + continue + } + if _, err := rest.CancelOrderByOrderID(ctx, o.OrderID); err != nil { + return err + } + } + return nil } func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error) { diff --git a/pkg/exchange/ftx/rest_order_request.go b/pkg/exchange/ftx/rest_order_request.go index fc3467e38..4ef294ecf 100644 --- a/pkg/exchange/ftx/rest_order_request.go +++ b/pkg/exchange/ftx/rest_order_request.go @@ -62,6 +62,39 @@ func (r *orderRequest) PlaceOrder(ctx context.Context, p PlaceOrderPayload) (ord return o, nil } +func (r *orderRequest) CancelOrderByOrderID(ctx context.Context, orderID uint64) (cancelOrderResponse, error) { + resp, err := r. + Method("DELETE"). + ReferenceURL("api/orders"). + Payloads(map[string]interface{}{"order_id": orderID}). + DoAuthenticatedRequest(ctx) + if err != nil { + return cancelOrderResponse{}, err + } + + var co cancelOrderResponse + if err := json.Unmarshal(resp.Body, &r); err != nil { + return cancelOrderResponse{}, err + } + return co, nil +} + +func (r *orderRequest) CancelOrderByClientID(ctx context.Context, clientID string) (cancelOrderResponse, error) { + resp, err := r. + Method("DELETE"). + ReferenceURL("api/orders/by_client_id"). + Payloads(map[string]interface{}{"client_order_id": clientID}). + DoAuthenticatedRequest(ctx) + if err != nil { + return cancelOrderResponse{}, err + } + + var co cancelOrderResponse + if err := json.Unmarshal(resp.Body, &r); err != nil { + return cancelOrderResponse{}, err + } + return co, nil +} func (r *orderRequest) OpenOrders(ctx context.Context, market string) (ordersResponse, error) { resp, err := r. diff --git a/pkg/exchange/ftx/rest_responses.go b/pkg/exchange/ftx/rest_responses.go index a030161bf..5911f8cec 100644 --- a/pkg/exchange/ftx/rest_responses.go +++ b/pkg/exchange/ftx/rest_responses.go @@ -3,7 +3,7 @@ package ftx import "time" type balances struct { - Success bool `json:"Success"` + Success bool `json:"success"` Result []struct { Coin string `json:"coin"` @@ -13,11 +13,16 @@ type balances struct { } type ordersResponse struct { - Success bool `json:"Success"` + Success bool `json:"success"` Result []order `json:"result"` } +type cancelOrderResponse struct { + Success bool `json:"success"` + Result string `json:"result"` +} + type order struct { CreatedAt time.Time `json:"createdAt"` FilledSize float64 `json:"filledSize"` @@ -39,7 +44,7 @@ type order struct { } type orderResponse struct { - Success bool `json:"Success"` + Success bool `json:"success"` Result order `json:"result"` }