2020-09-07 06:20:03 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2020-10-11 08:46:15 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/binance"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2020-09-07 06:20:03 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Account struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
|
|
|
|
Balances map[string]types.Balance
|
|
|
|
}
|
|
|
|
|
2020-10-11 23:38:38 +00:00
|
|
|
func LoadAccount(ctx context.Context, exchange types.Exchange) (*Account, error) {
|
2020-09-07 06:20:03 +00:00
|
|
|
balances, err := exchange.QueryAccountBalances(ctx)
|
|
|
|
return &Account{
|
|
|
|
Balances: balances,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
2020-10-03 12:09:22 +00:00
|
|
|
func (a *Account) BindPrivateStream(stream types.Stream) {
|
2020-09-07 06:20:03 +00:00
|
|
|
stream.OnBalanceSnapshot(func(snapshot map[string]types.Balance) {
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
|
|
|
|
for _, balance := range snapshot {
|
|
|
|
a.Balances[balance.Currency] = balance
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) Print() {
|
|
|
|
for _, balance := range a.Balances {
|
|
|
|
if util.NotZero(balance.Available) {
|
|
|
|
log.Infof("[trader] balance %s %f", balance.Currency, balance.Available)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|