-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
account.go
97 lines (79 loc) · 2.21 KB
/
account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package cmd
import (
"context"
"fmt"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/bbgo"
)
func init() {
accountCmd.Flags().String("session", "", "the exchange session name for querying information")
accountCmd.Flags().Bool("total", false, "report total asset")
RootCmd.AddCommand(accountCmd)
}
// go run ./cmd/bbgo account --session=binance --config=config/bbgo.yaml
var accountCmd = &cobra.Command{
Use: "account [--session SESSION]",
Short: "show user account details (ex: balance)",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
showTotal, err := cmd.Flags().GetBool("total")
if err != nil {
return err
}
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureDatabase(ctx); err != nil {
return err
}
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)
}
a, err := session.Exchange.QueryAccount(ctx)
if err != nil {
return errors.Wrapf(err, "account query failed")
}
a.Print()
} else {
var total = types.BalanceMap{}
for _, session := range environ.Sessions() {
a, err := session.Exchange.QueryAccount(ctx)
if err != nil {
return errors.Wrapf(err, "account query failed")
}
log.Infof("--------------------------------------------")
log.Infof("SESSION %s", session.Name)
log.Infof("--------------------------------------------")
a.Print()
for c, b := range a.Balances() {
tb, ok := total[c]
if !ok {
total[c] = b
} else {
tb.Available = tb.Available.Add(b.Available)
tb.Locked = tb.Locked.Add(b.Locked)
total[c] = tb
}
}
if showTotal {
log.Infof("===============================================")
log.Infof("TOTAL ASSETS")
log.Infof("===============================================")
total.Print()
}
}
}
return nil
},
}