bbgo_origin/pkg/cmd/balances.go
2022-03-03 11:36:06 +08:00

74 lines
1.5 KiB
Go

package cmd
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/c9s/bbgo/pkg/bbgo"
)
func init() {
balancesCmd.Flags().String("session", "", "the exchange session name for querying balances")
if err := balancesCmd.MarkFlagRequired("config") ; err != nil {
log.WithError(err).Errorf("can not mark --config option required")
}
RootCmd.AddCommand(balancesCmd)
}
// go run ./cmd/bbgo balances --session=ftx
var balancesCmd = &cobra.Command{
Use: "balances [--session SESSION]",
Short: "Show user account balances",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
if userConfig == nil {
return fmt.Errorf("user config is not loaded")
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
}
if len(sessionName) > 0 {
session, ok := environ.Session(sessionName)
if !ok {
return fmt.Errorf("session %s not found", sessionName)
}
b, err := session.Exchange.QueryAccountBalances(ctx)
if err != nil {
return err
}
b.Print()
} else {
for _, session := range environ.Sessions() {
b, err := session.Exchange.QueryAccountBalances(ctx)
if err != nil {
return err
}
log.Infof("SESSION %s", session.Name)
b.Print()
}
}
return nil
},
}