bbgo_origin/pkg/cmd/cancel.go

167 lines
3.9 KiB
Go
Raw Permalink Normal View History

2020-12-29 08:00:03 +00:00
package cmd
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types"
)
type advancedOrderCancelApi interface {
CancelAllOrders(ctx context.Context) ([]types.Order, error)
CancelOrdersBySymbol(ctx context.Context, symbol string) ([]types.Order, error)
2021-01-23 09:20:26 +00:00
CancelOrdersByGroupID(ctx context.Context, groupID int64) ([]types.Order, error)
2020-12-29 08:00:03 +00:00
}
func init() {
2021-05-27 19:05:59 +00:00
cancelOrderCmd.Flags().String("session", "", "session to execute cancel orders")
cancelOrderCmd.Flags().String("symbol", "", "symbol to cancel orders")
cancelOrderCmd.Flags().Int64("group-id", 0, "group ID to cancel orders")
cancelOrderCmd.Flags().Uint64("order-id", 0, "order ID to cancel orders")
cancelOrderCmd.Flags().String("order-uuid", "", "order UUID to cancel orders")
2021-05-27 19:05:59 +00:00
cancelOrderCmd.Flags().Bool("all", false, "cancel all orders")
RootCmd.AddCommand(cancelOrderCmd)
2020-12-29 08:00:03 +00:00
}
2021-05-27 19:05:59 +00:00
var cancelOrderCmd = &cobra.Command{
Use: "cancel-order",
2020-12-29 08:00:03 +00:00
Short: "cancel orders",
Long: "this command can cancel orders from exchange",
// SilenceUsage is an option to silence usage when an error occurs.
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
symbol, err := cmd.Flags().GetString("symbol")
if err != nil {
return err
}
2021-01-23 09:20:26 +00:00
groupID, err := cmd.Flags().GetInt64("group-id")
2020-12-29 08:00:03 +00:00
if err != nil {
return err
}
2021-05-27 19:05:59 +00:00
orderID, err := cmd.Flags().GetUint64("order-id")
if err != nil {
return err
}
orderUUID, err := cmd.Flags().GetString("order-uuid")
if err != nil {
return err
}
all, err := cmd.Flags().GetBool("all")
2020-12-29 08:00:03 +00:00
if err != nil {
return err
}
sessionName, err := cmd.Flags().GetString("session")
2020-12-29 08:00:03 +00:00
if err != nil {
return err
}
environ := bbgo.NewEnvironment()
2021-02-21 08:52:47 +00:00
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
2020-12-29 08:00:03 +00:00
return err
}
if err := environ.Init(ctx); err != nil {
return err
2020-12-29 08:00:03 +00:00
}
var sessions = environ.Sessions()
2021-05-27 19:05:59 +00:00
if len(sessionName) > 0 {
ses, ok := environ.Session(sessionName)
2020-12-29 08:00:03 +00:00
if !ok {
2021-05-27 19:05:59 +00:00
return fmt.Errorf("session %s not found", sessionName)
}
if orderID > 0 || orderUUID != "" {
if orderID > 0 {
logrus.Infof("canceling order by the given order id %d", orderID)
} else if orderUUID != "" {
logrus.Infof("canceling order by the given order uuid %s", orderUUID)
}
2021-05-27 19:05:59 +00:00
err := ses.Exchange.CancelOrders(ctx, types.Order{
SubmitOrder: types.SubmitOrder{
Symbol: symbol,
},
OrderID: orderID,
UUID: orderUUID,
2021-05-27 19:05:59 +00:00
})
if err != nil {
return err
}
return nil
2020-12-29 08:00:03 +00:00
}
2021-05-27 19:05:59 +00:00
sessions = map[string]*bbgo.ExchangeSession{sessionName: ses}
2020-12-29 08:00:03 +00:00
}
for sessionID, session := range sessions {
var log = logrus.WithField("session", sessionID)
e, ok := session.Exchange.(advancedOrderCancelApi)
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)
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())
}
2020-12-29 08:00:03 +00:00
}
} else if len(symbol) > 0 {
openOrders, err := session.Exchange.QueryOpenOrders(ctx, symbol)
if err != nil {
return err
}
if err := session.Exchange.CancelOrders(ctx, openOrders...); err != nil {
return err
}
} else {
log.Error("unsupported operation")
}
}
return nil
},
}