bbgo_origin/pkg/cmd/orderbook.go

71 lines
1.8 KiB
Go
Raw Normal View History

package cmd
import (
"context"
"fmt"
"syscall"
2021-03-02 14:18:41 +00:00
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
"github.com/c9s/bbgo/pkg/types"
)
2021-03-15 02:23:53 +00:00
// go run ./cmd/bbgo orderbook --exchange=ftx --symbol=BTC/USDT
var orderbookCmd = &cobra.Command{
2021-03-15 02:23:53 +00:00
Use: "orderbook",
Short: "connect to the order book market data streaming service of an exchange",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
2021-03-15 02:23:53 +00:00
exName, err := cmd.Flags().GetString("exchange")
if err != nil {
return fmt.Errorf("can not get exchange from flags: %w", err)
}
exchangeName, err := types.ValidExchangeName(exName)
if err != nil {
2021-03-15 02:23:53 +00:00
return err
}
2021-03-15 02:23:53 +00:00
ex, err := cmdutil.NewExchange(exchangeName)
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-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
}
s := ex.NewStream()
s.Subscribe(types.BookChannel, symbol, types.SubscribeOptions{})
2021-03-02 14:18:41 +00:00
s.OnBookSnapshot(func(book types.OrderBook) {
2021-03-04 00:55:52 +00:00
log.Infof("orderbook snapshot: %s", book.String())
})
s.OnBookUpdate(func(book types.OrderBook) {
log.Infof("orderbook update: %s", book.String())
2021-03-02 14:18:41 +00:00
})
2021-03-15 02:23:53 +00:00
log.Infof("connecting...")
if err := s.Connect(ctx); err != nil {
2021-03-15 02:23:53 +00:00
return fmt.Errorf("failed to connect to %s", exchangeName)
}
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
return nil
},
}
func init() {
2021-03-15 02:23:53 +00:00
// since the public data does not require trading authentication, we use --exchange option here.
orderbookCmd.Flags().String("exchange", "", "the exchange name for sync")
orderbookCmd.Flags().String("symbol", "", "the trading pair. e.g, BTCUSDT, LTCUSDT...")
RootCmd.AddCommand(orderbookCmd)
}