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

View File

@ -243,25 +243,40 @@ type OrderCancelAllRequest struct {
client *RestClient
params OrderCancelAllRequestParams
side *string
market *string
groupID *int64
}
func (r *OrderCancelAllRequest) Side(side string) *OrderCancelAllRequest {
r.params.Side = side
r.side = &side
return r
}
func (r *OrderCancelAllRequest) Market(market string) *OrderCancelAllRequest {
r.params.Market = market
r.market = &market
return r
}
func (r *OrderCancelAllRequest) GroupID(groupID int64) *OrderCancelAllRequest {
r.params.GroupID = groupID
r.groupID = &groupID
return r
}
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 {
return
}