2021-03-18 00:33:14 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2022-03-03 07:37:17 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
|
2021-03-18 00:33:14 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
accountCmd.Flags().String("session", "", "the exchange session name for querying information")
|
2021-06-30 05:24:03 +00:00
|
|
|
accountCmd.Flags().Bool("total", false, "report total asset")
|
2021-03-18 00:33:14 +00:00
|
|
|
RootCmd.AddCommand(accountCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// go run ./cmd/bbgo account --session=ftx --config=config/bbgo.yaml
|
|
|
|
var accountCmd = &cobra.Command{
|
2022-02-22 08:52:14 +00:00
|
|
|
Use: "account [--session=[exchange_name]]",
|
|
|
|
Short: "show user account details (ex: balance)",
|
2021-03-18 00:33:14 +00:00
|
|
|
SilenceUsage: true,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-06-30 05:24:03 +00:00
|
|
|
showTotal, err := cmd.Flags().GetBool("total")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-06 15:44:05 +00:00
|
|
|
sessionName, err := cmd.Flags().GetString("session")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:39:55 +00:00
|
|
|
if userConfig == nil {
|
|
|
|
return errors.New("config is not loaded")
|
2021-03-18 00:33:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:39:55 +00:00
|
|
|
|
2021-03-18 00:33:14 +00:00
|
|
|
environ := bbgo.NewEnvironment()
|
|
|
|
if err := environ.ConfigureDatabase(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-06 15:50:26 +00:00
|
|
|
if len(sessionName) > 0 {
|
|
|
|
session, ok := environ.Session(sessionName)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("session %s not found", sessionName)
|
|
|
|
}
|
2021-03-18 00:33:14 +00:00
|
|
|
|
2021-05-06 15:50:26 +00:00
|
|
|
a, err := session.Exchange.QueryAccount(ctx)
|
|
|
|
if err != nil {
|
2021-05-16 07:03:36 +00:00
|
|
|
return errors.Wrapf(err, "account query failed")
|
2021-05-06 15:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a.Print()
|
|
|
|
} else {
|
2021-06-30 05:24:03 +00:00
|
|
|
var total = types.BalanceMap{}
|
2021-05-06 15:50:26 +00:00
|
|
|
for _, session := range environ.Sessions() {
|
|
|
|
a, err := session.Exchange.QueryAccount(ctx)
|
|
|
|
if err != nil {
|
2021-05-16 07:03:36 +00:00
|
|
|
return errors.Wrapf(err, "account query failed")
|
2021-05-06 15:50:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 05:24:03 +00:00
|
|
|
log.Infof("--------------------------------------------")
|
2021-05-06 15:50:26 +00:00
|
|
|
log.Infof("SESSION %s", session.Name)
|
2021-06-30 05:24:03 +00:00
|
|
|
log.Infof("--------------------------------------------")
|
2021-05-06 15:50:26 +00:00
|
|
|
a.Print()
|
2021-06-30 05:24:03 +00:00
|
|
|
|
|
|
|
for c, b := range a.Balances() {
|
|
|
|
tb, ok := total[c]
|
|
|
|
if !ok {
|
|
|
|
total[c] = b
|
|
|
|
} else {
|
2022-02-07 03:40:30 +00:00
|
|
|
tb.Available = tb.Available.Add(b.Available)
|
|
|
|
tb.Locked = tb.Locked.Add(b.Locked)
|
2021-06-30 05:24:03 +00:00
|
|
|
total[c] = tb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if showTotal {
|
|
|
|
log.Infof("===============================================")
|
|
|
|
log.Infof("TOTAL ASSETS")
|
|
|
|
log.Infof("===============================================")
|
|
|
|
total.Print()
|
|
|
|
}
|
2021-05-06 15:50:26 +00:00
|
|
|
}
|
2021-06-30 05:24:03 +00:00
|
|
|
|
2021-03-18 00:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|