2021-02-27 11:28:01 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
2021-12-24 17:57:05 +00:00
|
|
|
"time"
|
2021-02-27 11:28:01 +00:00
|
|
|
|
2021-03-02 14:18:41 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-02-27 11:28:01 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-03-27 10:07:35 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
2021-02-27 11:28:01 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2021-12-24 17:57:05 +00:00
|
|
|
// go run ./cmd/bbgo orderbook --session=ftx --symbol=BTCUSDT
|
2021-02-27 11:28:01 +00:00
|
|
|
var orderbookCmd = &cobra.Command{
|
2022-02-22 08:52:14 +00:00
|
|
|
Use: "orderbook --session=[exchange_name] --symbol=[pair_name]",
|
2021-03-15 02:23:53 +00:00
|
|
|
Short: "connect to the order book market data streaming service of an exchange",
|
2022-03-03 08:34:32 +00:00
|
|
|
PreRunE: cobraInitRequired([]string{
|
|
|
|
"session",
|
|
|
|
"symbol",
|
|
|
|
}),
|
2021-02-27 11:28:01 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx := context.Background()
|
2021-03-15 02:23:53 +00:00
|
|
|
|
2021-12-24 17:57:05 +00:00
|
|
|
sessionName, err := cmd.Flags().GetString("session")
|
2021-02-27 11:28:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol, err := cmd.Flags().GetString("symbol")
|
|
|
|
if err != nil {
|
2021-03-15 02:23:53 +00:00
|
|
|
return fmt.Errorf("can not get the symbol from flags: %w", err)
|
2021-02-27 11:28:01 +00:00
|
|
|
}
|
2021-03-15 02:23:53 +00:00
|
|
|
|
2021-02-28 13:48:42 +00:00
|
|
|
if symbol == "" {
|
2021-03-15 02:23:53 +00:00
|
|
|
return fmt.Errorf("--symbol option is required")
|
2021-02-28 13:48:42 +00:00
|
|
|
}
|
2021-02-27 11:28:01 +00:00
|
|
|
|
2022-01-12 14:00:29 +00:00
|
|
|
dumpDepthUpdate, err := cmd.Flags().GetBool("dump-update")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-24 17:57:05 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:10:13 +00:00
|
|
|
orderBook := types.NewMutexOrderBook(symbol)
|
|
|
|
|
2021-12-24 17:57:05 +00:00
|
|
|
s := session.Exchange.NewStream()
|
2021-03-27 09:37:16 +00:00
|
|
|
s.SetPublicOnly()
|
2021-02-27 11:28:01 +00:00
|
|
|
s.Subscribe(types.BookChannel, symbol, types.SubscribeOptions{})
|
2021-05-22 03:36:58 +00:00
|
|
|
s.OnBookSnapshot(func(book types.SliceOrderBook) {
|
2022-01-12 14:00:29 +00:00
|
|
|
if dumpDepthUpdate {
|
|
|
|
log.Infof("orderbook snapshot: %s", book.String())
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:10:13 +00:00
|
|
|
orderBook.Load(book)
|
|
|
|
|
2022-01-12 14:00:29 +00:00
|
|
|
if ok, err := orderBook.IsValid(); !ok {
|
2021-12-24 18:10:13 +00:00
|
|
|
log.WithError(err).Panicf("invalid error book snapshot")
|
|
|
|
}
|
|
|
|
|
2022-01-12 14:00:29 +00:00
|
|
|
if bid, ask, ok := orderBook.BestBidAndAsk(); ok {
|
2021-12-24 18:10:13 +00:00
|
|
|
log.Infof("ASK | %f x %f / %f x %f | BID",
|
|
|
|
ask.Volume.Float64(), ask.Price.Float64(),
|
|
|
|
bid.Price.Float64(), bid.Volume.Float64())
|
|
|
|
}
|
2021-03-04 00:55:52 +00:00
|
|
|
})
|
2021-12-25 11:34:27 +00:00
|
|
|
|
2021-05-22 03:36:58 +00:00
|
|
|
s.OnBookUpdate(func(book types.SliceOrderBook) {
|
2022-01-12 14:00:29 +00:00
|
|
|
if dumpDepthUpdate {
|
|
|
|
log.Infof("orderbook update: %s", book.String())
|
|
|
|
}
|
2021-12-24 18:10:13 +00:00
|
|
|
orderBook.Update(book)
|
|
|
|
|
2022-01-12 14:00:29 +00:00
|
|
|
if bid, ask, ok := orderBook.BestBidAndAsk(); ok {
|
2021-12-24 18:10:13 +00:00
|
|
|
log.Infof("ASK | %f x %f / %f x %f | BID",
|
|
|
|
ask.Volume.Float64(), ask.Price.Float64(),
|
|
|
|
bid.Price.Float64(), bid.Volume.Float64())
|
|
|
|
}
|
2021-03-02 14:18:41 +00:00
|
|
|
})
|
|
|
|
|
2021-03-15 02:23:53 +00:00
|
|
|
log.Infof("connecting...")
|
2021-02-27 11:28:01 +00:00
|
|
|
if err := s.Connect(ctx); err != nil {
|
2021-12-24 17:57:05 +00:00
|
|
|
return fmt.Errorf("failed to connect to %s", sessionName)
|
2021-02-27 11:28:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 17:57:05 +00:00
|
|
|
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)
|
|
|
|
}()
|
|
|
|
|
2021-02-27 11:28:01 +00:00
|
|
|
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-28 06:42:54 +00:00
|
|
|
// go run ./cmd/bbgo orderupdate --session=ftx
|
2021-03-27 10:07:35 +00:00
|
|
|
var orderUpdateCmd = &cobra.Command{
|
2022-03-03 08:34:32 +00:00
|
|
|
Use: "orderupdate",
|
2022-02-22 08:52:14 +00:00
|
|
|
Short: "Listen to order update events",
|
2022-03-03 08:34:32 +00:00
|
|
|
PreRunE: cobraInitRequired([]string{
|
|
|
|
"config",
|
|
|
|
"session",
|
|
|
|
}),
|
2021-03-27 10:07:35 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-12-25 11:34:27 +00:00
|
|
|
|
2021-03-27 10:07:35 +00:00
|
|
|
log.Infof("connected")
|
2021-12-25 11:34:27 +00:00
|
|
|
defer func() {
|
|
|
|
log.Infof("closing connection...")
|
|
|
|
if err := s.Close(); err != nil {
|
|
|
|
log.WithError(err).Errorf("connection close error")
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}()
|
2021-03-27 10:07:35 +00:00
|
|
|
|
|
|
|
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-27 11:28:01 +00:00
|
|
|
func init() {
|
2021-12-24 17:57:05 +00:00
|
|
|
orderbookCmd.Flags().String("session", "", "session name")
|
2021-03-15 02:23:53 +00:00
|
|
|
orderbookCmd.Flags().String("symbol", "", "the trading pair. e.g, BTCUSDT, LTCUSDT...")
|
2022-01-12 14:00:29 +00:00
|
|
|
orderbookCmd.Flags().Bool("dump-update", false, "dump the depth update")
|
2021-03-27 10:07:35 +00:00
|
|
|
|
|
|
|
orderUpdateCmd.Flags().String("session", "", "session name")
|
2021-02-27 11:28:01 +00:00
|
|
|
RootCmd.AddCommand(orderbookCmd)
|
2021-03-27 10:07:35 +00:00
|
|
|
RootCmd.AddCommand(orderUpdateCmd)
|
2021-02-27 11:28:01 +00:00
|
|
|
}
|