2021-05-27 16:47:34 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
2021-12-23 09:32:56 +00:00
|
|
|
"time"
|
2021-05-27 16:47:34 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// go run ./cmd/bbgo userdatastream --session=ftx
|
|
|
|
var userDataStreamCmd = &cobra.Command{
|
|
|
|
Use: "userdatastream",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
if userConfig == nil {
|
|
|
|
return errors.New("--config option or config file is missing")
|
|
|
|
}
|
|
|
|
|
2021-12-24 17:57:05 +00:00
|
|
|
sessionName, err := cmd.Flags().GetString("session")
|
|
|
|
if err != nil {
|
2021-05-27 16:47:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-24 17:57:05 +00:00
|
|
|
environ := bbgo.NewEnvironment()
|
|
|
|
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
|
2021-05-27 16:47:34 +00:00
|
|
|
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) {
|
2021-12-23 05:40:37 +00:00
|
|
|
log.Infof("orderUpdate: %+v", order)
|
2021-05-27 16:47:34 +00:00
|
|
|
})
|
|
|
|
s.OnTradeUpdate(func(trade types.Trade) {
|
2021-12-23 05:40:37 +00:00
|
|
|
log.Infof("tradeUpdate: %+v", trade)
|
2021-05-27 16:47:34 +00:00
|
|
|
})
|
|
|
|
s.OnBalanceUpdate(func(trade types.BalanceMap) {
|
2021-12-23 05:40:37 +00:00
|
|
|
log.Infof("balanceUpdate: %+v", trade)
|
2021-05-27 16:47:34 +00:00
|
|
|
})
|
|
|
|
s.OnBalanceSnapshot(func(trade types.BalanceMap) {
|
2021-12-23 05:40:37 +00:00
|
|
|
log.Infof("balanceSnapshot: %+v", trade)
|
2021-05-27 16:47:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
log.Infof("connecting...")
|
|
|
|
if err := s.Connect(ctx); err != nil {
|
|
|
|
return fmt.Errorf("failed to connect to %s", sessionName)
|
|
|
|
}
|
2021-12-23 06:14:48 +00:00
|
|
|
|
2021-05-27 16:47:34 +00:00
|
|
|
log.Infof("connected")
|
2021-12-23 06:21:26 +00:00
|
|
|
defer func() {
|
|
|
|
log.Infof("closing connection...")
|
|
|
|
if err := s.Close(); err != nil {
|
|
|
|
log.WithError(err).Errorf("connection close error")
|
|
|
|
}
|
2021-12-23 09:32:56 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2021-12-23 06:21:26 +00:00
|
|
|
}()
|
2021-05-27 16:47:34 +00:00
|
|
|
|
|
|
|
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
userDataStreamCmd.Flags().String("session", "", "session name")
|
|
|
|
RootCmd.AddCommand(userDataStreamCmd)
|
|
|
|
}
|