2021-05-27 10:35:34 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
2022-08-10 16:08:48 +00:00
|
|
|
"time"
|
2021-05-27 10:35:34 +00:00
|
|
|
|
2022-03-16 05:52:46 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-12-23 09:32:56 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
2021-05-27 10:35:34 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2022-11-24 08:46:40 +00:00
|
|
|
// go run ./cmd/bbgo kline --exchange=binance --symbol=BTCUSDT
|
2021-05-27 10:35:34 +00:00
|
|
|
var klineCmd = &cobra.Command{
|
|
|
|
Use: "kline",
|
|
|
|
Short: "connect to the kline market data streaming service of an exchange",
|
2022-03-03 08:34:32 +00:00
|
|
|
PreRunE: cobraInitRequired([]string{
|
|
|
|
"session",
|
|
|
|
"symbol",
|
|
|
|
"interval",
|
|
|
|
}),
|
2021-05-27 10:35:34 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-12-23 09:32:56 +00:00
|
|
|
environ := bbgo.NewEnvironment()
|
|
|
|
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
|
2021-05-27 10:35:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-23 09:32:56 +00:00
|
|
|
sessionName, err := cmd.Flags().GetString("session")
|
2021-05-27 10:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-23 09:32:56 +00:00
|
|
|
session, ok := environ.Session(sessionName)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("session %s not found", sessionName)
|
|
|
|
}
|
|
|
|
|
2021-05-27 10:35:34 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
interval, err := cmd.Flags().GetString("interval")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-10 16:08:48 +00:00
|
|
|
now := time.Now()
|
|
|
|
kLines, err := session.Exchange.QueryKLines(ctx, symbol, types.Interval(interval), types.KLineQueryOptions{
|
|
|
|
Limit: 50,
|
|
|
|
EndTime: &now,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infof("kLines from RESTful API")
|
|
|
|
for _, k := range kLines {
|
|
|
|
log.Info(k.String())
|
|
|
|
}
|
|
|
|
|
2021-12-23 09:32:56 +00:00
|
|
|
s := session.Exchange.NewStream()
|
2021-05-27 10:35:34 +00:00
|
|
|
s.SetPublicOnly()
|
2022-05-19 01:48:36 +00:00
|
|
|
s.Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{Interval: types.Interval(interval)})
|
2021-05-27 10:35:34 +00:00
|
|
|
|
|
|
|
s.OnKLineClosed(func(kline types.KLine) {
|
|
|
|
log.Infof("kline closed: %s", kline.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
s.OnKLine(func(kline types.KLine) {
|
|
|
|
log.Infof("kline: %s", kline.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
log.Infof("connecting...")
|
|
|
|
if err := s.Connect(ctx); err != nil {
|
2021-12-23 09:32:56 +00:00
|
|
|
return err
|
2021-05-27 10:35:34 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 09:32:56 +00:00
|
|
|
log.Infof("connected")
|
|
|
|
defer func() {
|
|
|
|
log.Infof("closing connection...")
|
|
|
|
if err := s.Close(); err != nil {
|
|
|
|
log.WithError(err).Errorf("connection close error")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-05-27 10:35:34 +00:00
|
|
|
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// since the public data does not require trading authentication, we use --exchange option here.
|
2021-12-23 09:32:56 +00:00
|
|
|
klineCmd.Flags().String("session", "", "session name")
|
2021-05-27 10:35:34 +00:00
|
|
|
klineCmd.Flags().String("symbol", "", "the trading pair. e.g, BTCUSDT, LTCUSDT...")
|
|
|
|
klineCmd.Flags().String("interval", "1m", "interval of the kline (candle), .e.g, 1m, 3m, 15m")
|
|
|
|
RootCmd.AddCommand(klineCmd)
|
|
|
|
}
|