cmd: add --all option to the cancel command

This commit is contained in:
c9s 2021-03-05 14:43:31 +08:00
parent 00109eb464
commit 33213746f4
2 changed files with 59 additions and 27 deletions

View File

@ -22,6 +22,7 @@ func init() {
CancelCmd.Flags().String("session", "", "session to execute cancel orders") CancelCmd.Flags().String("session", "", "session to execute cancel orders")
CancelCmd.Flags().String("symbol", "", "symbol to cancel orders") CancelCmd.Flags().String("symbol", "", "symbol to cancel orders")
CancelCmd.Flags().Int64("group-id", 0, "groupID to cancel orders") CancelCmd.Flags().Int64("group-id", 0, "groupID to cancel orders")
CancelCmd.Flags().Bool("all", false, "cancel all orders")
RootCmd.AddCommand(CancelCmd) RootCmd.AddCommand(CancelCmd)
} }
@ -47,6 +48,11 @@ var CancelCmd = &cobra.Command{
return err return err
} }
all, err := cmd.Flags().GetBool("all")
if err != nil {
return err
}
configFile, err := cmd.Flags().GetString("config") configFile, err := cmd.Flags().GetString("config")
if err != nil { if err != nil {
return err return err
@ -62,7 +68,7 @@ var CancelCmd = &cobra.Command{
} }
environ := bbgo.NewEnvironment() environ := bbgo.NewEnvironment()
if err := environ.ConfigureDatabase(ctx) ; err != nil { if err := environ.ConfigureDatabase(ctx); err != nil {
return err return err
} }
@ -91,7 +97,19 @@ var CancelCmd = &cobra.Command{
var log = logrus.WithField("session", sessionID) var log = logrus.WithField("session", sessionID)
e, ok := session.Exchange.(advancedOrderCancelApi) e, ok := session.Exchange.(advancedOrderCancelApi)
if ok && groupID > 0 { if ok {
if all {
log.Infof("canceling all orders")
orders, err := e.CancelAllOrders(ctx)
if err != nil {
return err
}
for _, o := range orders {
log.Info("CANCELED ", o.String())
}
} else if groupID > 0 {
log.Infof("canceling orders by group id: %d", groupID) log.Infof("canceling orders by group id: %d", groupID)
orders, err := e.CancelOrdersByGroupID(ctx, groupID) orders, err := e.CancelOrdersByGroupID(ctx, groupID)
@ -102,8 +120,7 @@ var CancelCmd = &cobra.Command{
for _, o := range orders { for _, o := range orders {
log.Info("CANCELED ", o.String()) log.Info("CANCELED ", o.String())
} }
} else if len(symbol) > 0 {
} else if ok && len(symbol) > 0 {
log.Infof("canceling orders by symbol: %s", symbol) log.Infof("canceling orders by symbol: %s", symbol)
orders, err := e.CancelOrdersBySymbol(ctx, symbol) orders, err := e.CancelOrdersBySymbol(ctx, symbol)
@ -114,7 +131,7 @@ var CancelCmd = &cobra.Command{
for _, o := range orders { for _, o := range orders {
log.Info("CANCELED ", o.String()) log.Info("CANCELED ", o.String())
} }
}
} else if len(symbol) > 0 { } else if len(symbol) > 0 {
openOrders, err := session.Exchange.QueryOpenOrders(ctx, symbol) openOrders, err := session.Exchange.QueryOpenOrders(ctx, symbol)
if err != nil { if err != nil {

View File

@ -243,25 +243,40 @@ type OrderCancelAllRequest struct {
client *RestClient client *RestClient
params OrderCancelAllRequestParams params OrderCancelAllRequestParams
side *string
market *string
groupID *int64
} }
func (r *OrderCancelAllRequest) Side(side string) *OrderCancelAllRequest { func (r *OrderCancelAllRequest) Side(side string) *OrderCancelAllRequest {
r.params.Side = side r.side = &side
return r return r
} }
func (r *OrderCancelAllRequest) Market(market string) *OrderCancelAllRequest { func (r *OrderCancelAllRequest) Market(market string) *OrderCancelAllRequest {
r.params.Market = market r.market = &market
return r return r
} }
func (r *OrderCancelAllRequest) GroupID(groupID int64) *OrderCancelAllRequest { func (r *OrderCancelAllRequest) GroupID(groupID int64) *OrderCancelAllRequest {
r.params.GroupID = groupID r.groupID = &groupID
return r return r
} }
func (r *OrderCancelAllRequest) Do(ctx context.Context) (orders []Order, err error) { func (r *OrderCancelAllRequest) Do(ctx context.Context) (orders []Order, err error) {
req, err := r.client.newAuthenticatedRequest("POST", "v2/orders/clear", &r.params) var payload = map[string]interface{}{}
if r.side != nil {
payload["side"] = *r.side
}
if r.market != nil {
payload["market"] = *r.market
}
if r.groupID != nil {
payload["groupID"] = *r.groupID
}
req, err := r.client.newAuthenticatedRequest("POST", "v2/orders/clear", payload)
if err != nil { if err != nil {
return return
} }