-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
cancel.go
166 lines (136 loc) · 3.91 KB
/
cancel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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)
CancelOrdersByGroupID(ctx context.Context, groupID int64) ([]types.Order, error)
}
func init() {
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")
cancelOrderCmd.Flags().Bool("all", false, "cancel all orders")
RootCmd.AddCommand(cancelOrderCmd)
}
var cancelOrderCmd = &cobra.Command{
Use: "cancel-order",
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
}
groupID, err := cmd.Flags().GetInt64("group-id")
if err != nil {
return err
}
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")
if err != nil {
return err
}
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
}
if err := environ.Init(ctx); err != nil {
return err
}
var sessions = environ.Sessions()
if len(sessionName) > 0 {
ses, ok := environ.Session(sessionName)
if !ok {
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)
}
err := ses.Exchange.CancelOrders(ctx, types.Order{
SubmitOrder: types.SubmitOrder{
Symbol: symbol,
},
OrderID: orderID,
UUID: orderUUID,
})
if err != nil {
return err
}
return nil
}
sessions = map[string]*bbgo.ExchangeSession{sessionName: ses}
}
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())
}
}
} 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
},
}