ftx: improve order cancel by client order id

This commit is contained in:
c9s 2022-03-02 17:25:16 +08:00
parent 5cfc266d7a
commit 07dd2e8d9c
2 changed files with 28 additions and 14 deletions

View File

@ -127,7 +127,7 @@ func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
func (e *Exchange) _queryMarkets(ctx context.Context) (MarketMap, error) {
req := e.client.NewGetMarketsRequest()
ftxMarkets,err := req.Do(ctx)
ftxMarkets, err := req.Do(ctx)
if err != nil {
return nil, err
}
@ -380,7 +380,7 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
req.StartTime(since)
req.EndTime(until)
req.Order("asc")
fills ,err := req.Do(ctx)
fills, err := req.Do(ctx)
if err != nil {
return nil, err
}
@ -556,26 +556,24 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since,
return orders, nil
}
func sortByCreatedASC(orders []order) {
sort.Slice(orders, func(i, j int) bool {
return orders[i].CreatedAt.Before(orders[j].CreatedAt.Time)
})
}
func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) error {
for _, o := range orders {
rest := e.newRest()
if err := requestLimit.Wait(ctx); err != nil {
logrus.WithError(err).Error("rate limit error")
}
if len(o.ClientOrderID) > 0 {
if _, err := rest.CancelOrderByClientID(ctx, o.ClientOrderID); err != nil {
req := e.client.NewCancelOrderByClientOrderIdRequest(o.ClientOrderID)
_, err := req.Do(ctx)
if err != nil {
return err
}
} else {
req := e.client.NewCancelOrderRequest(strconv.FormatUint(o.OrderID, 10))
_, err := req.Do(ctx)
if err != nil {
return err
}
continue
}
if _, err := rest.CancelOrderByOrderID(ctx, o.OrderID); err != nil {
return err
}
}
return nil

View File

@ -106,6 +106,22 @@ func (c *RestClient) NewCancelAllOrderRequest() *CancelAllOrderRequest {
}
}
//go:generate requestgen -method DELETE -url "/api/orders/by_client_id/:clientOrderId" -type CancelOrderByClientOrderIdRequest -responseType .APIResponse
type CancelOrderByClientOrderIdRequest struct {
client requestgen.AuthenticatedAPIClient
clientOrderId string `param:"clientOrderId,required,slug"`
}
func (c *RestClient) NewCancelOrderByClientOrderIdRequest(clientOrderId string) *CancelOrderByClientOrderIdRequest {
return &CancelOrderByClientOrderIdRequest{
client: c,
clientOrderId: clientOrderId,
}
}
type Fill struct {
Id uint64 `json:"id"`
Future string `json:"future"`