-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
orderbook.go
170 lines (140 loc) · 4.21 KB
/
orderbook.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
167
168
169
170
package cmd
import (
"context"
"fmt"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
"github.com/c9s/bbgo/pkg/types"
)
// go run ./cmd/bbgo orderbook --session=binance --symbol=BTCUSDT
var orderbookCmd = &cobra.Command{
Use: "orderbook --session=[exchange_name] --symbol=[pair_name]",
Short: "connect to the order book market data streaming service of an exchange",
PreRunE: cobraInitRequired([]string{
"session",
"symbol",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
symbol, err := cmd.Flags().GetString("symbol")
if err != nil {
return fmt.Errorf("can not get the symbol from flags: %w", err)
}
if symbol == "" {
return fmt.Errorf("--symbol option is required")
}
dumpDepthUpdate, err := cmd.Flags().GetBool("dump-update")
if err != nil {
return err
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
}
session, ok := environ.Session(sessionName)
if !ok {
return fmt.Errorf("session %s not found", sessionName)
}
orderBook := types.NewMutexOrderBook(symbol)
s := session.Exchange.NewStream()
s.SetPublicOnly()
s.Subscribe(types.BookChannel, symbol, types.SubscribeOptions{})
s.OnBookSnapshot(func(book types.SliceOrderBook) {
if dumpDepthUpdate {
log.Infof("orderbook snapshot: %s", book.String())
}
orderBook.Load(book)
if ok, err := orderBook.IsValid(); !ok {
log.WithError(err).Panicf("invalid error book snapshot")
}
if bid, ask, ok := orderBook.BestBidAndAsk(); ok {
log.Infof("ASK | %f x %f / %f x %f | BID",
ask.Volume.Float64(), ask.Price.Float64(),
bid.Price.Float64(), bid.Volume.Float64())
}
})
s.OnBookUpdate(func(book types.SliceOrderBook) {
if dumpDepthUpdate {
log.Infof("orderbook update: %s", book.String())
}
orderBook.Update(book)
if bid, ask, ok := orderBook.BestBidAndAsk(); ok {
log.Infof("ASK | %f x %f / %f x %f | BID",
ask.Volume.Float64(), ask.Price.Float64(),
bid.Price.Float64(), bid.Volume.Float64())
}
})
log.Infof("connecting...")
if err := s.Connect(ctx); err != nil {
return fmt.Errorf("failed to connect to %s", sessionName)
}
log.Infof("connected")
defer func() {
log.Infof("closing connection...")
if err := s.Close(); err != nil {
log.WithError(err).Errorf("connection close error")
}
time.Sleep(1 * time.Second)
}()
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
return nil
},
}
// go run ./cmd/bbgo orderupdate --session=ftx
var orderUpdateCmd = &cobra.Command{
Use: "orderupdate",
Short: "Listen to order update events",
PreRunE: cobraInitRequired([]string{
"config",
"session",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
}
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
session, ok := environ.Session(sessionName)
if !ok {
return fmt.Errorf("session %s not found", sessionName)
}
s := session.Exchange.NewStream()
s.OnOrderUpdate(func(order types.Order) {
log.Infof("order update: %+v", order)
})
log.Infof("connecting...")
if err := s.Connect(ctx); err != nil {
return fmt.Errorf("failed to connect to %s", sessionName)
}
log.Infof("connected")
defer func() {
log.Infof("closing connection...")
if err := s.Close(); err != nil {
log.WithError(err).Errorf("connection close error")
}
time.Sleep(1 * time.Second)
}()
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
return nil
},
}
func init() {
orderbookCmd.Flags().String("session", "", "session name")
orderbookCmd.Flags().String("symbol", "", "the trading pair. e.g, BTCUSDT, LTCUSDT...")
orderbookCmd.Flags().Bool("dump-update", false, "dump the depth update")
orderUpdateCmd.Flags().String("session", "", "session name")
RootCmd.AddCommand(orderbookCmd)
RootCmd.AddCommand(orderUpdateCmd)
}