bbgo_origin/pkg/cmd/balances.go

70 lines
1.3 KiB
Go
Raw Normal View History

package cmd
import (
"context"
"fmt"
2021-03-14 02:31:53 +00:00
"os"
2021-03-14 02:31:53 +00:00
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
2021-03-14 02:31:53 +00:00
"github.com/c9s/bbgo/pkg/bbgo"
)
2021-03-14 02:31:53 +00:00
func init() {
balancesCmd.Flags().String("session", "", "the exchange session name for querying balances")
RootCmd.AddCommand(balancesCmd)
}
// go run ./cmd/bbgo balances --session=ftx
var balancesCmd = &cobra.Command{
Use: "balances",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
2021-03-14 02:31:53 +00:00
configFile, err := cmd.Flags().GetString("config")
if err != nil {
2021-03-14 02:31:53 +00:00
return err
}
if len(configFile) == 0 {
return errors.New("--config option is required")
}
2021-03-14 02:31:53 +00:00
if _, err := os.Stat(configFile); os.IsNotExist(err) {
return err
}
userConfig, err := bbgo.Load(configFile, false)
if err != nil {
return err
}
2021-03-14 02:31:53 +00:00
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
}
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
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
}
2021-03-14 02:31:53 +00:00
log.Infof("balances: %+v", b)
return nil
},
}