2020-12-21 06:48:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
|
|
|
|
"github.com/c9s/bbgo/pkg/exchange/binance"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.PersistentFlags().String("binance-api-key", "", "binance api key")
|
|
|
|
rootCmd.PersistentFlags().String("binance-api-secret", "", "binance api secret")
|
|
|
|
rootCmd.PersistentFlags().String("symbol", "BNBUSDT", "symbol")
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "binance-book",
|
|
|
|
Short: "binance book",
|
|
|
|
|
|
|
|
// 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 := viper.GetString("symbol")
|
|
|
|
if len(symbol) == 0 {
|
|
|
|
return errors.New("empty symbol")
|
|
|
|
}
|
|
|
|
|
|
|
|
key, secret := viper.GetString("binance-api-key"), viper.GetString("binance-api-secret")
|
|
|
|
if len(key) == 0 || len(secret) == 0 {
|
|
|
|
return errors.New("empty key or secret")
|
|
|
|
}
|
|
|
|
|
|
|
|
var exchange = binance.New(key, secret)
|
|
|
|
|
|
|
|
stream := exchange.NewStream()
|
2020-12-21 07:43:54 +00:00
|
|
|
stream.SetPublicOnly()
|
2020-12-21 06:48:57 +00:00
|
|
|
stream.Subscribe(types.BookChannel, symbol, types.SubscribeOptions{})
|
|
|
|
|
|
|
|
stream.OnBookSnapshot(func(book types.OrderBook) {
|
2021-01-23 08:59:51 +00:00
|
|
|
// log.Infof("book snapshot: %+v", book)
|
2020-12-21 06:48:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
stream.OnBookUpdate(func(book types.OrderBook) {
|
2021-01-23 08:59:51 +00:00
|
|
|
// log.Infof("book update: %+v", book)
|
2020-12-21 06:48:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
streambook := types.NewStreamBook(symbol)
|
|
|
|
streambook.BindStream(stream)
|
2021-01-23 08:59:51 +00:00
|
|
|
|
2021-01-25 06:13:39 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-streambook.C:
|
2021-01-25 06:21:19 +00:00
|
|
|
book := streambook.Get()
|
|
|
|
|
|
|
|
if valid, err := book.IsValid(); !valid {
|
2021-01-25 06:13:39 +00:00
|
|
|
log.Errorf("order book is invalid, error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-25 06:21:19 +00:00
|
|
|
bestBid, hasBid := book.BestBid()
|
|
|
|
bestAsk, hasAsk := book.BestAsk()
|
2021-01-25 06:13:39 +00:00
|
|
|
if hasBid && hasAsk {
|
|
|
|
log.Infof("================================")
|
|
|
|
log.Infof("best ask %f % -12f",
|
|
|
|
bestAsk.Price.Float64(),
|
|
|
|
bestAsk.Volume.Float64(),
|
|
|
|
)
|
|
|
|
log.Infof("best bid %f % -12f",
|
|
|
|
bestBid.Price.Float64(),
|
|
|
|
bestBid.Volume.Float64(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-01-23 08:59:51 +00:00
|
|
|
|
|
|
|
}
|
2021-01-25 06:13:39 +00:00
|
|
|
}()
|
2020-12-21 06:48:57 +00:00
|
|
|
|
|
|
|
log.Info("connecting websocket...")
|
|
|
|
if err := stream.Connect(ctx); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
|
|
|
|
|
|
|
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
|
|
|
|
log.WithError(err).Error("bind pflags error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rootCmd.ExecuteContext(context.Background()); err != nil {
|
|
|
|
log.WithError(err).Error("cmd error")
|
|
|
|
}
|
|
|
|
}
|